二维树状数组,改下可直接套模版,注意点就是坐标中有0的情况,所以只要x和y都加上1即可。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#define M 1500
#define LL long long
using namespace std;
int op,s;
int c[M][M];
int Lowbit(int x)
{
return x & (-x);
}
void Update(int x,int y,int d)
{
int i,j;
for(i=x;i<=s;i+=Lowbit(i))
{
for(j=y;j<=s;j+=Lowbit(j))
{
c[i][j]+=d;
}
}
}
LL Sum(int x,int y)
{
int i,j;
LL sum=0;
for(i=x;i>0;i-=Lowbit(i))
{
for(j=y;j>0;j-=Lowbit(j))
{
sum+=c[i][j];
}
}
return sum;
}
int main()
{
while(scanf("%d",&op)!=EOF)
{
if(op==0)
{
scanf("%d",&s);
memset(c,0,sizeof(c));
}
else if(op==1)
{
int x,y,t;
scanf("%d%d%d",&x,&y,&t);
Update(x+1,y+1,t);
}
else if(op==2)
{
int l,b,r,t;
scanf("%d%d%d%d",&l,&b,&r,&t);
LL ans=0;
ans+=Sum(r+1,t+1);
ans+=Sum(l,b);
ans-=Sum(l,t+1);
ans-=Sum(r+1,b);
printf("%lld\n",ans);
}
else
continue;
}
return 0;
}