【poj2155】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

T he 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

 

【题意】

n*n坐标图起初都为0,C:翻转左下和右上两个坐标围成的矩阵中所有点,Q:查询此点的0 1状态

【思路】

二维树状数组区间修改+单点查询模板题,先考虑一维。

一维单点查询就是前缀和,即query(x)。

区间修改先让s-n都加num,再让t+1-n减去num,即update(s, num),update(t+1, -num)。

二维的单点查询变成二维就好了query(x, y)。

区间修改update(x1, y1, num), update(x2+1, y1, -num), update(x1, y2+1, -num), update(x2+1, y2+1, num)。开始也是黑人问号,在纸上画几笔就看出来了。

这道题问得是0 1状态,那么只要统计翻转次数是偶次还是奇次就行了

【代码】

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 const int N = 1005;
 7 int c[N][N], n;
 8 int lowbit(int x)
 9 {
10     return x&(-x);
11 }
12 void update(int x, int y, int m)
13 {
14     for(int i = x; i <= n; i += lowbit(i))
15         for(int j = y; j <= n; j += lowbit(j))
16             c[i][j] += m;
17 }
18 int query(int x, int y)
19 {
20     int sum = 0;
21     for(int i = x; i > 0; i -= lowbit(i))
22         for(int j = y; j > 0; j-= lowbit(j))
23             sum += c[i][j];
24     return sum;
25 }
26 int main()
27 {
28     int t, m;
29     cin>>t;
30     while(t--)
31     {
32         memset(c, 0, sizeof c);
33         scanf("%d%d", &n, &m);
34         while(m--)
35         {
36             char c;
37             scanf(" %c", &c);
38             if(c == 'C')
39             {
40                 int x1, x2, y1, y2;
41                 scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
42                 update(x1, y1, 1);
43                 update(x1, y2+1, -1);
44                 update(x2+1, y1, -1);
45                 update(x2+1, y2+1, 1);
46             }
47             else
48             {
49                 int x, y;
50                 scanf("%d%d", &x, &y);
51                 printf("%d\n", query(x, y)&1);
52             }
53         }
54         if(t) puts("");
55     }
56     return 0;
57 }

 

转载于:https://www.cnblogs.com/lesroad/p/8479839.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值