树状数组,因为此是求二维数组的区间的和,所以将其扩展为二维树状数组
#include <stdio.h> #include <memory.h> const int maxn=1100; int c[maxn][maxn]; int n; int LowBit(int x) { return x&(x^(x-1)); } void Update(int x,int y,int a) { for(int i=x;i<=n;i+=LowBit(i)) for(int j=y;j<=n;j+=LowBit(j)) c[i][j]+=a; } int getSum(int x,int y) { int i,j,sum=0; for(i=x;i>0;i=i-LowBit(i)) { for(j=y;j>0;j-=LowBit(j)) { sum+=c[i][j]; } } return sum; } int main() { int in,x,y,a,l,b,r,t; while(scanf("%d",&in)&&in!=3) { if(in==0) { scanf("%d",&n); memset(c,0,sizeof(c)); } else if(in==1) { scanf("%d%d%d",&x,&y,&a); Update(x+1,y+1,a); } else { scanf("%d%d%d%d",&l,&b,&r,&t); int acount=getSum(r+1,t+1)-getSum(l+1-1,t+1)-getSum(r+1,b+1-1)+getSum(l+1-1,b+1-1); printf("%d\n",acount); } } return 0; }