Matrix
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total Submissions: 19174 |
|
Accepted: 7207 |
Description
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 whose 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
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
This is a two-dimensional tree-like array, interval update, single-point query
#include <queue>#include<stack>#include<math.h>#include<stdio.h>#include<stdlib.h>#include<iostream>#include<limits.h>#include<string.h>#include<algorithm>using namespacestd;Const intMAX =1010;intC[max][max];intN;inta,b,e,d;intLowbit (intx) { returnX & (-x);}voidUpdata (intXintYintz) { inti,k; for(i=x; i<=n; i+=lowbit (i)) for(k=y; k<=n; k+=Lowbit (k)) C[i][k]+=Z;}intGet (intXinty) { intI,k,sum =0; for(I=x; i>0; i-=lowbit (i)) for(K=y; k>0; k-=Lowbit (k)) Sum+=C[i][k]; returnsum;}intMain () {intx; scanf ("%d",&x); while(x--) {memset (c,0,sizeof(c)); intQ; scanf ("%d%d",&n,&q); Chars; while(q--) { //cout<<q<<endl;scanf"%c",&s); if(s=='C') {scanf ("%d%d%d%d",&a,&b,&e,&d); A++,b++,e++,d++; Updata (E,d,1); Updata (A-1, d,-1); Updata (E,b-1,-1); Updata (A-1, B-1,1); } if(s=='Q') {scanf ("%d%d",&a,&b); printf ("%d\n", Get (A, b)%2); } } if(x!=0) printf ("\ n"); } return 0;}
POJ 2155 Matrix Two-dimensional tree array