Repository navigation
Expand file tree
/
Copy pathindexed_set.cpp
More file actions
71 lines (54 loc) · 1.68 KB
/
Copy pathindexed_set.cpp
File metadata and controls
71 lines (54 loc) · 1.68 KB
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
64
65
66
67
68
69
70
// WE ARE NOT HERE TO TAKE PART , WE ARE HERE TO TAKE OVER
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
/*
Suppose that we want a data structure that supports all the operations as a set in C++ in addition to the following:
order_of_key(x): counts the number of elements in the set that are strictly less than x.
find_by_order(k): similar to find, returns the iterator corresponding to the k-th lowest element in the set (0-indexed).
you can use it fa3udh matesta3mel segment tree ya bro :) :) :) :) it's not faster than segment tree
but it's just double so no big deal .
*/
/*
typedef pair<int,int> pii;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
Tree<pii> o;
*/
void solve(){
ordered_set X;
X.insert(1);
X.insert(2);
X.insert(4);
X.insert(8);
X.insert(16);
cout<<*X.find_by_order(1)<<endl; // 2
cout<<*X.find_by_order(2)<<endl; // 4
cout<<*X.find_by_order(4)<<endl; // 16
cout<<(end(X)==X.find_by_order(6))<<endl; // true
cout<<X.order_of_key(-5)<<endl; // 0
cout<<X.order_of_key(1)<<endl; // 0
cout<<X.order_of_key(3)<<endl; // 2
cout<<X.order_of_key(4)<<endl; // 2
cout<<X.order_of_key(400)<<endl; // 5
int pos = *x.find_by_order(ind);
x.erase(pos); boom :)
}
int main() {
int tc=1;
while(tc--){
solve();
}
return 0;
}
/*
Some insights:
.Binary search
.Graph representation
.Write brute force code
.Change your approach
*/