POJ 2155: Matrix
来源:互联网 发布:合力叉车 知乎 编辑:程序博客网 时间:2024/05/10 23:49
题目链接:http://poj.org/problem?id=2155
题目大意:
在一个n*m的0-1矩阵上执行如下两个操作:
1)把某个子矩阵中的元素取反
2)查询某个元素的值
算法:
二维树状数组。
把[x1,x2]*[y1,y2]的矩阵取反,
相当于把点(x1,y1),(x2+1,y1),(x1,y2+1),(x2+1,y2+1)到点(n,n)之间的矩形都分别取反一次。
那么接下来就简单地将区间更新、单点查询转化为单点更新、区间查询就可以了。
代码如下:
#include<cstdio>#include<iostream>#include<algorithm>#include<sstream>#include<cstdlib>#include<cstring>#include<string>#include<climits>#include<queue>#include<vector>#include<stack>#include<set>#include<map>#define INF 0x3f3f3f3f#define eps 1e-8using namespace std;const int MAXN=1100;int tr[MAXN][MAXN];int n,m;void add(int x, int y){ for(int i=x; i<=n; i+=i&(-i)) { for(int j=y; j<=n; j+=j&(-j)) { tr[i][j]^=1; } }}int sum(int x, int y){ int ret=0; for(int i=x; i; i-=i&(-i)) { for(int j=y; j; j-=j&(-j)) { ret^=tr[i][j]; } } return ret;}int main() { int cas; scanf("%d",&cas); while(cas--) { scanf("%d%d",&n,&m); memset(tr,0,sizeof(tr)); while(m--) { char s[100]; scanf("%s",s); if(s[0]=='C') { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); add(x1,y1); add(x2+1,y1); add(x1,y2+1); add(x2+1,y2+1); } else { int x,y; scanf("%d%d",&x,&y); printf("%d\n",sum(x,y)); } } puts(""); } return 0;}
- poj 2155 Matrix
- poj 2155 Matrix
- poj 2155 Matrix
- Poj 2155 Matrix
- poj 2155 Matrix
- POJ 2155 Matrix
- POJ 2155 - Matrix
- poj 2155 Matrix
- POJ 2155 Matrix
- Poj 2155 Matrix
- POJ-2155-Matrix
- POJ 2155: Matrix
- poj 2155 Matrix
- POJ 2155 Matrix
- poj 2155 Matrix
- POJ 2155 Matrix
- POJ 2155 Matrix
- poj 2155 Matrix
- cocos2d-x删除vs2010项目模板 卸载
- 强制转换和数组下标的妙用
- ACM几何问题基础知识讲解(附代码)
- Java集合详解
- EnvironmentError: mysql_config not found
- POJ 2155: Matrix
- UITableViewCell样式设置
- error: command 'gcc' failed with exit status 1
- java第六课课堂笔记(抽象类)
- Qt Designer小观与qmake设置与使用
- 闲论排样/排料算法(nesting)
- 排料方面的比较有名的论文
- 矩形排料算法
- poj 1661 Help Jimmy