-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBinaryIndexedTree.java
50 lines (43 loc) · 1.04 KB
/
BinaryIndexedTree.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
package main.java.datastructure;
/*
https://www.youtube.com/watch?v=CWDQJGaN1gY
https://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2/
*/
public class BinaryIndexedTree {
int[] nums;
int[] BIT;
int n;
public BinaryIndexedTree(int[] nums){
this.nums = nums;
n = nums.length;
BIT = new int[n+1];
//Build BIT
for (int i =0;i<n;i++){
init(i,nums[i]);
}
}
private void init(int i,int val){
i++;
while (i<=n){ // till i reaches at the end of array
BIT[i]+=val;
i+=(i&-i);//get next position for update the prefix sum
}
}
public void update(int i,int val){
int diff = val - nums[i];
nums[i] = val;
init(i,diff);
}
public int getSum(int i){
int sum =0;
i++;
while (i>0){
sum+=BIT[i];
i-=(i&-i); //get parent
}
return sum;
}
public int sumRange(int i,int j){
return getSum(j) - getSum(i-1);
}
}