-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path308.cpp
More file actions
70 lines (67 loc) · 1.8 KB
/
308.cpp
File metadata and controls
70 lines (67 loc) · 1.8 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class BIT {
private:
int N;
int M;
vector<vector<int>> tree;
public:
BIT(int n, int m, vector<vector<int>>& matrix) {
N = n + 1;
M = m + 1;
tree.resize(N, vector<int>(M, 0));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
update(i, j, matrix[i - 1][j - 1]);
}
}
}
int lsb(int x) {
return x & (-x);
}
void update(int r, int c, int val) {
for (int i = r; i < N; i += lsb(i)) {
// j should be intialize in every loop
// should not use (; j < M; j += lsb(j))
for (int j = c; j < M; j += lsb(j)) {
tree[i][j] += val;
}
}
}
int query(int r, int c) {
int sum = 0;
for (int i = r; i > 0; i -= lsb(i)) {
for (int j = c; j > 0; j -= lsb(j)) {
sum += tree[i][j];
}
}
return sum;
}
};
class NumMatrix {
public:
BIT* bit;
int N;
int M;
NumMatrix(vector<vector<int>>& matrix) {
N = matrix.size();
M = matrix[0].size();
bit = new BIT(N, M, matrix);
}
void update(int row, int col, int val) {
int currVal = sumRegion(row, col, row, col);
int diff = val - currVal;
bit->update(row + 1, col + 1, diff);
}
int sumRegion(int row1, int col1, int row2, int col2) {
int a = bit->query(row2 + 1, col2 + 1);
int b = bit->query(row2 + 1, col1);
int c = bit->query(row1, col2 + 1);
int d = bit->query(row1, col1);
return a - b - c + d;
}
};
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix* obj = new NumMatrix(matrix);
* obj->update(row,col,val);
* int param_2 = obj->sumRegion(row1,col1,row2,col2);
*/