题意:由单位立方体组成的三维空间里,执行2种操作,一种是修改位置为(x, y, z)的ufo改变K个,另一种是询问空间(x1, y1, z1)-(x2, y2, z2)里的ufo数目(1 ≤ N ≤ 128, –20000 ≤ K ≤ 20000)。
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1470
——>>第一次刷的三维树状数组题目,有意思。。。#^_^。。。不过也没意思(只是比二维的多一层罢了。。。T_T)
小感:坐标有0,而树状数组从1开始计数,目前已形成条件反射,会加1后再计算了。。。。。。这个坑不到我啦。。。
#include <cstdio>using namespace std;const int maxn = 128 + 10;int N;
int c[maxn][maxn][maxn];int lowerbit(int x) {return x & (-x);
}void add(int x, int y, int z, int v) {for(int i = x; i <= N; i += lowerbit(i))for(int j = y; j <= N; j += lowerbit(j))for(int k = z; k <= N; k += lowerbit(k))c[i][j][k] += v;
}long long sum(int x, int y, int z) {long long ret = 0;for(int i = x; i > 0; i -= lowerbit(i))for(int j = y; j > 0; j -= lowerbit(j))for(int k = z; k > 0; k -= lowerbit(k))ret += c[i][j][k];return ret;
}int main()
{int op;while(scanf("%d", &N) == 1) {while(scanf("%d", &op) && op != 3) {if(op == 1) {int x, y, z, k;scanf("%d%d%d%d", &x, &y, &z, &k);add(x+1, y+1, z+1, k);}else {int x1, y1, z1, x2, y2, z2;scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);x1++; y1++; z1++;x2++; y2++; z2++;printf("%I64d\n", sum(x2, y2, z2)-sum(x2, y1-1, z2)-sum(x1-1, y2, z2)+sum(x1-1, y1-1, z2)-(sum(x2, y2, z1-1)-sum(x2, y1-1, z1-1)-sum(x1-1, y2, z1-1)+sum(x1-1, y1-1, z1-1)));}}}return 0;
}