Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 31892   Accepted: 11594

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 解体心得:
  • 题意就是给你一个矩阵,你可以选择一个子矩阵来进行0-1反转,然后单点查询0-1
  • 刚开始什么都没想,直接写了一个二维线段树,非常暴力,复杂度是n2logn,然后理所当然的TLE。
  • 后来发现线段树就维护一个0-1值十分的浪费,然后想了一下不就是一个树状数组就可以解决。直接记录变换次数就行了,偶数次变换就是没有变化,然后记录前缀和。但是这个题要注意的是用二维树状数组。二维树状数组的用法直接看代码就行了。
  • 在写二维树状数组的时候要注意就是前缀和记录时要容斥定理解决重复区间维护的问题。
#include <algorithm>
#include <stdio.h>
#include <cstring>
using namespace std;
const int maxn = 1010; int sum[maxn][maxn],n,m; int lowbit(int x) {
return x&-x;
} void add(int x,int y,int va) {
if(x < 1 || y < 1 || x > n || y > n)
return ;
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
sum[i][j] += va;
} int Query(int x,int y) {
int Sum = 0;
for(int i=x;i>0;i-=lowbit(i))
for(int j=y;j>0;j-=lowbit(j))
Sum += sum[i][j];
return Sum;
} int main() {
int t;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&m);
memset(sum,0,sizeof(sum));
while(m--) {
char s[3];
scanf("%s",s);
if(s[0] == 'C') {
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
add(x1,y1,1);
add(x1,y2+1,-1);
add(x2+1,y1,-1);
add(x2+1,y2+1,1);
} else {
int x,y;
scanf("%d%d",&x,&y);
int ans = Query(x,y);
printf("%d\n",ans%2);
}
}
printf("\n");
}
return 0;
}

POJ-2155:Matrix(二维树状数祖)的更多相关文章

  1. POJ 2155 Matrix(二维树状数组,绝对具体)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 Descripti ...

  2. poj 2155 Matrix (二维树状数组)

    题意:给你一个矩阵开始全是0,然后给你两种指令,第一种:C x1,y1,x2,y2 就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转,0变1,1变0 第二种:Q x1 y1,输 ...

  3. POJ 2155:Matrix 二维树状数组

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21757   Accepted: 8141 Descripti ...

  4. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  5. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  6. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  7. POJ1195Mobile phones (从二维树状数组到cdq分治)

    Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...

  8. POJ 2029 (二维树状数组)题解

    思路: 大力出奇迹,先用二维树状数组存,然后暴力枚举 算某个矩形区域的值的示意图如下,代码在下面慢慢找... 代码: #include<cstdio> #include<map> ...

  9. poj 2155 matrix 二维线段树 线段树套线段树

    题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...

随机推荐

  1. January 28 2017 Week 4 Saturday

    Do what you say, say what you do. 做你说过的,说你能做的. Do more than what you say, and sometimes say more tha ...

  2. node-7.2.1 already installed, it's just not linked

    直接在terminal下运行以卸载node和nvm: sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,shar ...

  3. shared_ptr 的使用及注意事项

    1. 声明#include <boost/shared_ptr.hpp> class UsersBitmap { ... } typedef boost::shared_ptr<Us ...

  4. Hibernate多对多关联关系

    今天遇到一个问题:有一个的类Reckoning,一个类AccountItem.这两个类之间的关系时多对多的关联关系. 一开始是由AccountItem来维护关联关系的,也就是Reckoning.hbm ...

  5. BZOJ1797:[AHOI2009]最小割(最小割)

    Description A,B两个国家正在交战,其中A国的物资运输网中有N个中转站,M条单向道路.设其中第i (1≤i≤M)条道路连接了vi,ui两个中转站,那么中转站vi可以通过该道路到达ui中转站 ...

  6. 【HAOI2010】工厂选址题解

    题目描述 某地区有m座煤矿,其中第i号矿每年产量为ai吨,现有火力发电厂一个,每年需用煤b吨,每年运行的固定费用(包括折旧费,不包括煤的运费)为h元,每吨原煤从第i号矿运到原有发电厂的运费为Ci0(i ...

  7. Time Zone 【模拟时区转换】(HDU暑假2018多校第一场)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6308 Time Zone Time Limit: 2000/1000 MS (Java/Others)  ...

  8. 高考结束了,在门头沟有没有想学php建站的。

    教你简单的html 教你文章后台管理 数据库管理及备份 编程工具的使用 如何找到你要学习的内容

  9. H5 移动端开发中 ios/安卓坑 和经验总结

    1. ios new时间对象,需要用逗号隔开传日期的方式, 不支持 new Date('2019-03-01 08:00:00') 格式: 支持以下两种方式: 2. ios个别版本对fixed的属性的 ...

  10. iOS | NSProxy

    Objective-C作为一种动态消息型语言,其机制不同于Java ,C#等编译型语言. 它将数据类型的确定等工作推迟到了运行时期来执行,并且它调用方法的方式实质是像对象发送消息,根据selector ...