码迷,mamicode.com
首页 > 其他好文 > 详细

poj2155 Matrix

时间:2018-02-16 18:40:30      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:ring   name   int   区间修改   include   pos   ++   set   const   

思路:

二维树状数组。点修改区间查询转化为区间修改点查询。

http://blog.csdn.net/z309241990/article/details/9615259

http://blog.csdn.net/acm_BaiHuzi/article/details/46819049

实现:

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 const int MAXN = 1005; 
 5 int bit[MAXN][MAXN], x, n, t;
 6 
 7 int lowbit(int x)
 8 {
 9     return x & -x;
10 }
11 
12 void add(int x, int y, int d)
13 {
14     for (int i = x; i <= n; i += lowbit(i))
15     {
16         for (int j = y; j <= n; j += lowbit(j))
17         {
18             bit[i][j] += d;
19         }
20     }
21 }
22 
23 int sum(int x, int y)
24 {
25     int ans = 0;
26     for (int i = x; i > 0; i -= lowbit(i))
27     {
28         for (int j = y; j > 0; j -= lowbit(j))
29         {
30             ans += bit[i][j];
31         }
32     }
33     return ans;
34 }
35 
36 int main()
37 {
38     ios::sync_with_stdio(false);
39     cin >> x;
40     char c; int x1, y1, x2, y2;
41     while (x--)
42     {
43         memset(bit, 0, sizeof bit);
44         cin >> n >> t;
45         for (int i = 0; i < t; i++)
46         {
47             cin >> c;
48             if (c == Q) 
49             {
50                 cin >> x1 >> y1;
51                 cout << (sum(x1, y1) & 1) << endl;
52             }
53             else
54             {
55                 cin >> x1 >> y1 >> x2 >> y2;
56                 add(x1, y1, 1);
57                 add(x1, y2 + 1, -1);
58                 add(x2 + 1, y1, -1);
59                 add(x2 + 1, y2 + 1, 1);
60             }    
61         }
62         cout << endl;
63     }
64     return 0;
65 }

 

poj2155 Matrix

标签:ring   name   int   区间修改   include   pos   ++   set   const   

原文地址:https://www.cnblogs.com/wangyiming/p/8450345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!