[POJ 2155] Matrix

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

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

Source

POJ Monthly,Lou Tiancheng
 
题解:
二维树状数组
对于翻转操作:在二维bit中记录翻转的次数,由于是后缀操作,所以要把不在翻转范围内的区间翻回来
对于询问操作:每次查询(x,y)在bit中的前缀和最后%2就是结果
 1 /*
 2   挑战2-树状数组
 3   二维bit
 4   写起来简单,想起来难= =
 5   by-solution
 6 */
 7 #include<cstdio>
 8 #include<cstdlib>
 9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #define ll long long
14 #define lowbit(x) x&(-x)
15 using namespace std;
16 
17 const int N = 1010;
18 
19 int n,qu,c[N][N];
20 
21 int gi() {
22   int x=0,o=1; char ch=getchar();
23   while(ch!='-' && (ch<'0'||ch>'9')) ch=getchar();
24   if(ch=='-') ch=getchar(),o=-1;
25   while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
26   return o*x;
27 }
28 
29 void add(int i, int j) {
30   for(int p=i; p<=n; p+=lowbit(p))
31     for(int q=j; q<=n; q+=lowbit(q))
32       c[p][q]++;
33 }
34 
35 int query(int i, int j) {
36   int ret=0;
37   for(int p=i; p; p-=lowbit(p))
38     for(int q=j; q; q-=lowbit(q))
39       ret+=c[p][q];
40   return ret;
41 }
42 
43 int main() {//poj2155
44   freopen("1.in","r",stdin);
45   freopen("1.out","w",stdout);
46   int T=gi();
47   while(T--) {
48     memset(c,0,sizeof(c));
49     n=gi(),qu=gi();
50     for(int i=1; i<=qu; i++) {
51       char ch;
52       scanf("%c", &ch);
53       if(ch=='C') {
54     int x=gi(),y=gi(),xx=gi(),yy=gi();
55     add(x,y),add(xx+1,y),add(x,yy+1),add(xx+1,yy+1);
56       }
57       else {
58     int x=gi(),y=gi();
59     printf("%d\n", query(x,y)%2);
60       }
61     }
62     printf("\n");
63   }
64   return 0;
65 }
posted @ 2017-09-03 22:40  HLX_Y  阅读(183)  评论(0)    收藏  举报