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 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...
随机推荐
- 正式放弃Edge,重新拥抱Chrome
从Edge还叫斯巴达的时候我就开始用了,本来对浏览器的要求也没多高,能够打开多个选项卡,稳定,支持最新的规范就好了. 但是Edge真的是越来越让我失望了,卡死问题越来越多,崩溃越来越频繁,我也快奔溃了 ...
- 5.安装hbase
下载安装包并解压设置hbase环境变量配置hbase-site.xml启动hbase检测hbase启动情况测试hbase shell 下载安装包并解压 https://mirrors.tuna.tsi ...
- JavaScript初探系列之基本概念
JavaScript的核心语言特性在ECMA-262中是以名为ECMAScript(ECMA, EuropeanComputer Manufacturers Association )的伪语言的形式来 ...
- scrapy(2)——scrapy爬取新浪微博(单机版)
Sina爬虫教程 Scrapy环境搭建 环境:window10 + python2.7(包含scrapy)+ mongoDB 1.1 安装集成了python2.7的anaconda ana ...
- NeoLoad系列- 快速上手教程
1.新建工程 2.点击录制脚本按钮 3.在弹出的开始录制对话框中,填写虚拟用户信息. Record in下拉框,用来填写用户路径,一般有三个容器组成: Init, Actions, and End.当 ...
- Qt快速入门学习笔记(画图篇)
1.Qt中提供了强大的2D绘图系统,可以使用相同的API在屏幕和绘图设备上进行绘制,它主要基于QPainter.QPaintDevice和QPaintEngine这三个类.其中QPainter用来执行 ...
- 【bzoj3932】[CQOI2015]任务查询系统 离散化+主席树
题目描述 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第Ei ...
- Python:树的遍历
各种遍历顺序如下图所示: 树的最大深度 # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = ...
- 【刷题】SPOJ 1812 LCS2 - Longest Common Substring II
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- 【POJ2976】Dropping Tests(分数规划)
[POJ2976]Dropping Tests(分数规划) 题面 Vjudge 翻译在\(Vjudge\)上有(而且很皮) 题解 简单的\(01\)分数规划 需要我们做的是最大化\(\frac{\su ...