poj 2155 Matrix 二维树状数组
来源:互联网 发布:虚拟拍照是什么软件 编辑:程序博客网 时间:2024/06/05 01:52
题目链接:
http://poj.org/problem?id=2155
题意:
给出N*N的矩阵,初始为0。
给出矩阵左上角和右下角坐标,这个面积范围里的元素 1变0 ,0 变1,然后给出询问,问某个点是多少。
题解:
http://blog.csdn.net/zxy_snow/article/details/6264135
我们将1变0,0变1转变为将这一块面积翻转几次,奇数次就是1,偶数次就是0。
二维数组维护面积,加加减减。。
add(x2,y2);
add(x1-1,y2);
add(x2,y1-1);
add(x1-1,y1-1);
看图:虽然非常丑,对于翻转一块面积,先翻转整体,减掉多加的,加上多减的,图上的数字代表这块面积翻转了几次,偶数次代表没有改变嘛。
代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;typedef long long ll;#define MS(a) memset(a,0,sizeof(a))#define MP make_pair#define PB push_backconst int INF = 0x3f3f3f3f;const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;inline ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}//////////////////////////////////////////////////////////////////////////const int maxn = 1e3+10;int bit[maxn][maxn];void add(int x,int y){ for(int i=x; i<maxn; i+=i&-i) for(int j=y; j<maxn; j+=j&-j) bit[i][j]++;}int sum(int x,int y){ int res = 0; for(int i=x; i>0; i-=i&-i) for(int j=y; j>0; j-=j&-j) res += bit[i][j]; return res;}int main(){ int T = read(); while(T--){ MS(bit); int n,k; cin >> n >> k; for(int i=0; i<k; i++){ char op; scanf(" %c",&op); if(op == 'C'){ int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); x1++,y1++,x2++,y2++; add(x2,y2); add(x1-1,y1-1); add(x1-1,y2); add(x2,y1-1); }else { int x,y; scanf("%d%d",&x,&y); cout << sum(x,y)%2 << endl; } } puts(""); } return 0;}
0 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(二维树状数组)
- Matrix - POJ 2155 二维树状数组
- POJ 2155 Matrix 二维树状数组
- POJ 2155 Matrix 【二维树状数组】
- POJ 2155 Matrix (二维树状数组)
- poj 2155 Matrix (二维树状数组)
- 第三十八篇:CAKeyframeAnimation--+CAShapeLayer+UIBezierPath用法,关键帧动画
- 王爽《汇编语言》实验10:编写子程序show_str
- Java学习笔记170324
- C语言3-23
- 计161_Problem Z: 切面条
- poj 2155 Matrix 二维树状数组
- 求最大公约数:欧几里德算法(即 辗转相除法 )
- (二)数据链路层
- SSL 1612——最优布线问题(最小生成树)
- codeforces 791D 树形dp 所有点对
- 小波去燥原理分析
- “100台机器,拟分四个周期使用,在每一周期有两种生产任务”问题
- Python3爬虫之入门和正则表达式
- [BZOJ4596][Shoi2016]黑暗前的幻想乡(容斥原理+矩阵树定理)