POJ-2155 Matrix---二维树状数组+区域更新单点查询

题目链接:

https://vjudge.net/problem/POJ-2155

题目大意:

给一个n*n的01矩阵,然后有两种操作(m次)C x1 y1 x2 y2是把这个小矩形内所有数字异或一遍,Q x y 是询问当前这个点的值是多少?n<=1000 m<=50000.

解题思路:

裸的二维树状数组,但是这里是区域更新,单点查询,

做法应该是

 1  #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<string>
 6 #include<cmath>
 7 #include<set>
 8 #include<queue>
 9 #include<map>
10 #include<stack>
11 #include<vector>
12 #include<list>
13 #include<deque>
14 #include<sstream>
15 #include<cctype>
16 #define REP(i, n) for(int i = 0; i < (n); i++)
17 #define FOR(i, s, t) for(int i = (s); i < (t); i++)
18 using namespace std;
19 typedef long long ll;
20 typedef unsigned long long ull;
21 const int maxn = 1e3 + 10;
22 const double eps = 1e-10;
23 const int INF = 1 << 30;
24 const int dir[4][2] = {1,0,0,1,0,-1,-1,0};
25 const double pi = 3.1415926535898;
26 int T, n, m, cases;
27 int tree[maxn][maxn];
28 int lowbit(int x)
29 {
30     return x&(-x);
31 }
32 int sum(int x, int y)
33 {
34     int ret = 0;
35     for(int i = x; i <= n; i += lowbit(i))
36     {
37         for(int j = y; j <= n; j += lowbit(j))
38             ret += tree[i][j];
39     }
40     return ret;
41 }
42 void add(int x, int y, int d)
43 {
44     for(int i = x; i > 0; i -= lowbit(i))
45     {
46         for(int j = y; j > 0; j -= lowbit(j))
47             tree[i][j] += d;
48     }
49 }
50 int main()
51 {
52     std::ios::sync_with_stdio(false);
53     cin >> T;
54     while(T--)
55     {
56         cin >> n >> m;
57         string c;
58         int x1, y1, x2, y2;
59         memset(tree, 0, sizeof(tree));
60         while(m--)
61         {
62             cin >> c;
63             if(c[0] == 'C')
64             {
65                 cin >> x1 >> y1 >> x2 >> y2;
66                 add(x2, y2, 1);
67                 add(x1 - 1, y1 - 1, 1);
68                 add(x2, y1 - 1, -1);
69                 add(x1 - 1, y2, -1);
70             }
71             else if(c[0] == 'Q')
72             {
73                 cin >> x1 >> x2;
74                 cout<<(sum(x1, x2)&1)<<endl;
75             }
76         }
77         if(T)cout<<endl;
78     }
79     return 0;
80 }

 

posted @ 2018-04-25 20:31  _努力努力再努力x  阅读(161)  评论(0编辑  收藏  举报