题目分析
二维树状数组,单点更新、区间查询
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=1024+5;
int bit[maxn][maxn];
int n;
inline int lowbit(int x)
{
return x&-x;
}
void add(int x,int y,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
bit[i][j]+=val;
}
int getsum(int x,int y)
{
int res=0;
for(int i=x;i>0;i-=lowbit(i))
for(int j=y;j>0;j-=lowbit(j))
res+=bit[i][j];
return res;
}
int calc(int x1,int y1,int x2,int y2)
{
return getsum(x2,y2)-getsum(x1-1,y2)-getsum(x2,y1-1)+getsum(x1-1,y1-1);
}
int main()
{
int op;
memset(bit,0,sizeof bit);
while(cin>>op)
{
switch(op)
{
case 0:cin>>n;break;
case 1:
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x+1,y+1,z);
break;
}
case 2:
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
cout<<calc(x1+1,y1+1,x2+1,y2+1)<<endl;
break;
}
}
}
return 0;
}