-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy based data structures.cpp
62 lines (49 loc) · 1.51 KB
/
policy based data structures.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
62
/// Add / remove elements
/// No of elements < X
/// k-th smallest element
/// min / max
/// Time complexity = log(n)
#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> pbds;
int main()
{
/// Add elements in any random order
pbds A;
A.insert(11);
A.insert(1);
A.insert(5);
A.insert(3);
A.insert(7);
A.insert(9);
A.insert(3);
//Total contents
cout << "1, 3,5, 7, 9, 11" << endl;
//K-th smallest
int k = 3;
cout << k << "rd smallest: " << *A.find_by_order(k-1) << endl;
k = 5;
cout << k << "th smallest: " << *A.find_by_order(k-1) << endl;
//NO OF ELEMENTS < X
int X = 9;
/*
Ordered_set can't erase element x but it can erase k th element by doing A.erase(A.find_by_order(k))
*/
cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;
//DELETE Elements
cout << "Deleted 3" << endl;
A.erase(A.find(3));
//Total contents
/// will not work for duplicates.
cout << "1, 3 ,5 ,7, 9, 11" << endl;
k = 3;
cout << k << "rd smallest: " << *A.find_by_order(k-1) << endl;
cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;
//NEXT BIGGER/SMALLER ELEMENT than X
X = 8;
cout << "Next greater element than " << X << " is " << *A.upper_bound(X) << endl;
return 0;
}