Matrix
Time limit:3000 Ms |
|
Memory limit:65536 K |
Total submissions:18021 |
|
Accepted:6755 |
Description
Given an N * n matrix A, whose elements are either 0 or 1. A [I, j] means the number in the I-th row and J-th Column. initially we have a [I, j] = 0 (1 <= I, j <= N ).
We can change the matrix in the following way. given a rectangle whose upper-left corner is (x1, Y1) and lower-right corner is (X2, Y2 ), we change all the elements in the rectangle by using "not" Operation (if it is a '0' then change it into '1' otherwise change it into '0 '). to maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions.
1. c X1 Y1 X2 Y2 (1 <= X1 <= X2 <= N, 1 <= Y1 <= Y2 <= N) changes the matrix by using the rectangle whose upper-left corner is (x1, Y1) and lower-right corner is (X2, Y2 ).
2. q x y (1 <= X, Y <= N) querys A [x, y].
Input
The first line of the input is an integer x (x <= 10) representing the number of test cases. The following X blocks each represents a test case.
The first line of each block contains two numbers N and T (2 <= n <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. the following T lines each represents an instruction having the format "q x y" or "C X1 Y1 X2 Y2", which has been described above.
Output
For each querying output one line, which has an integer representing a [x, y].
There is a blank line between every two continuous test cases.
Sample Input
12 10C 2 1 2 2Q 2 2C 2 1 2 1Q 1 1C 1 1 2 1C 1 2 1 2C 1 1 2 2Q 1 1C 1 1 2 1Q 2 1
Sample output
1001
Source
Poj monthly, Lou Tiancheng two-dimensional tree array, general modification and query of two-dimensional tree array modification void add (int I, Int J, int D, int N)
{
Int X, Y;
For (x = I; x <= N; x + = lowbit (x ))
For (y = J; y <= N; y + = lowbit (y ))
C [x] [Y] + = D;
}
Query int sum (int I, Int J)
{
Int A = 0, x, y;
For (x = I; x> = 0; X-= lowbit (x ))
For (y = J; y> = 0; y-= lowbit (y ))
A + = C [x] [Y];
Return;
}
The N * n matrix is given. The values in the matrix can only be 0 or 1. The initial values are all 0. c X1 Y1 X2 Y2 indicates the values of (x1, Y1) and (X2, y2) converts all values in the enclosed rectangle. q x y queries the current value of the vertex, which can also be considered as the number of times the vertex has changed; in this question, the tree array is updated from the back to the back, and the value of each vertex indicates the number of changes from all vertices forward to the point. For C, the first operation is to () to (X2, Y2) all vertices + 1, representing the number of changes in the rectangle with the point forward + 1; then (x1-1, Y2) (X2, y1-1) are-1, (x1-1, y1-1) + 1: balance out the vertices with multiple operations. When the number of transformations for this vertex is calculated, the values are accumulated from the vertex to the backend. The sum is the number of transformations for this vertex. If an odd number represents 1, otherwise, it is 0.
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int c[1010][1010] ;int lowbit(int x){ return x & -x ;}void add(int i,int j,int d){ int x , y ; for(x = i ; x > 0 ; x -= lowbit(x)) for(y = j ; y > 0 ; y -= lowbit(y)) c[x][y] += d;}int sum(int i,int j,int n){ int a = 0 , x , y ; for(x = i ; x <= n ; x += lowbit(x)) for(y = j ; y <= n ; y += lowbit(y)) a += c[x][y] ; return a ;}int main(){ int t , tt , i , j , n , m , x1 , y1 , x2 , y2 ; char ch ; scanf("%d", &t); for(tt = 1 ; tt <= t ; tt++) { scanf("%d %d", &n, &m); memset(c,0,sizeof(c)); while(m--) { getchar(); scanf("%c", &ch); if( ch == 'C' ) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); add(x2,y2,1); add(x1-1,y2,-1); add(x2,y1-1,-1); add(x1-1,y1-1,1); } else { scanf("%d %d", &x1, &y1); printf("%d\n", sum(x1,y1,n)%2 ); } } if(tt != t) printf("\n"); } return 0;}