[poj 2155]Matrix [2D BIT][二维线段树]
来源:互联网 发布:农村淘宝村小二登录 编辑:程序博客网 时间:2024/04/29 04:03
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].
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.
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.
There is a blank line between every two continuous test cases.
Sample Input
12 10C 2 1 2 2Q 2 2C 2 1 2 1Q 1 1C 1 1 2 1C 1 2 1 2C 1 1 2 2Q 1 1C 1 1 2 1Q 2 1
Sample Output
1001
Source
POJ Monthly,Lou Tiancheng
#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<cctype>using namespace std;int T,n,m;char op[2];inline void read(int& x){char c = getchar();x = 0;while(!isdigit(c))c=getchar();while(isdigit(c)){x=x*10+c-'0';c=getchar();}}#define maxn 1005#define lowbit(x) (x&(-x))int c[maxn][maxn];inline void init(){memset(c,0,sizeof(c));}//其实直接加也可以不用弄成+1,-1(%2等价) inline void add(int x,int y,int d){for(int i=x;i<=n;i+=lowbit(i))for(int j=y;j<=n;j+=lowbit(j))c[i][j]+=d;}inline int sum(int x,int y){int ret = 0;for(int i=x;i>0;i-=lowbit(i))for(int j=y;j>0;j-=lowbit(j))ret+=c[i][j];return ret;}inline void update(int x1,int y1,int x2,int y2){add(x1-1,y1-1,1);add(x1-1,y2,-1);add(x2,y1-1,-1);add(x2,y2,1);}int main(void){read(T);int x1,y1,x2,y2;while(T--){read(n),read(m);init();for(int i=1;i<=m;i++){scanf("%s",op);if(*op=='C'){read(x1),read(y1),read(x2),read(y2);update(x1+1,y1+1,x2+1,y2+1);}else{read(x1),read(y1);printf("%d\n",sum(x1,y1)%2);}}puts("");}return 0;}
下面补个二维线段树
#include <stdio.h>#include <string.h>#define xlson kx<<1, xl, mid#define xrson kx<<1|1, mid+1, xr#define ylson ky<<1, yl, mid#define yrson ky<<1|1, mid+1, yr#define MAXN 1005#define mem(a) memset(a, 0, sizeof(a))bool tree[MAXN<<2][MAXN<<2];int X, N, T;int num, X1, X2, Y1, Y2;char ch;void editY(int kx,int ky,int yl,int yr){ if(Y1<=yl && yr<=Y2) { tree[kx][ky] = !tree[kx][ky]; return ; } int mid = (yl+yr)>>1; if(Y1 <= mid) editY(kx,ylson); if(Y2 > mid) editY(kx,yrson);}void editX(int kx,int xl,int xr){ if(X1<=xl && xr<=X2) { editY(kx,1,1,N); return ; } int mid = (xl+xr)>>1; if(X1 <= mid) editX(xlson); if(X2 > mid) editX(xrson);}void queryY(int kx,int ky,int yl,int yr){ if(tree[kx][ky]) num ++; if(yl==yr) return ; int mid = (yl+yr)>>1; if(Y1 <= mid) queryY(kx,ylson); else queryY(kx,yrson);}void queryX(int kx,int xl,int xr){ queryY(kx,1,1,N); if(xl==xr) return ; int mid = (xl+xr)>>1; if(X1 <= mid)queryX(xlson); else queryX(xrson);}int main(){ while(~scanf("%d", &X))while(X--) { mem(tree); scanf("%d %d%*c", &N,&T); for(int i=0;i<T;i++) { scanf("%c %d %d%*c",&ch,&X1,&Y1); if(ch == 'C') { scanf("%d %d%*c", &X2, &Y2); editX(1,1,N); } else { num = 0; queryX(1,1,N); if(num & 1)printf("1\n"); else printf("0\n"); } } if(X) printf("\n"); } return 0;}
1 0
- [poj 2155]Matrix [2D BIT][二维线段树]
- 二维线段树 POJ 2155 Matrix
- POJ 2155 Matrix 二维线段树
- POJ 2155 Matrix【二维线段树】
- POJ 2155 Matrix (二维线段树)
- POJ 2155 Matrix 二维线段树
- POJ 2155 Matrix (二维线段树)
- poj 2155 Matrix 【二维线段树】
- POJ 2155 Matrix(二维线段树)
- 二维线段树(Matrix,poj 2155)
- POJ 2155 Matrix (二维线段树)
- POJ 2155 Matrix 二维线段树
- POJ 2155 Matrix (二维线段树)
- POJ-2155-Matrix(二维树状数组 & 二维线段树)
- [poj 2155] Matrix(二维zkw线段树)
- POJ 2155 Matrix 二维线段树 区间修改 单点查询
- POJ 2155 Matrix 二维线段树+标记永久化?
- POJ 2155 Matrix 【二维线段树模板题】
- 请写出制作一个4M的ramdisk的步骤
- 运营
- 中缀表达式转前缀表达式的简便运算
- maven私服(nexus)搭建
- 微信支付Native扫码支付模式二之CodeIgniter集成篇
- [poj 2155]Matrix [2D BIT][二维线段树]
- jquery 滚动条 平滑滚动到顶部、底部、置顶位置
- 贝叶斯法则,先验概率,后验概率,最大后验概率
- Linux 初始化 init 系统(Systemd)
- HDU 2059 龟兔赛跑(dp)
- 如何实现listView中checkbox的全选与反选功能
- OgnlValueStack的结构和几个方法分析
- Zookeeper实现简单的分布式RPC框架
- Kali设置快捷键