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. JavaScript精要

    写在开篇之前 这个系列都文章算是我最近研究了JavaScript(以后简称js)大半个月的一点心得吧.记得以前看过罗小平的一本书叫<Delphi精要>,我也就姑且起名叫<JavaSc ...

  2. JavaScript UI选型及Jquery EasyUI使用经验谈

    最近由于项目需要,对js UI作了一些简单的了解和使用,有自己的一些想法,在这里留个记录. 当然,我的专注点在管理系统的范围内,所以互联网网站及其他形态的应用这里不提及,所以jQuery UI和Boo ...

  3. [BZOJ3211]花神游历各国(线段树+区间开根)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3211 分析: 区间开根是没法区间合并的. 但是注意到10^9开根开个5次就变成1了…… ...

  4. Beta版本冲刺Day5

    会议讨论: 628:配置java环境已经成功,Tomcat部署也成功了,Mysql还未进行配置.601:继续修改其他的页面外观. 528:继续完成其他的功能页面. 340:对一些页面进行了优化美观,并 ...

  5. elasticsearch与mongodb分布式集群环境下数据同步

    1.ElasticSearch是什么 ElasticSearch 是一个基于Lucene构建的开源.分布式,RESTful搜索引擎.它的服务是为具有数据库和Web前端的应用程序提供附加的组件(即可搜索 ...

  6. 天朝git的使用

    开源中国社区 官方网站 https://git.oschina.net/ 开源中国社区成立于2008年8月,其目的是为中国的IT技术人员提供一个全面的.快捷更新的用来检索开源软件以及交流使用开源经验的 ...

  7. Java Decompiler 反编译工具下载地址及JD-Eclipse设置菜单翻译

    官网地址:http://jd.benow.ca/ JD-GUI:jd-gui-0.3.6.windows.zip JD-Eclipse:jd-eclipse-site-1.0.0-RC2.zip 菜单 ...

  8. poj2762 缩点+topo排序

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16486 ...

  9. 解决虚拟机 MAC10.9 不能设置1366*768分辨率问题

    首先在虚拟机设置里,显示--3d下面--固定分辨率--最大设成1024*768--保存然后打开虚拟机的.vmx文件,最后几行 svga.autodetect = "FALSE"sv ...

  10. OPENGGL深度测试

    深度测试是为了解决那些在绘图过程中本应该被隐藏的面结果却出现了,例如: 绘图代码中先绘制了一个一个近处的立方体,后绘制了一个远处的立方体,结果在绘制过程中,远处的立方体总是在近处的立方体后绘制,所以在 ...