-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiset.cpp
61 lines (52 loc) · 1.4 KB
/
Multiset.cpp
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
#include <bits/stdtr1c++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
map <long long, int> mp;
struct Treap{ /// hash = 96814
int len;
const int ADD = 1000010;
const int MAXVAL = 1000000010;
tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> T;
Treap(){
len = 0;
T.clear(), mp.clear();
}
inline void clear(){
len = 0;
T.clear(), mp.clear();
}
inline void insert(long long x){
len++, x += MAXVAL;
int c = mp[x]++;
T.insert((x * ADD) + c);
}
inline void erase(long long x){
x += MAXVAL;
int c = mp[x];
if (c){
c--, mp[x]--, len--;
T.erase((x * ADD) + c);
}
}
/// 1-based index, returns the K'th element in the treap, -1 if none exists
inline long long kth(int k){
if (k < 1 || k > len) return -1;
auto it = T.find_by_order(--k);
return ((*it) / ADD) - MAXVAL;
}
/// Count of value < x in treap
inline int count(long long x){
x += MAXVAL;
int c = mp[--x];
return (T.order_of_key((x * ADD) + c));
}
/// Number of elements in treap
inline int size(){
return len;
}
};
int main(){
Treap s;
}