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 ...
随机推荐
- notepad++新建文档时,会出现语法错误的红色下波浪线
notepad++新建文档时,会出现语法错误的红色下波浪线: 原因:新建文档时默认设置语言为PHP. 解决方法:修改默认语言为java或JavaScript,如下: 小结:打开文档时,也可能出现下波浪 ...
- stl sort和qsort的使用
好不容易使用了下stl的qsort函数,顺便和sort函数一起总结下: 很多时候我们都需要用到排序. 例如: 1 #include <iostream> #include <algo ...
- 分分钟钟学会Python - 文件操作
文件操作 1 文件基本操作 obj = open('路径',mode='模式',encoding='编码') obj.write() # 写入 obj.read() # 读取 obj.close() ...
- 集训第六周 数学概念与方法 概率 F题
Submit Status Description Sometimes some mathematical results are hard to believe. One of the common ...
- [bzoj1208][HNOI2004][宠物收养所] (平衡树)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- SpringBoot第十六篇:自定义starter
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言 这一段时间 ...
- 关于 <customErrors> 标记的“mode”属性设置为“Off”的问题的解决方案
用 权限问题 <customErrors> 标记的“mode”属性设置为“Off”. 权限问题标记的“mode”属性设置为“Off”.说明: 服务器上出现应用程序错误.此应用程序的当前自定 ...
- python接口测试之Http请求(三)
python的强大之处在于提供了很多的标准库,这些标准库可以直接调用,本节部分,重点学习和总结在 接口测试中Python的Http请求的库的学习. 首先来看httplib,官方的解释为:本模块定义了类 ...
- [luoguP1042] 乒乓球(模拟)
传送门 终于过了这sb题了. 当初我连这道题都A不了(╯▔皿▔)╯ 代码 #include <cstdio> #include <iostream> #define N 100 ...
- [K/3Cloud] 隐藏菜单后,如何在插件间接的调用隐藏菜单的操作
使用场景: 动态表单里面挂了个单据的序时薄,序时薄有菜单,但是把序时薄的工具栏隐藏了.新增,修改全部动态表单自己写.删除和过滤我想间接调用下隐藏的序时薄的删除和过滤按钮的操作.在插件里如何实现? 答: ...