-
Notifications
You must be signed in to change notification settings - Fork 7
/
FenwickTree.java
51 lines (50 loc) · 1.09 KB
/
FenwickTree.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
// brett fazio, fenwick tree, sum for range
import java.util.*;
public class FenwickTree {
static int[] tree;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
tree = new int[n+1];
tree[0] = 0;
for (int i = 1; i < n; i++) {
update(i,sc.nextInt());
}
// sum for range
System.out.println(query(0,3));
}
public static int sum(int i) {
int res = 0;
while (i > 0) {
res += tree[i];
i &= (i-1);
}
return res;
}
public static void update(int i, int sum) {
while (i <= tree.length) {
tree[i] += sum;
i += (i) & (-i); //Integer.lowestOneBit(i);
}
}
public static int query(int i, int j) {
return sum(j) - sum(i-1);
}
public static int findKth(int k) {
int lastK = tree.length;
int pos = 0;
int pow2 = Integer.highestOneBit(lastK);
while (pow2 > 0) {
if (pos + pow2 <= tree.length) {
if (tree[pos+pow2] >= k) {
lastK = Math.min(lastK, pos+pow2);
}else {
pos += pow2;
k -= tree[pos];
}
}
pow2 >>= 1;
}
return lastK;
}
}