poj2155 树状数组 Matrix
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14826 | Accepted: 5583 |
Description
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 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
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的更多相关文章
- POJ2155 树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 26650 Accepted: 9825 Descripti ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- 【POJ2155】【二维树状数组】Matrix
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- poj----2155 Matrix(二维树状数组第二类)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 Descripti ...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- 【poj2155】【Matrix】二位树状数组
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34310873 Description Given ...
- 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 ...
- POJ2155 Matrix 【二维树状数组】+【段更新点查询】
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17766 Accepted: 6674 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
随机推荐
- UVa 12716 (GCD == XOR) GCD XOR
题意: 问整数n以内,有多少对整数a.b满足(1≤b≤a)且gcd(a, b) = xor(a, b) 分析: gcd和xor看起来风马牛不相及的运算,居然有一个比较"神奇"的结论 ...
- bzoj1412: [ZJOI2009]狼和羊的故事
空地之间开始没有连然后一直WA...题意混乱...尴尬. #include<cstdio> #include<cstring> #include<iostream> ...
- RTP封装h264
网络抽象层单元类型 (NALU): NALU头由一个字节组成,它的语法如下: +---------------+ |0|1|2|3|4|5|6|7| +-+-+-+-+-+-+-+ ...
- Mysqlbackup 备份详解(mysql官方备份工具)
A.1全库备份. 命令: mysqlbackup --defaults-file=/home/mysql-server/mysql3/my.cnf --user=root --password=ro ...
- wap版和pc版的旋转js
<script type="text/javascript"> var evt = "onorientationchange" in window ...
- js把div固定在页面的右下角
在公司做材料系统中,需要做一个总是居于右下角的div,但是因为右边这部分本就是用iframe做的,所以是不好弄的. 一开始,以为用position:fixed,一句css就可以完成,结果在iframe ...
- wifi详解(五)
1 Android平台的Wifi模块移植要点 1.1 Wifi结构 user interface Android WiFiService WPA_Supplicant DHD ...
- linux中waitpid及wait的用法
wait(等待子进程中断或结束) 表头文件 #include<sys/types.h> #include<sys/wait.h> 定义函数 pid_t wa ...
- hdu 3341(ac自动机+状态压缩)
题意:容易理解... 思路:首先一开始容易想到要用到dp,开设一个dp[41][41][41][41][501]的数组来解决,但是明显内存已经超出范围了,于是就想如何减少内存呢?只要知道A.T.C.G ...
- 【和我一起学python吧】Python 启航
话说万张高楼平地起,不会走之前先学会爬吧.尤其对于我等的小菜同学来说更是这样,不管怎么先code first吧,等我等小菜们翅膀硬了才test first吧. 1, 怎么运行python? 先到pyt ...