-
Notifications
You must be signed in to change notification settings - Fork 77
/
2D_BIT.cpp
44 lines (41 loc) · 943 Bytes
/
2D_BIT.cpp
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
template <class T>
class _2DBIT {
public:
T** tree;
int MaxX, MaxY;
_2DBIT(int M, int N) {
*tree = new T*[M + 1];
for(int i = 0; i <= M; ++i)
tree[i] = new T[N + 1];
memset(tree, 0, sizeof(T) * (M + 1) * (N + 1));
MaxX = M, MaxY = N;
}
~_2DBIT() {
free(tree);
}
void update(int x, int y, T val) {
assert(x > 0 and y > 0);
int y1;
while(x <= MaxX) {
y1 = y;
while(y1 <= MaxY) {
tree[x][y1] += val;
y1 += (y1 & -y1);
}
x += (x & -x);
}
}
T read(int x, int y) {
assert(x > 0 and y > 0);
int sum = 0, y1;
while(x > 0) {
y1 = y;
while(y1 > 0) {
sum += tree[x][y1];
y1 -= (y1 & -y1);
}
x -= (x & -x);
}
return sum;
}
};