-
Notifications
You must be signed in to change notification settings - Fork 0
/
BIT 3D.CPP
57 lines (50 loc) · 1.62 KB
/
BIT 3D.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
45
46
47
48
49
50
51
52
53
54
55
56
57
/// 3D Fenwick tree, Range updates and point queries
struct Fenwick3D{
int n, m, r, tree[MAX][MAX][MAX];
Fenwick3D(){
}
Fenwick3D(int a, int b, int c){
clr(tree);
n = a, m = b, r = c;
}
/// Add v to the cube from lower-right [i,j,k] to upper-left [1,1,1]
void update(int i, int j, int k, int v){
if ((i < 0) || (j < 0) || (i > n) || (j > m) || (k < 0) || (k > r)) return;
while (i){
int x = j;
while (x){
int y = k;
while (y){
tree[i][x][y] += v;
y ^= (y & (-y));
}
x ^= (x & (-x));
}
i ^= (i & (-i));
}
}
/// Add v to the cube from upper-left [x1,y1,z1] to lower-right [x2,y2,z2]
void update(int x1, int y1, int z1, int x2, int y2, int z2){
update(x2, y2, z2, 1), update(x1 - 1, y1 - 1, z2, 1);
update(x1 - 1, y2, z1 - 1, 1), update(x2, y1 - 1, z1 - 1, 1);
update(x1 - 1, y2, z2, -1), update(x2, y1 - 1, z2, -1);
update(x2, y2, z1 - 1, -1), update(x1 - 1, y1 - 1, z1 - 1, -1);
}
/// Query for the value at index [i][j][k]
int query(int i, int j, int k){
int res = 0;
while (i <= n){
int x = j;
while (x <= m){
int y = k;
while (y <= r){
res += tree[i][x][y];
y += (y & (-y));
}
x += (x & (-x));
}
i += (i & (-i));
}
return res;
}
};