-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBIT.cpp
More file actions
32 lines (30 loc) · 757 Bytes
/
BIT.cpp
File metadata and controls
32 lines (30 loc) · 757 Bytes
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
struct BIT{
ll N;
vll bit;
void init(ll n){
N = n;
bit.assign(n+1 , 0);
}
void add(int x, int k) {
for (; x <= N; x += x & -x) bit[x] += k;
}
int rsum(int l, int r) {
int res = 0;
for (int x = l - 1; x; x -= x & -x) res -= bit[x];
for (int x = r; x; x -= x & -x) res += bit[x];
return res;
}
ll find(ll val){
ll curr = 0 , prevsum = 0;
for(int i = log2(N) ; i >= 0 ; i --){
if(curr + (1 << i) < N && prevsum + bit[curr + (1 << i)] < val){
prevsum += bit[curr + (1 << i)];
curr += (1 << i);
}
}
return curr + 1;
}
void prints(void){
printv(bit);
}
};