J :Matrix Subtraction
链接:https://ac.nowcoder.com/acm/contest/7501/J
来源:牛客网
Given a matrix M of size n\times mn×m and two integers a,b , determine weither it is possible to make all entrys of M
zero by repeatedly choosing a×b submatrices and reduce the values in the chosen matrices by 1. If possible, print “_” in one line, or print “QAQ” in one line.
输入描述:
The first line contains one integer T~(1\le T \le 100)T (1≤T≤100), denoting the number of test cases.
For each test case:
The first line contains four integers n,m,a,b~(1\le n,m \le 1000, 1\le a\le n, 1\le b\le m)n,m,a,b (1≤n,m≤1000,1≤a≤n,1≤b≤m), denoting the size of given matrix and the size of chosen submatrices respectively.
The next n_{}n
lines each contains m_{}m
integers M_{i,j}~(0\le M_{i,j} \le 10^9)M i,j (0≤M i,j ≤10 9 ), denoting the entrys of matrix M_{}M
.
It’s guaranteed that \sum nm \le 10^6∑nm≤10
6
.
输出描述:
Print T_{}T
lines each containing a string “_” or “QAQ”, denoting the answer to each test case.
示例1
输入
复制
2
2 2 1 2
1 2
1 2
2 3 1 2
1 2 1
1 2 1
输出
复制
QAQ
_
说明
For the second case, one possible scheme is to choose (1, 1) - (1, 2), (1, 2) - (1, 3), (2, 1) - (2, 2), (2, 2) - (2, 3)_{}(1,1)−(1,2),(1,2)−(1,3),(2,1)−(2,2),(2,2)−(2,3)
respectively.
题解:首先贪心,我们看最左上角的元素,如果左上角,那么我们就要对其子矩阵的所有元素减去他,然后因为左上角已经是0了,假如右边存在的话,我们就只能让它成为子矩阵的左上角然后再对所有子矩阵减去右边,以此类推,但是直接暴力的话复杂度会炸,我们需要用数据结构来维护,直接用二维差分即可,每次枚举到某个元素的时候判断它是否不小于0,如果不是就不合法.