-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathBIT.java
49 lines (44 loc) · 1.03 KB
/
BIT.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
/* 1 indexed
to insert elements use update(arr[i],1) if want to find kth order;
insert update(i,arr[i]) if you want rangeQuery and point update
*/
long[] bit=new long[8+2];
//get method should be used when kth order statistic is used : on BIT
int get(int pos){
int start = 1,end = (int)1e6+5;
int ans = -1;
while(start<=end){
int m = start + (end-start)/2 ;
int countOfElements = sum(m);
if(countOfElements>=K){
ans = m ;
end = m-1;
}
else{
start = m+1;
}
}
}
void rangeUpdate(int l,int r,long del){
//you can not use rangeUpdate and RangeQuery at same time
//because both have different notions for bit[]
update(l,del);
update(r+1,-del);
}
void update(int pos,long del){
while(pos<bit.length){
bit[pos] += del;
pos += Long.lowestOneBit(pos);
}
}
long sum(int pos){
long sum=0;
while(pos>0){
sum = (sum + bit[pos]);
pos -= Long.lowestOneBit(pos);
}
return sum;
}
long rangeSum(int l ,int r){
return sum(r)-sum(l-1);
}