poj 2155 二维树状数组

//现在知道的树状数组有两种写法,一个适用擦边查点,一种适用擦点查边
//擦点查边的写法比较普通,arr[i] 存的就是i管辖范围内的sum
//擦边查点反过来写,此时,插入t到[a,b]相当于插入-t到[0,a-1] 插入t到[0,b] 
//插入时,arr[i]表示管辖范围内的影响值,当不是全部,实际上查询要不断+lowbit加到暴
//二维的也只是插入一个大矩形,恢复三个小矩形的过程
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

#define lowbit(x) (x&(-x))
int x, n, m;
char cmd[5];
int x1, x2, y1, y2;
bool arr[1001][1001];
int t = 1;

void updata(int x, int y)
{
    while(x)
    {
        int t = y;
        while(t)
        {
            arr[x][t] ^= true;
            t -= lowbit(t);
        }
        x -= lowbit(x);
    }
}
bool query(int x, int y)
{
    bool result =false;
    while(x <= n)
    {
        int t = y;
        while(t <= n)
        {
            result ^= arr[x][t];

            t += lowbit(t);
        }
        x += lowbit(x);
    }
    return result;
}

int main()
{
    //freopen("1.txt", "r", stdin);
    scanf("%d", &x);
    while(x--)
    {
        if(t++ > 1) printf("\n");
        memset(arr, false, sizeof(arr));
        scanf("%d %d", &n, &m);
        
        while(m--)
        {
            scanf("%s", cmd);
            if(cmd[0] == 'C')
            {
                scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
                
                updata(x2, y2);
                updata(x1-1,y1-1);
                updata(x1-1,y2);
                updata(x2,y1-1);
            }
            else
            {
                scanf("%d %d", &x1, &y1);

                printf("%d\n", (query(x1,y1)?1:0));
            }
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值