题目来源:URAL 1470. UFOs
题意:求三维区间和
思路:
#include <cstdio> #include <cstring> using namespace std; const int maxn = 130; int a[maxn][maxn][maxn]; int b[maxn][maxn][maxn]; int n, q; int lowbit(int x) { return x & -x; } void update(int x, int y, int z, int num) { for(int i = x; i <= n; i += lowbit(i)) for(int j = y; j <= n; j += lowbit(j)) for(int k = z; k <= n; k += lowbit(k)) { a[i][j][k] += num; } } int sum(int x, int y, int z) { int ans = 0; for(int i = x; i > 0; i -= lowbit(i)) for(int j = y; j > 0; j -= lowbit(j)) for(int k = z; k > 0; k -= lowbit(k)) { ans += a[i][j][k]; } return ans; } int main() { scanf("%d", &n); while(scanf("%d", &q) && q != 3) { if(q == 1) { int x, y, z, k; scanf("%d %d %d %d", &x, &y, &z, &k); x++, y++, z++; if(b[x][y][z]+k >= 0) { update(x, y, z, k); b[x][y][z] += k; } else { update(x, y, z, b[x][y][z]); b[x][y][z] = 0; } } else if(q == 2) { 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++; int ans = sum(x2, y2, z2)-sum(x1-1, y2, z2)-sum(x2, y1-1, z2)+sum(x1-1, y1-1, z2) -(sum(x2, y2, z1-1)-sum(x1-1, y2, z1-1)-sum(x2, y1-1, z1-1)+sum(x1-1, y1-1, z1-1)); printf("%d\n", ans); } } return 0; }
URAL 1470. UFOs 三维树状数组
时间: 2024-11-09 20:16:02