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

题意:

在一个N*N的矩阵里(左上是(1,1)),初始点的值都为0,C(x1,y1,x2,y2)表示将这个矩阵的点都异或,Q(x,y)表示查询点的值(0或者1)。

思路:

常见的二维树状数组是单点更新,区间查询; 而这里是区间更新,单点查询。

由于是单点查询,这里直接用差分的思想做的:a[i][j]表示坐标(i,j)到(n,m)增加多少。

如果矩形(x1,y1,x2,y2)加一,则a[x1][x2]+1;a[x1][y2+1]-1;a[x2][y1+1]-1,a[x2][y2]+1;那么所求点(i,j)的值就是前缀和。

(但如果是区间更新,区间查询,则要像上一题那样推公式,最后得到5个一维树状数组。上一题是3个一维树状数组。)

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,a[][];
int lowbit(int x){return x&(-x);}
void add(int x,int y,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
a[i][j]+=val;
}
int query(int x,int y)
{
int res=;
for(int i=x;i;i-=lowbit(i))
for(int j=y;j;j-=lowbit(j))
res+=a[i][j];
return res;
}
int main()
{
int T,x1,x2,y1,y2;char opt[];scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(a,,sizeof(a));
for(int i=;i<=m;i++){
scanf("%s",opt);
if(opt[]=='Q'){
scanf("%d%d",&x1,&y1);
printf("%d\n",&(query(x1,y1)));
}
else {
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
add(x1,y1,);add(x2+,y2+,);
add(x2+,y1,-);add(x1,y2+,-);
}
}
if(T) printf("\n");
} return ;
}

POJ2155 Matrix(二维树状数组||区间修改单点查询)的更多相关文章

  1. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  2. 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询

    题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...

  3. 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询

    题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...

  4. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  5. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  6. 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

    http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...

  7. poj----2155 Matrix(二维树状数组第二类)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16950   Accepted: 6369 Descripti ...

  8. 【树状数组区间修改单点查询】HDU 4031 Attack

    http://acm.hdu.edu.cn/showproblem.php?pid=4031 [题意] 有一个长为n的长城,进行q次操作,d为防护罩的冷却时间,Attack表示区间a-b的墙将在1秒后 ...

  9. POJ2155:Matrix(二维树状数组,经典)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

随机推荐

  1. POJ 3373 Changing Digits

    题目大意: 给出一个数n,求m,使得m的长度和n相等.能被k整除.有多个数符合条件输出与n在每位数字上改变次数最小的.改变次数同样的输出大小最小的.  共同拥有两种解法:DP解法,记忆化搜索的算法. ...

  2. ThinkPHP中的模型命名

    当我们创建一个UserModel类的时候,其实已经遵循了系统的约定.ThinkPHP要求数据库的表名和模型类的命名遵循一定的规范,首先数据库的表名和字段全部采用小写形式,模型类的命名规则是除去表前缀的 ...

  3. java ConcurrentHashMap 初识

    “ConcurrentHashMap是一个线程安全的哈希表“,但是不允许key和value为空: HashTable和ConcurrentHashMap都是线程安全的,但是HashTable是同步容器 ...

  4. C语言日期计算器

    记录下码子 # define _CRT_SECURE_NO_WARNINGS # include <stdio.h> # include <stdlib.h> int days ...

  5. HTML元素定位

    一切皆为框 div.h1 或 p 元素常常被称为块级元素(block element).这意味着这些元素显示为一块内容,即"块框".与之相反,span 和 strong 等元素称为 ...

  6. Linux XMind

    XMind这个软件好像不错的样子,至少在Windows/Linux/Mac下都可以工作,作为FreeMind的替代品应该是没什么问题(还有一个vym貌似也可以,可能没有XMind好,毕竟XMind有公 ...

  7. 四、Silverlight中使用MVVM(四)——演练

    本来打算用MVVM实现CRUD操作的,这方面例子网上资源还挺多的,毕竟CRUD算是基本功了,因为最近已经开始学习Cailburn框架了,感觉时间 挺紧的,这篇就实现其中的更新操作吧. 功能很明确,当我 ...

  8. 【问】Windows下C++局部变量在内存中的分布问题

    原本是为了看看C++对象模型中子对象赋值给一个父对象和父类型指针指向的域时,到底会不会切割,就打开codebloks写了下面的代码,编译器选的是GNU. #define DEBUG(X) std::c ...

  9. Redis单台的安装部署及集群部署

    Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(diff ...

  10. 摩根大通银行被黑客攻克, ATM机/网银危在旦夕,winxp退市灾难来临了

    winxp4月退市到如今还不到半年,就出现故障了 7600多万个消费者银行账户被黑.此外还有700万个小企业账户的信息也被黑客窃取,这个算不算灾难呢?假设等到银行业彻底崩溃,资金彻底丧失,那不仅仅是灾 ...