-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_308.java
More file actions
52 lines (44 loc) · 1.42 KB
/
Copy pathP_308.java
File metadata and controls
52 lines (44 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package leetcode.hard;
public class P_308 {
static class NumMatrix {
int n, m;
int[][] bit;
int[][] original;
NumMatrix(int[][] matrix) {
if (matrix.length < 1) { return; }
n = matrix.length;
m = matrix[0].length;
bit = new int[n + 1][m + 1];
original = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
update(i, j, matrix[i][j]);
}
}
}
public void update(int row, int col, int val) {
final int diff = val - original[row][col];
original[row][col] = val;
for (int i = row + 1; i <= n; i += lsb(i)) {
for (int j = col + 1; j <= m; j += lsb(j)) {
bit[i][j] += diff;
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
return sum(row2, col2) + sum(row1 - 1, col1 - 1) - sum(row1 - 1, col2) - sum(row2, col1 - 1);
}
public int sum(int row, int col) {
int res = 0;
for (int i = row + 1; i > 0; i -= lsb(i)) {
for (int j = col + 1; j > 0; j -= lsb(j)) {
res += bit[i][j];
}
}
return res;
}
private static int lsb(int i) {
return i & -i;
}
}
}