POJ 2155 Matrix(二维树状数组)

来源:互联网 发布:电子罗盘 数据 使用 编辑:程序博客网 时间:2024/05/17 05:12

题目链接:http://poj.org/problem?id=2155

这个题目和上面一题一样,不过这个还简单点

先理解一维的再来理解二维的就好多了

看算法合集之《浅谈信息学竞赛中的“0”和“1”》上面讲的比较清楚

#include <string.h>#include <algorithm>#include <iostream>#include <stdio.h>using namespace std;const int maxn = 1005;int tree[maxn][maxn];int n,m;char str[10];int lowbit(int x){    return (-x)&x;}int sum(int i,int j){    int ans=0,now;    while(i>0){        now=j;        while(now>0){            ans+=tree[i][now];            now-=lowbit(now);        }        i-=lowbit(i);    }    return ans;}int update(int i,int j){    int now;    while(i<=n){        now=j;        while(now<=n){            tree[i][now]++;            now+=lowbit(now);        }        i+=lowbit(i);    }    return 0;}int main(){    int i,j,k,t,x1,y1,x2,y2,x11,y11,x22,y22;    scanf("%d",&t);    while(t--){        memset(tree,0,sizeof(tree));        scanf("%d%d",&n,&m);        for(i=0;i<m;i++){            scanf("%s",str);            if(str[0]=='C'){                scanf("%d%d%d%d",&x11,&y11,&x22,&y22);                x1=min(x11,x22);                x2=max(x11,x22);                y1=min(y11,y22);                y2=max(y11,y22);                update(x1,y1);                update(x1,y2+1);                update(x2+1,y1);                update(x2+1,y2+1);            }else{                scanf("%d%d",&x1,&y1);                printf("%d\n",sum(x1,y1)%2);            }        }        printf("\n");    }    return 0;}


原创粉丝点击