Matrix Sum HihoCoder - 1336 二维树状数组 感觉好像二维差分。

#include
#include
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int N=1005;
ll c[N][N];
char op[5];
int lowbit(int x)
{
    return x&-x;
}
void add(int x,int y,int d)
{
    for(int i=x;i<=N;i+=lowbit(i))
        for(int j=y;j<=N;j+=lowbit(j)) 
            c[i][j]=(c[i][j]+d)%mod;
}

ll getsum(int x,int y)
{
    ll sum=0;
    for(int i=x;i>0;i-=lowbit(i))
        for(int j=y;j>0;j-=lowbit(j)) 
            sum=(sum+c[i][j])%mod;
    return sum;
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(c,0,sizeof(c));
        while(m--)
        {
            scanf("%s",op);
            if(op[0]=='A')
            {
                int x,y,v;
                scanf("%d%d%d",&x,&y,&v);
                x++,y++;
                //+1的目的是为了排除0而避免死循环 
                add(x,y,v);
            }
            else
            {
                int x1,y1,x2,y2;
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                x1++,y1++,x2++,y2++;
                ll ans=((getsum(x2,y2)-getsum(x1-1,y2)-getsum(x2,y1-1)+getsum(x1-1,y1-1))%mod+mod)%mod;
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}

 

你可能感兴趣的:(Matrix Sum HihoCoder - 1336 二维树状数组 感觉好像二维差分。)