二维树状数组(区间修改,单点查询)

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

好像不管是几维都和一维原理差不多,多了一个维度也就多了一层循环而已(QAQ)

 

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include<iomanip>
using namespace std;
typedef long long ll;
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define P pair<int,int>
#define pi acos(-1)
inline int read()
{
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
int n,m,a[1008][1008];
int sum(int x,int y)
{
    int s=0;
    while(x>0)
    {
        int yy=y;
        while(yy>0)
        {
            s+=a[x][yy];
            yy-=yy&-yy;
        }
        x-=x&-x;
    }
    return s;
}
void add(int x,int z,int y)
{
    while(x<=n)
    {
        int zz=z;
        while(zz<=n)
        {
            a[x][zz]+=y;
            zz+=zz&-zz;
        }
        x+=x&-x;
    }
    return;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--)
    {
        cin>>n>>m;
        REW(a,0);
        int e=n;
        while(m--)
        {
            string b;
            int q,w,e,r;
            cin>>b;
            if(b=="C")
            {
                cin>>q>>w>>e>>r;
                add(q,w,1);
                add(q,r+1,-1);
                add(e+1,w,-1);
                add(e+1,r+1,1);
            }
            else
            {
            	cin>>q>>w;
                cout<<sum(q,w)%2<<endl;
            }
        }
        if(t)
        {
            cout<<endl;
        }
    }
    return 0;
}

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值