POJ-2155:Matrix(二维树状数祖)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 31892 | Accepted: 11594 |
Description
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
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
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(二维树状数祖)的更多相关文章
- POJ 2155 Matrix(二维树状数组,绝对具体)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20599 Accepted: 7673 Descripti ...
- poj 2155 Matrix (二维树状数组)
题意:给你一个矩阵开始全是0,然后给你两种指令,第一种:C x1,y1,x2,y2 就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转,0变1,1变0 第二种:Q x1 y1,输 ...
- POJ 2155:Matrix 二维树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 21757 Accepted: 8141 Descripti ...
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- POJ1195Mobile phones (从二维树状数组到cdq分治)
Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...
- POJ 2029 (二维树状数组)题解
思路: 大力出奇迹,先用二维树状数组存,然后暴力枚举 算某个矩形区域的值的示意图如下,代码在下面慢慢找... 代码: #include<cstdio> #include<map> ...
- poj 2155 matrix 二维线段树 线段树套线段树
题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...
随机推荐
- Oracle 死锁处理
一.数据库死锁的现象程序在执行的过程中,点击确定或保存按钮,程序没有响应,也没有出现报错.二.死锁的原理当对于数据库某个表的某一列做更新或删除等操作,执行完毕后该条语句不提交,另一条对于这一列数据做更 ...
- Python之Dict和Set类型(入门5)
转载请标明出处: http://www.cnblogs.com/why168888/p/6407905.html 本文出自:[Edwin博客园] Python之Dict和Set类型 1. Python ...
- CF13C Sequence
嘟嘟嘟 偶然看到的这道题,觉得有点意思,就做了. 首先题里说修改后的数列只能出现修改前的数,那么状态之间的转移也就之可能在这些数之间. 令f[i][j]表示第 i 个数改成原序列第 j 小的数时的最小 ...
- 20、Springboot 与数据访问(JDBC/自动配置)
简介: 对于数据访问层,无论是SQL还是NOSQL,Spring Boot默认采用整合 Spring Data的方式进行统一处理,添加大量自动配置,屏蔽了很多设置.引入 各种xxxTemplate,x ...
- 几句代码简单实现IoC容器
前言 最近在调试EasyNetQ代码的时候发现里面有一段代码,就是IoC容器的简单实现,跟着他的代码敲了一遍,发现了奇妙之处.当然也是因为我才疏学浅导致孤陋寡闻了.他的思路就是通过动态调用构造函数生成 ...
- Linux Shell 编程 文件转置问题
给定一个文件 file.txt,转置它的内容. 你可以假设每行列数相同,并且每个字段由 ' ' 分隔. 示例: 假设 file.txt 文件内容如下: name age alice 21 ryan 3 ...
- vue.js 组件监听
一.在通过点击事件触发的子组件中: addCart(event) { if (!event._constructed) { return; } if (!this.food.count) { Vue. ...
- Unity中自定义扩展方法
问题背景 在使用unity开发过程中,通常会遇到一种情况,比如说给物体重新赋值坐标的问题, Transfrom tran: ,pos_y=,pos_z=; tran.position=new Vect ...
- AngularJS简介-起步阶段
AngularJS 是一个为动态WEB应用设计的结构框架,提供给大家一种新的开发应用方式,这种方式可以让你扩展HTML的语法,以弥补在构建动态WEB应用时静态文本的不足,从而在web应用程序中使用HT ...
- Linux下的压缩解压缩命令
*.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— *.gz ...