POJ Problem 1195 - Mobile phones
题目类型:二维树状数组
题意
在一个 S × S S \times S S×S 大小的矩阵中,进行单点修改,子矩阵查询。
分析
使用二维树状数组。
代码
static int[][] C;
public static void solve() throws IOException {
int tt = nextInt();
int S = nextInt();
C = new int[S + 10][S + 10];
while (true) {
int op = nextInt();
if (op == 3) break;
if (op == 1) {
int x = nextInt() + 1;
int y = nextInt() + 1;
int v = nextInt();
add(x, y, v, S + 10);
} else {
int x1 = nextInt() + 1;
int y1 = nextInt() + 1;
int x2 = nextInt() + 1;
int y2 = nextInt() + 1;
int ans = getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1);
pw.println(ans);
}
}
}
public static void add(int x, int y, int v, int n) {
for (int i = x; i < n; i += lowbit(i))
for (int j = y; j < n; j += lowbit(j))
C[i][j] += v;
}
public static int getSum(int x, int y) {
int re = 0;
for (int i = x; i > 0; i -= lowbit(i))
for (int j = y; j > 0; j -= lowbit(j))
re += C[i][j];
return re;
}
public static int lowbit(int x) { return x & (-x); }