forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFenwickTree3D.java
More file actions
25 lines (22 loc) · 870 Bytes
/
FenwickTree3D.java
File metadata and controls
25 lines (22 loc) · 870 Bytes
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
package structures;
public class FenwickTree3D {
public static void add(int[][][] t, int x, int y, int z, int value) {
for (int i = x; i < t.length; i |= i + 1)
for (int j = y; j < t[0].length; j |= j + 1)
for (int k = z; k < t[0][0].length; k |= k + 1) t[i][j][k] += value;
}
// sum[(0, 0, 0), (x, y, z)]
public static int sum(int[][][] t, int x, int y, int z) {
int res = 0;
for (int i = x; i >= 0; i = (i & (i + 1)) - 1)
for (int j = y; j >= 0; j = (j & (j + 1)) - 1)
for (int k = z; k >= 0; k = (k & (k + 1)) - 1) res += t[i][j][k];
return res;
}
public static void main(String[] args) {
int[][][] t = new int[10][20][30];
add(t, 0, 0, 0, 1);
add(t, 9, 19, 29, -2);
System.out.println(-1 == sum(t, 9, 19, 29));
}
}