Matrix
| Time limit:3000 Ms |
|
Memory limit:65536 K |
| Total submissions:17976 |
|
Accepted:6737 |
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
# Include "iostream" # include "cstdio" # include "cstring" using namespace STD; const int MS = 1e3 + 10; int map [MS] [MS]; int N, m; int lowbit (INT t) {return T & (-T);} void updata (int x, int y, int d) {While (x> 0) {int Y1 = y; while (Y1> 0) {map [x] [Y1] + = D; Y1-= lowbit (Y1 );} x-= lowbit (x) ;}} int getsum (int x, int y) {int sum = 0; while (x <= N) {int Y1 = y; while (Y1 <= N) {sum + = map [x] [Y1]; Y1 + = lowbit (Y1);} X + = lowbit (x );} return sum;} int main () {int I, J, K, T, tcase; // # ifndef oo // cout <"dkkjdkk" <Endl; // # endif CIN> tcase; while (tcase --) {CIN> N> m; memset (MAP, 0, sizeof (MAP )); char s [5]; // avoid the impact of carriage return; otherwise, getchar () while (M --) {CIN> S; If (s [0] = 'C ') {int X1, Y1, X2, Y2; CIN> x1> Y1> X2> Y2; updata (X2, Y2, 1); updata (x1-1, Y2, -1); updata (x1-1, y1-1, 1); updata (X2, y1-1,-1); // updata (x1-1, y1-1, 1 );} else if (s [0] = 'q') {int X, Y; CIN> x> Y; cout <getsum (x, y) % 2 <Endl ;}}if (tcase) cout <Endl ;}return 0 ;}