1470. UFOs(三维树状数组)
最简单的三维树状数组
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 using namespace std; 7 #define lowbit(x) x&(-x) 8 int re[130][130][130],n; 9 int getsum(int x,int y,int z) 10 { 11 int s = 0,i,j,g; 12 for(i = x; i >= 1 ; i -= lowbit(i)) 13 for(j = y ; j>= 1; j -= lowbit(j)) 14 for(g = z ; g >= 1 ; g-=lowbit(g)) 15 s+=re[i][j][g]; 16 return s; 17 } 18 void add(int x,int y,int z,int da) 19 { 20 int i,j,g; 21 for(i = x ; i <= n ; i+=lowbit(i)) 22 for(j = y ; j <= n ; j+=lowbit(j)) 23 for(g = z ; g <= n ; g+=lowbit(g)) 24 re[i][j][g]+=da; 25 } 26 int main() 27 { 28 int k,x1,x2,x3,x4,x5,x6,p; 29 scanf("%d",&n); 30 while(scanf("%d",&k)!=EOF) 31 { 32 if(k==3) 33 break; 34 if(k==1) 35 { 36 scanf("%d%d%d%d",&x1,&x2,&x3,&p); 37 add(x1+1,x2+1,x3+1,p); 38 } 39 else 40 { 41 scanf("%d%d%d%d%d%d",&x1,&x2,&x3,&x4,&x5,&x6); 42 x4+=1,x5+=1,x6+=1; 43 int ans = 0; 44 ans+=getsum(x4,x5,x6); 45 ans-=getsum(x1,x5,x6); 46 ans-=getsum(x4,x2,x6); 47 ans-=getsum(x4,x5,x3); 48 ans+=getsum(x1,x5,x3); 49 ans+=getsum(x4,x2,x3); 50 ans+=getsum(x1,x2,x6); 51 ans-=getsum(x1,x2,x3); 52 printf("%d\n",ans); 53 } 54 } 55 return 0; 56 }