首页 > 代码库 > POJ·2155·Matrix

POJ·2155·Matrix

Matrix

Time Limit: 3000MS Memory Limit: 65536K

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
 
 
楼教主的题,官方题解似乎是二维树状数组,过段时间再来写。
这是偶第一棵二维线段树啊,之前想了一个小时没想明白为什么queryx的时候要queryy,后来发现这和一维是一个道理,一维要访问所有覆盖某个点的线段,二维则要访问所有覆盖该店的矩形。
 
Codes:
 1 #include<set> 2 #include<queue> 3 #include<vector> 4 #include<cstdio> 5 #include<cstdlib> 6 #include<cstring> 7 #include<iostream> 8 #include<algorithm> 9 using namespace std;10 const int N = 1010;11 #define Ch1 (i<<1)12 #define Ch2 (Ch1|1)13 #define mid(l,r) ((l+r)>>1)14 #define For(i,n) for(int i=1;i<=n;i++)15 #define Rep(i,l,r) for(int i=l;i<=r;i++)16 17 struct tnodey{18     short l,r,mid;19     int change;20 };21 22 struct tnodex{23     short l,r,mid;24     tnodey y[N<<2];25 }T[N<<2];26 int X,n,m,x1,y1,x,y,x2,y2;27 char op;28 29 void Buildy(int root,int l,int r,int i){30     T[root].y[i].l = l; T[root].y[i].r = r; T[root].y[i].mid = mid(l,r);31     T[root].y[i].change = 0;32     if(l==r) return;33     Buildy(root,l,mid(l,r),Ch1); Buildy(root,mid(l,r)+1,r,Ch2);34 }35 36 void Buildx(int i,int l,int r){37     Buildy(i,1,n,1);38     T[i].l = l; T[i].r = r; T[i].mid = mid(l,r);39     if(l==r) return;40     Buildx(Ch1,l,mid(l,r));Buildx(Ch2,mid(l,r)+1,r);41 }42 43 void Modifyy(int root,int l,int r,int i){44     if(l==T[root].y[i].l&&T[root].y[i].r==r){45         T[root].y[i].change ^= 1;46         return;47     }48     if(r<=T[root].y[i].mid) Modifyy(root,l,r,Ch1);else49     if(l>T[root].y[i].mid)  Modifyy(root,l,r,Ch2);else50     Modifyy(root,l,T[root].y[i].mid,Ch1),Modifyy(root,T[root].y[i].mid+1,r,Ch2);51 }52 53 void Modifyx(int i,int l,int r){54     if(l==T[i].l&&T[i].r==r){55         Modifyy(i,y1,y2,1);56         return;57     }58     if(r<=T[i].mid) Modifyx(Ch1,l,r);else59     if(l>T[i].mid)  Modifyx(Ch2,l,r);else60     Modifyx(Ch1,l,T[i].mid),Modifyx(Ch2,T[i].mid+1,r);61 }62 63 int queryy(int root,int i,int x){64     if(T[root].y[i].l==T[root].y[i].r) return T[root].y[i].change;65     if(x<=T[root].y[i].mid) return queryy(root,Ch1,x) + T[root].y[i].change;else66     if(x>T[root].y[i].mid)  return queryy(root,Ch2,x) + T[root].y[i].change;67 }68 69 int queryx(int i,int x){70     if(T[i].l==T[i].r) return queryy(i,1,y);71     if(x<=T[i].mid)    return queryx(Ch1,x) + queryy(i,1,y);else72     if(x>T[i].mid)     return queryx(Ch2,x) + queryy(i,1,y);73 } 74 75 76 void init(){77     scanf("%d%d",&n,&m);78     Buildx(1,1,n);79     For(i,m){80         scanf("\n");scanf("%c",&op);81         if(op==C) {82             scanf("%d%d%d%d",&x1,&y1,&x2,&y2);83             Modifyx(1,x1,x2);84         }85         else        {86             scanf("%d%d",&x,&y);87             printf("%d\n",queryx(1,x)%2);88         }89     }90     printf("\n");91 }92 93 int main(){94     scanf("%d",&X);95     For(i,X) init();96     return 0;97 }