poj2155
来源:互联网 发布:h5投票系统源码 编辑:程序博客网 时间:2024/05/21 13:22
poj2155
题意是给一个01矩阵,有两种操作,一种是把左上角x1,y1到右下角x2,y2这个子矩阵中的01翻转,一种是查询某个点x,y的值。
楼教主的题目,很经典的树套树。翻转实际上就是把这个区间异或1,也可以看成模2意义下加1。然后就是树套树的思想对每一行建立一个树状数组,每一列也建一个。每次更新,我们可以在x1行对[ y1,y2 ]进行区间+1更新,在x2+1行对[y1,y2]进行-1更新。查询的时候我们把[1,x]行里[1,y]列求和即可。
#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<set>#include<map>#include<time.h>#include<cstdio>#include<vector>#include<list>#include<stack>#include<queue>#include<iostream>#include<stdlib.h>using namespace std;#define LONG long longconst LONG INF=0x3f3f3f3f;const LONG MOD= 97 ;const double PI=acos(-1.0);#define clrI(x) memset(x,-1,sizeof(x))#define clr0(x) memset(x,0,sizeof x)#define clr1(x) memset(x,INF,sizeof x)#define clr2(x) memset(x,-INF,sizeof x)#define EPS 1e-10#define lowbit(x) (x&-x)int tree[1100][1100] ;int n ;void update(int x , int y ,int val){ while(y <= n ) { tree[x][y] += val ; tree[x][y] &= 1; y += lowbit(y) ; }}void UPD(int x ,int y1 ,int y2 , int val ){ while(x <= n) { update(x , y1 , val ); update(x , y2 + 1 , -val ) ; x += lowbit(x) ; }}int que(int x ,int y ){ int res =0 ; while(y > 0) { res += tree[ x ][y] ; res &= 1; y -= lowbit(y ) ; } return (res&1 );}int QUE(int x , int y){ int res = 0 ; while(x > 0) { res += que(x , y) ; res&= 1; x -= lowbit( x ) ; } return res ;}int main (){ int T; cin >> T ; while(T -- ) { clr0(tree) ; int m ; scanf("%d%d",&n,&m) ; char op[5] ; int x1 , x2 , y1 , y2 ; while( m -- ) { scanf("%s",op) ; if(op[0] =='C') { scanf("%d%d%d%d",&x1,&y1,&x2,&y2) ; UPD(x1 ,y1 , y2 , 1) ; UPD(x2+1 , y1 , y2 , -1) ; } else { scanf("%d%d",&x1,&y1) ; printf("%d\n",(QUE(x1 , y1) &1 ) ) ; } } if( T ) printf("\n") ; } return 0 ;}
阅读全文
1 0
- POJ2155
- poj2155
- poj2155
- poj2155
- POJ2155
- poj2155
- poj2155
- poj2155
- POJ2155
- poj2155
- POJ2155
- poj2155
- POJ2155 Matrix
- POJ2155:Matrix
- poj2155 Matrix
- poj2155 Matrix
- poj2155 Matrix
- POJ2155 Matrix
- NYOJ289
- 把python基本功搞扎实(3)
- Web应用程序的最佳实践
- 剑指Offer------从尾到头打印链表
- SolrJ 操作索引文档
- poj2155
- GAP
- UVA 532 Dungeon Master
- C++服务器开发资源收集
- 字符串p型编码
- okHttp基本使用
- Spring Batch 之 Spring Batch 简介
- 【HDU
- JAVA内存模型