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. Eclipse版本推荐(tomcat运行项目:web、maven)

    1.区别 Eclipse根据特性和jdk版本,从Eclipse官网可看到,分为:Neon/Oxygen/Mars/Luna/Kepler/Juno/Indigo/Helios...等等. Eclips ...

  2. Mac终端下 连接 ubuntu 16.04 ssh root@*.*.*.* permission denied 问题解决方案

    默认 Ubuntu 不开启 ssh 服务 (1)检查是否开启SSH服务  命令:ps -e|grep ssh  查看SSH服务是否开启,或者通过命令:service sshd status 可以查看某 ...

  3. Hyperledger Fabric 1.0 学习搭建 (五)--- 启动Fabric多节点集群

    5.1.启动orderer节点服务 上述操作完成后,此时各节点的compose配置文件及证书验证目录都已经准备完成,可以开始尝试启动多机Fabric集群. 首先启动orderer节点,切换至order ...

  4. ZT ---- 给孩子的信(孩子写给爸爸妈妈的信在24、25、26楼)

    胡同口 > 情感 > 婚后空间 > 给孩子的信(孩子写给爸爸妈妈的信在24.25.26楼) 给孩子的信(孩子写给爸爸妈妈的信在24.25.26楼)分享: 腾讯微博 新浪微博 QQ空间 ...

  5. 由memcpy内存越界引发的问题 && delete 和 delete []的真正区别

    今天遇到了一个问题,在程序运行到某处总会报访问到错误的地址的错误,而且每次报错的堆栈还都不一样,排查了一段时间,发现是memcpy这里出了错 ]; memset(d, , data.size() * ...

  6. std::string,std::vector,std::accumulate注意事项

    在用string做字符串拼接时,会发现随着string的增大越来越慢,原因主要是string(和vector)是基于现行内存的数据结构,在海量数据时,经常会申请新的一块内存,把原有的数据拷贝过去然后再 ...

  7. 通过JAVA从MQ读取消息的时候报错及解决

    如果是通过JAVA将消息写入到MQ,再通过JAVA去读取消息,采用MQMessage读消息的方法readUTF()去读取的时候,就不会报错,可以正常读出来.如果采用在MQ资源管理器中插入测试消息或者是 ...

  8. BZOJ1996:[HNOI2010]CHORUS 合唱队(区间DP)

    Description Input Output Sample Input 4 1701 1702 1703 1704 Sample Output 8 HINT Solution 辣鸡guide真难用 ...

  9. 二十九、利用 IntelliJ IDEA 进行代码对比的方法

    我们会有这样的需求,即:想对比出两个不同版本代码的区别.如何实现? 第 1 种:如果我们是从 SVN 检出的项目,并且想比较本地代码与从 SVN 检出时的代码相比都有那些区别,可以按如下步骤操作, 如 ...

  10. Anaconda常用命令

    conda版本: conda --version 环境信息: 激活环境后,conda info 查看环境已安装包: conda list 新建环境: conda create -n {NAME} [ ...