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,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...
随机推荐
- Intellij idea用快捷键自动生成序列化id
ntellij idea用快捷键自动生成序列化id 类继承了Serializable接口之后,使用alt+enter快捷键自动创建序列化id 进入setting→inspections→seriali ...
- Go语言(二) 继承和重载
继承 package main import "fmt" type Skills []string type person struct { name string age int ...
- 在Kubernetes上运行SAP UI5应用(下): 一个例子体会Kubernetes内容器的高可用性和弹性伸缩
上一篇文章 在Kubernetes上运行SAP UI5应用(上),我介绍了如何在Docker里运行一个简单的SAP UI5应用,并且已经成功地将一个包含了这个UI5应用的docker镜像上传到Dock ...
- python UI自动化实战记录十一: 总结
首先说说为什么想起来用自动化脚本来实现该项目的自动化. 工作还是以手工测试为主,业务驱动型的项目大概就是这样,业务不停地变,不断的迭代. 自动化测试实施的先决条件: 一 得有时间. 如果有时间大部分的 ...
- Oracle拆分字符串,字符串分割的函数。
第一种:oracle字符串分割和提取 分割 create or replace function Get_StrArrayLength ( av_str varchar2, --要分割的字符串 av_ ...
- 树形背包O(n * v^2)入门
我虽然做了好几道树形背包的题,但是一直不是十分理解,对于每一道题,总是看题解就明白,然后换一道题自己写不出来.临近NOIP,gg让我们强化一下背包以及树形背包,我也恰有此打算,于是又开始从头学习了树形 ...
- [19/04/04-星期四] IO技术_CommonsIO(通用IO,别人造的轮子,FileUtils类 操作文件 & IOUtilsl类 操作里边的内容 )
一.概念 JDK中提供的文件操作相关的类,但是功能都非常基础,进行复杂操作时需要做大量编程工作.实际开发中,往往需要 你自己动手编写相关的代码,尤其在遍历目录文件时,经常用到递归,非常繁琐. Apac ...
- 用LinkedList list实现栈的功能
package homework; public class Dog extends Pet { String strain = "dogxx"; int love=80; pub ...
- java中常见的math方法
java.lang.Math : 绝对值: static int abs(int a) static long abs(long a) static float abs(float a) s ...
- POJ 3216 Prime Path(打表+bfs)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27132 Accepted: 14861 Desc ...