Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 22058   Accepted: 8219

Description

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
二维树状数组,跟一维的差不多,
这个道题的思路就是看看x1,y1往上加一,同时方块右边,下面和右下方的区域再加1,只要保证他们那边加个偶数就可以了。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = + ;
int c[MAX][MAX];
int n;
int lowbit(int k)
{
return k & (-k);
}
void update(int x,int y,int num)
{
for(int i = x; i < n; i += lowbit(i))
{
for(int j = y; j < n; j += lowbit(j))
c[i][j] += num;
}
}
int sum(int x,int y)
{
int s = ;
for(int i = x; i > ; i -= lowbit(i))
{
for(int j = y; j > ; j -= lowbit(j))
s += c[i][j];
}
return s;
}
int main()
{
int t,q;
int num = ;
scanf("%d", &t);
while(t--)
{
if(num ++)
printf("\n");
scanf("%d%d", &n,&q);
memset(c,,sizeof(c));
char ch;
int x1,y1,x2,y2;
getchar();
while(q--)
{
scanf("%c", &ch);
if(ch == 'Q')
{
scanf("%d%d", &x1,&y1);
getchar();
int m = sum(x2,y2) - sum(x1 - , y2) - sum(x2,y1 - ) + sum(x1-,y1-);
if(m % == )
printf("0\n");
else
printf("1\n");
}
else if(ch == 'C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
getchar();
update(x1,y1,);
}
}
}
return ;
}

POJMatrix(二维树状数组)的更多相关文章

  1. 二维树状数组 BZOJ 1452 [JSOI2009]Count

    题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...

  2. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

  3. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  4. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

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

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

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

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

  7. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

  8. [POJ2155]Matrix(二维树状数组)

    题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...

  9. MooFest_二维树状数组

    Description Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a s ...

随机推荐

  1. 教你写一个web远程控制小工具

    惯例先上图 晚上躺床上了,发现忘关电脑了,又不想起来关,来用手机控制电脑多好,百度了下,果然一大把.哈,我自己为什么不自己也实现个呢,任意的自己diy.Just do it. 如果不想看如何实现,那么 ...

  2. HTML5+JS 《五子飞》游戏实现(八)人机对战

    要想实现人机对战,就必须让电脑自动下棋,而且要知道自动去查找对方的棋子,看看有没有可以挑一对的,有没有可以夹一个的,这样下起来才有意思. 当电脑用户下完棋后,电脑应立即搜索用户的棋子,然后如果没有被吃 ...

  3. HTML5之创新的视频拼图剖析式学习之二

    昨天我们剖析了一下翻阅体验的实现.今天要剖析另外一个很有意思的效果——视频拼图. 网站中第一部分第二页<月熊的标志>是月熊志中互动性较强的一页,页面上会随机分布9块视频碎片,用户可以通过鼠 ...

  4. Ubuntu更改右键菜单

    方法/步骤1.这是我们在桌面文件夹ubuntugege上打开的右键菜单,你说你在~/.gnome2/nautilus-scripts/添加的右键菜单项目但它就是没有显示呀,于是你觉得Ubuntu 12 ...

  5. C++命名规范

    1.1    类型名 首字母大写,末尾加_T.如: class TnppCoverageArea_T{…}; 1.2    1.2 变量和函数名 变量和函数名中首字母小写,其后每个英文单词的第一个字母 ...

  6. 1020理解MySQL——索引与优化

    转自http://www.cnblogs.com/hustcat/archive/2009/10/28/1591648.html 写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性 ...

  7. linux安装软件的学习

    Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动下载 ...

  8. ssh全屏退出的办法

    在使用ssh的时候遇到,不知道碰到哪里了,突然xshell就全屏了 解决方法 按下键盘 Alt+Enter就好了

  9. 【hihoCoder 1036】Trie图

    看了一下简单的$Trie图$,调模板调啊调一连调了$2h$,最后发现$-'a'$打成$-'A'$了hhh,有种摔键盘的冲动. $Trie图$是$Trie树$上建立“前缀边”,不用再像在$Trie树$上 ...

  10. 实现socket非阻塞设置

    刚开始学习,难免有些疏漏之处,请各位多多指教. 程序亲测可以使用.废话不多说. 网络IO模型分为阻塞IO.非阻塞IO.异步IO.IO复用.信号驱动IO. 阻塞IO: 有数据时,直接读:没有数据时,等待 ...