首页 > 代码库 > URAL 1470. UFOs 三维树状数组
URAL 1470. UFOs 三维树状数组
题目来源: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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。