題目分析
二維樹狀數組,單點更新、區間查詢
代碼
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=+;
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=;
for(int i=x;i>;i-=lowbit(i))
for(int j=y;j>;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-,y2)-getsum(x2,y1-)+getsum(x1-,y1-);
}
int main()
{
int op;
memset(bit,,sizeof bit);
while(cin>>op)
{
switch(op)
{
case :cin>>n;break;
case :
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x+,y+,z);
break;
}
case :
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
cout<<calc(x1+,y1+,x2+,y2+)<<endl;
break;
}
}
}
return ;
}