Matrix
Time limit:3000 MSMemory limit:65536 KB
64-bit integer Io format:% I64d, % i64uJava class name:Main
[Submit] [Status] [discuss]
Descriptiongiven 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]. inputthe 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. outputfor 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
# Include <iostream> # include <stdio. h> # include <string. h> using namespace STD; # define Max 1005int C [Max] [Max]; int N; int lowbit (int x) {return X &-X ;} void Update (int x, int y, int Val) {for (INT I = x; I <= N; I + = lowbit (I )) {for (Int J = y; j <= N; j + = lowbit (j) {C [I] [J] + = Val ;}}} int get_sum (int x, int y) {int S = 0; For (INT I = x; I> 0; I-= lowbit (I )) {for (Int J = y; j> 0; j-= lowbit (j) {S + = C [I] [J] ;}} return s ;} int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt", "W", stdout); int CC, t, x1, x2, Y1, Y2, a, B; char ch; scanf ("% d", & CC); For (INT I = 1; I <= cc; I ++) {memset (C, 0, sizeof (c); scanf ("% d", & N, & T); getchar (); for (Int J = 0; j <t; j ++) // subscripts cannot start from 0 and will have an endless loop {scanf ("% C", & Ch ); if (CH = 'C') {scanf ("% d", & X1, & Y1, & X2, & Y2); getchar (); update (x1, Y1, 1); Update (x1, y2 + 1,1); Update (X2 + 1, Y1, 1); Update (X2 + 1, y2 + 1,1);} else {scanf ("% d", & A, & B); getchar (); printf ("% d \ n ", get_sum (a, B) % 2) ;}} printf ("\ n") ;}return 0 ;}
* 00 of the matrix is in the upper left corner.
Http://blog.csdn.net/zxy_snow/article/details/6264135 illustration