POJ 2155 Matrix (二维树状数组)

题目地址:POJ 2155
二维数组裸题。
对于更新操作,分别对(x1,y1)和(x2+1,y2+1)加1,对(x2+1,y1)和(x1,y2+1)减1.然后对于询问操作,求(0,0)至(x2,y2)的和。
代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
#include <string>
#include <time.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
//#pragma comment(linker, "/STACK:1024000000")
//const int mod=9901;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=1000+10;
int c[MAXN][MAXN], n;
int lowbit(int x)
{
        return x&(-x);
}
void add(int x, int y, int val)
{
        int i=x, j;
        while(i<=n){
                j=y;
                while(j<=n){
                        c[i][j]+=val;
                        j+=lowbit(j);
                }
                i+=lowbit(i);
        }
}
int Query(int x, int y)
{
        int ans=0;
        int i=x, j;
        while(i>0){
                j=y;
                while(j>0){
                        ans+=c[i][j];
                        j-=lowbit(j);
                }
                i-=lowbit(i);
        }
        return ans;
}
int main()
{
        int t, q, i, j, x1, y1, x2, y2;
        scanf("%d",&t);
        char s[3];
        while(t--){
                scanf("%d%d",&n,&q);
                memset(c,0,sizeof(c));
                while(q--){
                        scanf("%s",s);
                        if(s[0]=='C'){
                                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                                add(x1,y1,1);
                                add(x2+1,y1,-1);
                                add(x1,y2+1,-1);
                                add(x2+1,y2+1,1);
                        }
                        else{
                                scanf("%d%d",&x1,&y1);
                                printf("%d\n",Query(x1,y1)%2);
                        }
                }
                if(t) puts("");
        }
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值