Matrix 二维树状数组的第二类应用
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 17976 | Accepted: 6737 |
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
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const int ms=1e3+;
int map[ms][ms];
int n,m;
int lowbit(int t)
{
return t&(-t);
}
void updata(int x,int y,int d)
{
while(x>)
{
int y1=y;
while(y1>)
{
map[x][y1]+=d;
y1-=lowbit(y1);
}
x-=lowbit(x);
}
}
int getsum(int x,int y)
{
int sum=;
while(x<=n)
{
int y1=y;
while(y1<=n)
{
sum+=map[x][y1];
y1+=lowbit(y1);
}
x+=lowbit(x);
}
return sum;
}
int main()
{
int i,j,k,t,tcase;
//#ifndef oo
//cout<<"dkkjdkk"<<endl;
//#endif
cin>>tcase;
while(tcase--)
{
cin>>n>>m;
memset(map,,sizeof(map));
char s[];//避免回车符的影响 否则 getchar()
while(m--)
{
cin>>s;
if(s[]=='C')
{
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
updata(x2,y2,);
updata(x1-,y2,-);
updata(x1-,y1-,);
updata(x2,y1-,-);
//updata(x1-1,y1-1,1);
}
else if(s[]=='Q')
{
int x,y;
cin>>x>>y;
cout<<getsum(x,y)%<<endl;
}
}
if(tcase)
cout<<endl;
}
return ;
}
Matrix 二维树状数组的第二类应用的更多相关文章
- [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 ...
- POJ 2155 Matrix(二维树状数组,绝对具体)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20599 Accepted: 7673 Descripti ...
- POJ 2155:Matrix 二维树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 21757 Accepted: 8141 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: 16950 Accepted: 6369 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 ...
- Matrix (二维树状数组)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- poj 2155 B - Matrix 二维树状数组
#include<iostream> #include<string> #include<string.h> #include<cstdio> usin ...
随机推荐
- Codeforces Round #363 (Div. 1) C. LRU
题意: n个数,长度为k的缓存,每次询问,每个数以pi的概率被选,如果不在缓存区则加入,如果缓存区满了,则第一个进缓存的出来,问10^100次询问以后每个数在缓存的概率 思路: 状压DP,看了hzwe ...
- elasticsearch配置文件解析
配置es的集群名称 : cluster.name: fcz_es
- HDU 4882 ZCC Loves Codefires (贪心)
ZCC Loves Codefires 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/B Description Though ...
- ZOJ 3911 Prime Query(线段树)
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- Linux的运行级别和chkconfig用法
Linux的运行级别和chkconfig用法 一.Linux的运行级别 在装MySQL的时候,才知道了Linux的运行级别这么一回事.汗…自己太水了…下面总结一下: 什么是运行级别呢?简 ...
- 转载LINQ系列OrderBy(), ThenBy()简介
前言 前面两篇分别介绍了 Where() 与 Select() ,这篇则是要介绍 OrderBy() 与 ThenBy() ,这几个东西看起来最像 SQL 上会用到的语法,但切记一点,这边介绍的是 L ...
- [转]Freemarker数据类型转换
转至:http://blog.sina.com.cn/s/blog_667ac0360102eaz8.html // 测试程序 package myTest; import java.io.Buffe ...
- SQL Server 2008 Data Types and Entity Framework 4
Because I’ve had a lot of conversations about spatial data types lately, I thought I would create a ...
- C#多线程(上) 分类: C# 线程 2015-03-09 10:35 174人阅读 评论(0) 收藏
一.多线程的相关概念 什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源. 而一个进程又是由多个线程所组成的. 什么是线程? 线程是程序中的一个执行 ...
- MemCacheManager
#region Fields private AreaRepository _areaRepository = new AreaRepository(); private ICacheManager ...