Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 14826   Accepted: 5583

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
树状数组好强大,在这一题中,说是原始都是一些0 1,通过操作可以使1变0 0变1,在这一题中,我们可以发现,是一个区间的更新,然后是,求得一个点的值,这和我们一般用法刚好相反,其实,我们可以转换角度,其实,如果,是从最上向下更新,然后从下到上求和,这样,我们不就把一个点的值,转化成了求一个区间的值了么?也就基于这样的思想,我们在实际的用法中,要注意把向下的时候,+1然后,在重叠处-1,画画图就知道了!说也说不清楚!很好的题啊!原本是想弄线段树的,但是有的复杂,还有可以暴内存!
#include<iostream>
#include <string.h>
#include<stdio.h>
using namespace std;
#define MAXN 1005
int n;
int matrix[MAXN][MAXN];
int lowbit(int x)
{
return x&(-x);
}
int change(int x,int y,int val)//从上到下更新
{
int i,j;
for(i=x;i<=n;i=i+lowbit(i))
for(j=y;j<=n;j=j+lowbit(j))
{
matrix[i][j]+=val;
}
return 0;
}
int getsum(int x,int y)//从下向上求和
{
int i,j,re=0;
for(i=x;i>0;i=i-lowbit(i))
for(j=y;j>0;j=j-lowbit(j))
{
re+=matrix[i][j];
}
return re;
}
int main ()
{
int tcase,ask,x1,x2,y1,y2;
char c;
scanf("%d",&tcase);
while(tcase--)
{
memset(matrix,0,sizeof(matrix));
scanf("%d%d",&n,&ask);
getchar();
while(ask--)
{
c=getchar();
if(c=='C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x2++;y2++;
change(x1,y1,1);
change(x2,y2,1);
change(x1,y2,-1);
change(x2,y1,-1);
}
else
{
scanf("%d%d",&x1,&y1);
printf("%d\n",1&getsum(x1,y1));//判定奇偶
}
getchar();
}
printf("\n"); }
return 0;
}
												

poj2155 树状数组 Matrix的更多相关文章

  1. POJ2155 树状数组

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 26650   Accepted: 9825 Descripti ...

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

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

  3. 【POJ2155】【二维树状数组】Matrix

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

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

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

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

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

  6. 【poj2155】【Matrix】二位树状数组

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34310873 Description Given ...

  7. POJ2155 Matrix(二维树状数组||区间修改单点查询)

    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 ...

  8. POJ2155 Matrix 【二维树状数组】+【段更新点查询】

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17766   Accepted: 6674 Descripti ...

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

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

随机推荐

  1. UVa 1646 (递推 JAVA大数) Edge Case

    题意: 有n个点围成一圈,这n个点的匹配就是没有公共点的边集(这些边只能连接一圈中相邻的两点),求所有匹配的个数. 额,我不会分析..=_=|| 算了几个数,找找规律发现它满足斐波那契数列的递推关系, ...

  2. 使用Java VisualVM监控远程JVM

    我们经常需要对我们的开发的软件做各种测试, 软件对系统资源的使用情况更是不可少, 目前有多个监控工具, 相比JProfiler对系统资源尤其是内存的消耗是非常庞大,JDK1.6开始自带的VisualV ...

  3. Java [Leetcode 66]Plus One

    题目描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...

  4. linux + ffmpeg + eclipse 调试

    使用linux + ffmpeg + eclipse调试步骤OS : ubuntu 12.04Eclipse : 3.7.2 为Eclipse安装cdt插件,使其支持c/c++ 导入ffmpeg项目 ...

  5. Oracle 闪回特性(FLASHBACK DATABASE)

    --===================================== -- Oracle 闪回特性(FLASHBACK DATABASE) --======================= ...

  6. squid+nginx+apache

    一.前言 二.编译安装 三.安装MySQL.memcache 四.安装Apache.PHP.eAccelerator.php-memcache 五.安装Squid 六.后记 一.前言,准备工作当前,L ...

  7. 【转】linux中wait与waitpid的差别

    原文网址:http://blog.163.com/libo_5/blog/static/15696852010324287748/ zombie不占用内存也不占用CPU,表面上我们可以不用在乎它们的存 ...

  8. Tour

    题意: 给n个点的坐标,求形成的最短的闭合回路. 分析: 经典问题,dp[i][j]表示有1-i点再由j回到1点的最短距离,i点有两种情况,在去的路径上 dp[i][j]=min(dp[i][j],d ...

  9. HDU-4632 http://acm.hdu.edu.cn/showproblem.php?pid=4632

    http://acm.hdu.edu.cn/showproblem.php?pid=4632 题意: 一个字符串,有多少个subsequence是回文串. 别人的题解: 用dp[i][j]表示这一段里 ...

  10. Authentication with SignalR and OAuth Bearer Token

    Authentication with SignalR and OAuth Bearer Token Authenticating connections to SignalR is not as e ...