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

    计算 m的整数倍 不大于n #define ut_2pow_round(n, m) ((n) & ~((m) - 1)) #include <stdio.h>#include &l ...

  2. bzoj2431: [HAOI2009]逆序对数列

    dp. f[i][j]表示放置第i个数有j个逆序对的方案数. s[i][j]维护前缀和(f[i][0]~f[i][j]). 状态转移方程 f[i][j]=s[i-1][j]-s[i-1][max(j- ...

  3. js学习总结

    转自 http://blog.sina.com.cn/s/blog_75cf5f3201011csu.html 一: 关于基本数据类型在栈内存和堆内存中的关系 基本数据对于栈内存和堆内存是可以复制的, ...

  4. django - 替换admin的textarea为 富文本

    1. 安装这个:https://github.com/pydanny/django-wysiwyg 2. 下载你希望的 编辑器,到你的指定路径STATIC_ROOT [详细后面补充]

  5. P2P直播、点播技术学习经验

    自8月份以来一直埋头学习P2P在音/视频直播.点播上的学习,受到不少网友的帮助,在此也留下自己学到的一点点的经验. 第一个接触的开源项目是peercast,应该说上手非常快,这必须感谢王浩聪的注释版, ...

  6. java 读取TXT文件的方法

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  7. 【LeetCode 209】Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  8. ARM处理机模式--内部寄存器

    处理器模式 用户模式(user)简称usr 快速中断模式(FIQ)简称fiq 外部中断模式(IRQ)简称irq 特权模式(supervisor)简称sve 数据访问终止模式(abort)简称abt 未 ...

  9. redis集群的搭建

    1.首先下载好软件包 #cd /opt/tzr/ #wget http://redis.googlecode.com/files/redis-2.6.11.tar.gz #mkdir /opt/tzr ...

  10. 《GettingThingsDone》--GTD学习笔记(二)-GTD实践指导

    一.准备阶段的建议: 高级的工作管理方法就是学会一套系统并付诸实施,直到将这套系统和方法融入你的工作和生活中. 通过行动使自己感觉良好,要比通过使自己感觉良好而进入一种较佳的行动状态容易的多. 当你在 ...