-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBIT.cpp
More file actions
52 lines (45 loc) · 998 Bytes
/
Copy pathBIT.cpp
File metadata and controls
52 lines (45 loc) · 998 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "bits/stdc++.h"
using namespace std;
template <typename T>
class BIT{
int n;
vector<T> bit;
public :
BIT(int tree_size){
n = tree_size;
bit.resize(n + 5) ;
}
bool update(int x, T val){
if(x>n or x<1)return false;
for (; x <= n; x += x & -x){
bit[x] += val;
}
return true;
}
T query(int x){
T sum = 0;
for (; x > 0; x -= x & -x)
sum += bit[x];
return sum;
}
T query(int l, int r){
return query(r) - query(l - 1) ;
}
T lower_bound(T sum){
T curr=0,prev=0;
for (int i = log2(n); i>=0 ; --i){
if( ((curr+( 1LL<<i))<=n) && (bit[curr+(1LL<<i)]+prev)<sum){
curr+=(1LL<<i);
prev+=bit[curr];
}
}
return curr+1;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
BIT<int>bt(10);
return 0;
}