poj 1195

第一次写二维树状数组,纪念一下

#include<iostream>

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long  c[1025][1025];
int s;
int lowbit(int i){
return (i&(-i));
}
void update(int x,int y,long long  val){
for(int i = x;i<=s;i+=lowbit(i))
for(int j = y;j<=s;j+=lowbit(j)){
c[i][j]+=val;
}
}
long long sum(int x,int y){
long long ans = 0;
for(int i = x;i>0;i-=lowbit(i))
for(int j = y;j>0;j-=lowbit(j)){
ans+=c[i][j];
}
return ans;
}
int main(){
int t;
while(scanf("%d%d",&t,&s)!=EOF){
memset(c,0,sizeof(c));
while(scanf("%d",&t),t!=3){
if(t==1){
int x,y;
long long a;
scanf("%d%d%lld",&x,&y,&a);
update(x+1,y+1,a);
}else{
int L,R,B,T;
scanf("%d%d%d%d",&L,&B,&R,&T);
L++,B++,R++,T++;
printf("%lld\n",sum(R,T)-sum(L-1,T)-sum(R,B-1)+sum(L-1,B-1));
}
}
}
return 0;
}

你可能感兴趣的:(c)