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 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...
随机推荐
- [转载] RCNN/SPP/FAST RCNN/FASTER RCNN/YOLO/SSD算法简介
RCNN: RCNN(Regions with CNN features)是将CNN方法应用到目标检测问题上的一个里程碑,由年轻有为的RBG大神提出,借助CNN良好的特征提取和分类性能,通过Regio ...
- http://www.yiibai.com/javalang/string_endswith.html
http://www.yiibai.com/javalang/string_endswith.html
- 五:Edits Viewer离线日志查看器
离线日志查看器可以将二进制日志翻译成可读的文件(如XML),只有当hadoop集群停止时才能使用.输入文件支持的类型:XML和二进制.输出文件支持类型:XML 二进制 Stats(标准输出?) ...
- vue.js学习之 打包为生产环境后,页面为白色
vue.js学习之 打包为生产环境后,页面为白色 一:配置问题 当我们将项目打包为生产环境后,在dist文件夹下打开index.html,会发现页面为白色. 1:打开config>index.j ...
- 《剑指Offer》题一~题十
一.赋值运算符函数 题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: CMyString(char *pData = nul ...
- Special Offer! Super Price 999 Bourles!
Description Polycarpus is an amateur businessman. Recently he was surprised to find out that the mar ...
- C语言 指针数组 多维数组
. 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21402047 . 1. 地址算数运算示例 指针算数运算 ...
- 基于TensorFlow解决手写数字识别的Softmax方法、多层卷积网络方法和前馈神经网络方法
一.基于TensorFlow的softmax回归模型解决手写字母识别问题 详细步骤如下: 1.加载MNIST数据: input_data.read_data_sets('MNIST_data',one ...
- servlet转发和重定向
1 转发 RequestDispatcher getRequestDispatcher(java.lang.String path) 1)地址栏不会改变 2)转发只能转发到当前web应用内的资源 3) ...
- 数据库索引(结合B-树和B+树)
数据库索引,是数据库管理系统中一个排序的数据结构以协助快速查询.更新数据库表中数据.索引的实现通常使用B树及其变种B+树. 在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种 ...