-
Notifications
You must be signed in to change notification settings - Fork 522
/
FenwickTreeExtended.java
102 lines (88 loc) · 2.87 KB
/
FenwickTreeExtended.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package structures;
public class FenwickTreeExtended {
// T[i] += value
public static void add(int[] t, int i, int value) {
for (; i < t.length; i |= i + 1) t[i] += value;
}
// sum[0..i]
public static int sum(int[] t, int i) {
int res = 0;
for (; i >= 0; i = (i & (i + 1)) - 1) res += t[i];
return res;
}
public static int[] createFromArray(int[] a) {
int[] t = a.clone();
for (int i = 0; i < a.length; i++) {
int j = i | (i + 1);
if (j < a.length)
t[j] += t[i];
}
return t;
}
// sum[a..b]
public static int sum(int[] t, int a, int b) {
return sum(t, b) - sum(t, a - 1);
}
public static int get(int[] t, int i) {
int res = t[i];
int lca = (i & (i + 1)) - 1;
for (--i; i != lca; i = (i & (i + 1)) - 1) res -= t[i];
return res;
}
public static void set(int[] t, int i, int value) {
add(t, i, -get(t, i) + value);
}
///////////////////////////////////////////////////////
// interval add
public static void add(int[] t, int a, int b, int value) {
add(t, a, value);
add(t, b + 1, -value);
}
// point query
public static int get1(int[] t, int i) {
return sum(t, i);
}
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
// interval add
public static void add(int[] t1, int[] t2, int a, int b, int value) {
add(t1, a, value);
add(t1, b, -value);
add(t2, a, -value * (a - 1));
add(t2, b, value * b);
}
// interval query
public static int sum(int[] t1, int[] t2, int i) {
return sum(t1, i) * i + sum(t2, i);
}
///////////////////////////////////////////////////////
// Returns min(p | sum[0,p] >= sum)
public static int lower_bound(int[] t, int sum) {
int pos = 0;
for (int blockSize = Integer.highestOneBit(t.length); blockSize != 0; blockSize >>= 1) {
int p = pos + blockSize - 1;
if (p < t.length && t[p] < sum) {
sum -= t[p];
pos += blockSize;
}
}
return pos;
}
// Usage example
public static void main(String[] args) {
int[] t = new int[10];
set(t, 0, 1);
add(t, 9, -2);
System.out.println(-1 == sum(t, 0, 9));
t = createFromArray(new int[] {1, 2, 3, 4, 5, 6});
for (int i = 0; i < t.length; i++) System.out.print(get(t, i) + " ");
System.out.println();
t = createFromArray(new int[] {0, 0, 1, 0, 0, 1, 0, 0});
System.out.println(5 == lower_bound(t, 2));
int[] t1 = new int[10];
int[] t2 = new int[10];
add(t1, t2, 0, 9, 1);
add(t1, t2, 0, 0, -2);
System.out.println(sum(t1, t2, 9));
}
}