forked from robs10443/User
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit.sublime-snippet
63 lines (56 loc) · 1.12 KB
/
bit.sublime-snippet
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
53
54
55
56
57
58
59
60
61
62
63
<snippet>
<content><![CDATA[
struct BIT{//use one based indexing
int N;
vector<int> bit;
void init(int n){
N = n;
bit.assign(n + 10, 0);
}
void update(int idx, int val){
while(idx <= N){
bit[idx] += val;
idx += idx & -idx;
}
}
int pref(int idx){
int ans = 0;
while(idx > 0){
ans += bit[idx];
idx -= idx & -idx;
}
return ans;
}
int rsum(int l, int r){
return pref(r) - pref(l - 1);
}
int kthOrder(int k){
int cur = 0,sum = 0, ExtraSize = N + 1;
int ln = log2(ExtraSize);
for(int i = ln;i >= 0 ; --i){
int temp = cur + (1 << i);
if(temp <= ExtraSize && (sum + bit[temp]) < k){
cur = temp;
sum += bit[temp];
}
}
if(cur == ExtraSize){
return 0;
}
return cur + 1;
}
//one based indexing
int lower_bound(int val){
return (pref(val - 1) + 1);
}
//one based indexing
int upper_bound(int val){
return (pref(val) + 1);
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>bit</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>