【POJ】2155 Matrix

二维树状数组。

 1 /* poj2155 */
 2 #include <iostream>
 3 #include <string>
 4 #include <map>
 5 #include <queue>
 6 #include <vector>
 7 #include <algorithm>
 8 #include <cstdio>
 9 #include <cmath>
10 #include <cstring>
11 #include <climits>
12 #include <cctype>
13 using namespace std;
14 
15 #define MAXN 1005
16 
17 bool cnt[MAXN][MAXN];
18 int n, m;
19 
20 inline int lowest(int x) {
21     return x&-x;
22 }
23 
24 int sum(int x1, int y1) {
25     int i, j, k;
26     int ret = 0;
27     
28     for (i=x1; i; i-=lowest(i))
29         for (j=y1; j; j-=lowest(j))
30             ret += cnt[i][j];
31     return ret&1;
32 }
33 
34 void update(int x1, int y1) {
35     int i, j, k;
36     
37     for (i=x1; i<=n; i+=lowest(i))
38         for (j=y1; j<=n; j+=lowest(j))
39             cnt[i][j] = !cnt[i][j];
40 }
41 
42 int main() {
43     int t;
44     int i, j, k;
45     int x1, y1, x2, y2;
46     char s[3];
47     
48     #ifndef ONLINE_JUDGE
49         freopen("data.in", "r", stdin);
50         freopen("data.out", "w", stdout);
51     #endif
52     
53     scanf("%d", &t);
54     while (t--) {
55         scanf("%d %d", &n, &m);
56         memset(cnt, false, sizeof(cnt));
57         while (m--) {
58             scanf("%s", s);
59             if (s[0] == 'Q') {
60                 scanf("%d %d", &x1, &y1);
61                 k = sum(x1, y1);
62                 if (k)
63                     puts("1");
64                 else
65                     puts("0");
66             } else {
67                 scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
68                 update(x1, y1);
69                 update(x1, y2+1);
70                 update(x2+1, y1);
71                 update(x2+1, y2+1);
72             }
73         }
74         if (t)
75             putchar('\n');
76     }
77     
78     return 0;
79 }

 

posted on 2015-03-24 16:47  Bombe  阅读(129)  评论(0编辑  收藏  举报

导航