题链:
http://poj.org/problem?id=1195
题解:
二维树状数组
#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 1500
using namespace std;
struct BIT{
int val[MAXN][MAXN],N;
void Reset(int n){N=n; memset(val,0,sizeof(val));}
int Lowbit(int x){return x&-x;}
void Modify(int x,int y,int v){
x++; y++;
for(int i=x;i<=N;i+=Lowbit(i))
for(int j=y;j<=N;j+=Lowbit(j))
val[i][j]+=v;
}
int Sum(int x,int y){
int ret=0;
for(int i=x;i;i-=Lowbit(i))
for(int j=y;j;j-=Lowbit(j))
ret+=val[i][j];
return ret;
}
int Area(int x1,int y1,int x2,int y2){
x1++; y1++; x2++; y2++;
return Sum(x2,y2)-Sum(x1-1,y2)-Sum(x2,y1-1)+Sum(x1-1,y1-1);
}
}DT;
int main(){
int cm,a,b,c,d;
scanf("%d%d",&cm,&a);
DT.Reset(a);
while(~scanf("%d",&cm)&&cm!=3){
if(cm==1) scanf("%d%d%d",&a,&b,&c),DT.Modify(a,b,c);
else scanf("%d%d%d%d",&a,&b,&c,&d),printf("%d\n",DT.Area(a,b,c,d));
}
return 0;
}