Describe
Given an n*n matrix A, whose elements is 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. Given a rectangle whose upper-left corner is (x1, y1) and Lower-right Corner are (x2, y2), we change all the elements in th e Rectangle by using the "not" operation (if it was a ' 0 ' then change it to ' 1 ' otherwise change it into ' 0 '). To maintain the information of the matrix, you is asked to write a program to receive and execute both kinds of instructio Ns.
1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= N, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whos E upper-left Corner is (x1, y1) and Lower-right Corner are (x2, y2).
2. Q x Y (1 <= x, y <= N) querys a[x, y].
Input
The first line of the input was an integer X (x <=) representing the number of test cases. The following X blocks each represents a test case.
The first line of each block contains numbers n and T (2 <= n <=, 1 <= T <= 50000) representing the S Ize of the Matrix and the number of the instructions. The following T lines each represents an instruction have the format "Q x y" or "C x1 y1 x2 y2", which has been describe D above.
Output
For each querying output one line, which have an integer representing A[x, y].
There is a blank line between every the continuous test cases.
Sample input
1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1
Sample output
1
0
0
1
Test instructions
The matrix of the initial n*n is all 0
Q Operations
1.[X1,Y1]-[X2,Y2] in the reverse operation
2. Query the value of [x1,y1]
Exercises
1. Interval update is divided into 4 blocks ([X1,y1]-[n,n]) ([X2,x2]-[n,n]) ([X2+1,y1]-[n,n]) ([X1,y2+1]-[n,n]), each interval is +1 operation, only guaranteed [x1,y1]-[x2,y2]+1, the remaining + 2 or +4
2. Single-point query [x1,y1] value, only need to query the value of [x1,y1]%2
Code
1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intn=1234;5 intN;6 7 structbit2{8 intSum[n][n];9 voidInit () {memset (sum,0,sizeof(sum));}Ten intLowbit (intx) {returnx& (-x);} One voidUpdateintXintYintW) A { - for(inti=x;i<=n;i+=lowbit (i)) - for(intj=y;j<=n;j+=Lowbit (j)) thesum[i][j]+=W; - } - intQueryintXinty) - { + intans=0; - for(intI=x;i>0; i-=lowbit (i)) + for(intJ=y;j>0; j-=Lowbit (j)) Aans+=Sum[i][j]; at returnans; - } - }t; - - intMain () - { in intT,q,o; -scanf"%d",&t); to while(t--) + { - if(o++) printf ("\ n"); the t.init (); *scanf"%d%d",&n,&q); $ for(intI=0; i<q;i++)Panax Notoginseng { - Charop[3]; the intX1,y1,x2,y2; +scanf"%s", op); A if(op[0]=='C') the { +scanf"%d%d%d%d",&x1,&y1,&x2,&y2); -T.update (X1,y1,1); $T.update (x2+1, Y1,1); $T.update (x1,y2+1,1); -T.update (x2+1, y2+1,1); - } the Else -scanf"%d%d", &x1,&y1), printf ("%d\n", T.query (x1,y1)%2);Wuyi } the } -}
TOJ 2722 Matrix (tree array interval inverse single point query)