POJ2155 Matrix 【二维树状数组】+【段更新点查询】
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 17766 | Accepted: 6674 |
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
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
题意:对一个给定size且初始化为0的矩阵。运行一些命令,Q A B为查看arr[a][b]元素的值,C X1 Y1 X2 Y2为将(x1, y1) (x2, y2)矩形范围内的全部点0、1翻转。
题解:树状数组模式二的使用方法。段更新,点查询。update(x2, y2)表示从(1, 1)到(x2, y2)范围内的全部点都要翻转一次,可是这样会把给定范围外的一些点也翻转到,因此须要将这些点翻转回去。
#include <stdio.h>
#include <string.h>
#define maxn 1002 int size, tree[maxn][maxn]; int lowBit(int x){ return x & (-x); } //向下更新表示A[1]...A[i]每一个元素都要 += val,推广到二维同理
void update(int x, int y, int val)
{
int temp;
while(x > 0){
temp = y;
while(temp > 0){
tree[x][temp] += val;
temp -= lowBit(temp);
}
x -= lowBit(x);
}
} int query(int x, int y)
{
int sum = 0, temp;
while(x <= size){
temp = y;
while(temp <= size){
sum += tree[x][temp];
temp += lowBit(temp);
}
x += lowBit(x);
}
return sum;
} int main()
{
//freopen("stdin.txt", "r", stdin); int cas, q, a, b, c, d;
char com[2];
scanf("%d", &cas); while(cas--){
scanf("%d%d", &size, &q);
memset(tree, 0, sizeof(tree)); while(q--){
scanf("%s%d%d", com, &a, &b);
if(com[0] == 'C'){
scanf("%d%d", &c, &d);
update(c, b - 1, -1);
update(a - 1, d, -1);
update(a - 1, b - 1, 1);
update(c, d, 1);
}else printf("%d\n", query(a, b) & 1);
} if(cas) printf("\n");
}
return 0;
}
POJ2155 Matrix 【二维树状数组】+【段更新点查询】的更多相关文章
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- poj----2155 Matrix(二维树状数组第二类)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 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 ...
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
- hdu 2642二维树状数组 单点更新区间查询 模板题
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...
- POJ2155 Matrix(二维树状数组||区间修改单点查询)
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row an ...
- poj2155一个二维树状数组
...
- POJ 2155 Matrix(二维树状数组,绝对具体)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20599 Accepted: 7673 Descripti ...
- POJ 2155:Matrix 二维树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 21757 Accepted: 8141 Descripti ...
随机推荐
- (转)Spring中的事务操作
http://blog.csdn.net/yerenyuan_pku/article/details/70024364 事务的回顾 什么是事务 事务是逻辑上的一组操作,组成这组操作的各个逻辑单元,要么 ...
- java将字段映射成另一个字段,关于 接口传参 字段不对应转换
在接口开发中我们经常会遇到一个问题,打个比方,我们的实体类A中有两个字段user和pwd但是接口中需要username和password这怎么办呢,我想到了两种方法:1.新创建一个实体类B或者new一 ...
- spring的设计思想
在学习Spring框架的时候, 第一件事情就是分析Spring的设计思想 在学习Spring的时候, 需要先了解耦合和解耦的概念 耦合: 简单来说, 在软件工程当中, 耦合是指对象之间的相互依赖 耦合 ...
- Fragment Transactions和Activity状态丢失
本文由 伯乐在线 - 独孤昊天 翻译.未经许可,禁止转载!英文出处:androiddesignpatterns.欢迎加入翻译组. 下面的堆栈跟踪和异常代码,自从Honeycomb的初始发行版本就一直使 ...
- <Redis> 入门X 分布式锁
分布式其实就是多进程的程序,当多个进程访问一个资源,会造成问题: 1.资源共享的竞争问题 2.数据的安全性 分布式锁的解决方案: 1.怎么去获取锁 数据库 zookeeper redis 2.怎么释放 ...
- sh与bash执行语法严谨问题
在Linux中,我们知道有几种方式可以运行.sh脚本 通过sh或者bash命令来运行 通过source来运行 通过./xxx.sh来运行(这种方式要求对脚本文件有r和x权限才行) 今天在写脚本的过程中 ...
- 树莓派 -- 按键 (key)使用BCM2835 gpio library
BCM2835 GPIO library介绍 This is a C library for Raspberry Pi (RPi). It provides access to GPIO and ot ...
- buf.slice()
buf.slice([start[, end]]) start {Number} 默认:0 end {Number} 默认:buffer.length 返回:{Buffer} 返回一个指向相同原始内存 ...
- 多.h项目出现的问题:使用了预编译头依然出现error LNK2005:***obj已在***obj中定义与c++ error C2011: “xxx”:“class”类型重定义解决办法
使用了预编译头依然出现error LNK2005:***obj已在***obj中定义 造成该问题的可能性比较多,本人将在今后遇到时添加进来,今天先放出本人遇到的一种情况. 多重包含含有变量定义的.h文 ...
- C语言学习4
C/C++语言五大内存分区:堆.栈.自由存储区.全局/静态存储区和常量存储区 栈:就是那些由编译器在需要的时候分配,在不需要的时候自动清楚的存储区,里面的变量通常是全局变量.函数参数等. 堆:就是那些 ...