-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFenwickTree.java
More file actions
52 lines (46 loc) · 1.05 KB
/
Copy pathFenwickTree.java
File metadata and controls
52 lines (46 loc) · 1.05 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
//Assumes indices are 1-based
static public class Fenwick {
public int[] table;
public Fenwick(int maxN) {
this.table = new int[maxN + 1];
}
public int sumQuery(int a, int b) {
return sumQuery(b) - sumQuery(a - 1);
}
public int sumQuery(int k) {
int ret = 0;
while (k > 0) {
ret += table[k];
k &= k - 1;
}
return ret;
}
public void adjust(int i, int adj) {
while (i < table.length) {
table[i] += adj;
i += (i & (-i));
}
}
public int getValue(int i) {
return sumQuery(i, i);
}
// Assumes entries of list are non-negative (i.e., cumulative sums are
// increasing)
// Returns first index whose cumulative sum is >= k
// Returns -1 if all are less
public int findFirst(int k) {
int L = 1, R = table.length - 1;
while (R - L > 1) {
int M = (R + L) / 2;
int val = sumQuery(M);
if (val < k)
L = M + 1;
else
R = M;
}
int LVal = sumQuery(L);
if (LVal >= k)
return L;
return R == L || sumQuery(R) < k ? -1 : R;
}
}