POJ2155 树状数组
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 26650 | Accepted: 9825 |
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
Source
//可以画个图看一下,每次更新(x1,y1),(x2+1,y1),(x1,y2+1),(x2+1,y2+1)这4个点的值
//时所有的点都不会被影响。求每个点的sum(并不真实),奇数是1,偶数是0.
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
int A[maxn][maxn];
int cas,n,m,x1,x2,y1,y2;
int Lowbit(int x){
return x&(-x);
}
void Add(int x,int y,int val)
{
for(int i=x;i<=;i+=Lowbit(i)){
for(int j=y;j<=;j+=Lowbit(j)){
A[i][j]+=val;
}
}
}
int Query(int x,int y)
{
int s=;
for(int i=x;i>;i-=Lowbit(i)){
for(int j=y;j>;j-=Lowbit(j)){
s+=A[i][j];
}
}
return s;
}
int main()
{
scanf("%d",&cas);
while(cas--){
scanf("%d%d",&n,&m);
memset(A,,sizeof(A));
char ch[];
while(m--){
scanf("%s",ch);
if(ch[]=='C'){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
Add(x1,y1,);
Add(x1,y2+,);
Add(x2+,y2+,);
Add(x2+,y1,);
}
else{
scanf("%d%d",&x1,&y1);
int ans=Query(x1,y1);
//cout<<ans<<endl;
printf("%d\n",ans&);
}
}
printf("\n");
}
return ;
}
POJ2155 树状数组的更多相关文章
- poj2155 树状数组 Matrix
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14826 Accepted: 5583 Descripti ...
- POJ-2155 Matrix---二维树状数组+区域更新单点查询
题目链接: https://vjudge.net/problem/POJ-2155 题目大意: 给一个n*n的01矩阵,然后有两种操作(m次)C x1 y1 x2 y2是把这个小矩形内所有数字异或一遍 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
- 【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二维树状数组
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一个二维树状数组
...
- POJ2155(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- poj2155二维树状数组区间更新
垃圾poj又交不上题了,也不知道自己写的对不对 /* 给定一个矩阵,初始化为0:两种操作 第一种把一块子矩阵里的值翻转:0->1,1->0 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...
随机推荐
- 前端整合MathjaxJS的配置笔记
这篇文章是我给Pinghsu主题添加数学公式功能的一个小教程,包含我大量的官方文档阅读后的实践,跟着这篇配置教程走,你可以做到给任何一个需要数学公式的站点添加支持. 教程如标题所述是针对 Mathja ...
- 阿里校招内推C++岗位编程题第一题 空格最少的字符串
给定一个字符串S和有效单词的字典D,请确定可以插入到S中的最小空格数,使得最终的字符串完全由D中的有效单词组成.并输出解. 如果没有解则应该输出n/a 例如: 输入: S = “ilikealibab ...
- LVS+Keepalive+Nginx实现负载均衡
本文参考:http://blog.csdn.net/yinwenjie/article/details/47211551 简单粗暴写一下,做备忘,刚刚搭好没做优化呢,后期补充 一.机器准备 LVS-M ...
- 如果jsp表单元素的值为空,如何避免null出现在页面上?
可以写一个简单的函数对空值进行处理,判断值是否为空,如果是空就返回空字符串.实例代码如下: <%! String blanknull(String s) { return (s == null) ...
- android在程序崩溃时Catch异常并处理
Android系统的"程序异常退出",给应用的用户体验造成不良影响.为了捕获应用运行时异常并给出友好提示,便可继承UncaughtExceptionHandler类来处理.通过Th ...
- spring学习(一)——控制反转简单例子
spring框架是一个开源的轻量级的基于IOC与AOP核心技术的容器框架,主要是解决企业的复杂操作实现. 那IOC与AOP,到底如何解释呢,在看spring视频中,两个专业术语一定必须要懂得. IOC ...
- exception = {"元数据集合中已存在具有标识“xxx”的项。\r\n参数名: item"}
vs提示:exception = {"元数据集合中已存在具有标识"xxx"的项.\r\n参数名: item"} 出现这个错误说明有重复的字段,有可能是继承的类里 ...
- vim 删除文件全部内容
很多时候我们需要删除脚本文件全部内容, 重新再写入新的内容,进行其他的操作: 很多时候我们对应用程序的排错需要查看日志文件,然而日志中通常有许多我们以前的应用程序产生的日志,其他的日志过多的时候,有时 ...
- 硬盘引导扇区、多分区图、不通硬盘的LINUX逻辑分区数量
主要启动记录区(Master Boot Record,MBR):可以安装开机管理程序的地方,有446byte 分割表(Paritition table):记录整块硬盘分割的状态,有64bytes 下面 ...
- java 堆和栈的区别
1,在栈中存放的是基本类型变量和对象的引用变量,当一段代码定义一个变量时,java 就在栈内为这个变量分配内存空间,当超过变量的作用域时,java会自动回收分配的内存. 局部变量在栈内存 2,堆内存放 ...