-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPBDS.cpp
More file actions
39 lines (33 loc) · 962 Bytes
/
PBDS.cpp
File metadata and controls
39 lines (33 loc) · 962 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
// ordered_set - PBDS
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class K, class V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
//s.order_of_key(k) : Number of items strictly smaller than k
//s.find_by_order(k) : iterator to K-th element in a set (counting from zero)
//multiset : use pair
//Using Fenwick Tree implementing PBDS
int bit[mxN];
void update(ll x, ll val) {
for (; x < mxN; x += x & -x)
bit[x] += val;
}
ll query(ll x) {
ll res = 0;
for (; x > 0; x -= x & -x)
res += bit[x];
return res;
}
ll median(ll x)
{
ll l=0, r=mxN-1;
ll res=0;
while(l<=r)
{
ll m=(l+r)/2;
if(query(m)>=x) { res=m; r=m-1; }
else l=m+1;
}
return res;
}