标签:
一. 题意:
裸二维树状数组,求(L, B) (R, T) 形成的矩形内所有点的和。
ans = getSum(R + 1, T + 1) - getSum(R + 1, B) - getSum(L, T + 1) + getSum(L, B)
#include <cstdio> #include <cstring> #define lowbit(x) x&(-x) #define MAXN 1030 int c[MAXN][MAXN]; int n; void update(int x, int y, int val) { for (int i = x; i <= n; i += lowbit(i)) { for (int j = y; j <= n; j += lowbit(j)) { c[i][j] += val; } } } int getSum(int x, int y) { int sum = 0; for (int i = x; i > 0; i -= lowbit(i)) { for (int j = y; j > 0; j -= lowbit(j)) { sum += c[i][j]; } } return sum; } int main() { int cmd, x, y, val; int x1, y1, x2, y2; scanf("%d%d", &cmd, &n); for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { c[i][j] = 0; } } while (scanf("%d", &cmd) && cmd < 3) { if (cmd == 1) { scanf("%d %d %d", &x, &y, &val); update(x + 1, y + 1, val); } else if (cmd == 2) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); int ans = getSum(x2 + 1, y2 + 1) - getSum(x2 + 1, y1) - getSum(x1, y2 + 1) + getSum(x1, y1); printf("%d\n", ans); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/subrshk/p/4236938.html