-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrdered Set.cpp
More file actions
56 lines (47 loc) · 1.56 KB
/
Copy pathOrdered Set.cpp
File metadata and controls
56 lines (47 loc) · 1.56 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
//Ordered Set
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
namespace __gnu_pbds{
typedef tree<long long ,null_type,less_equal<long long >,rb_tree_tag,tree_order_statistics_node_update> oset;
}
using namespace __gnu_pbds;
//this function inserts one more occurrence of (x) into the set.
void Insert(oset &s,long long x){
s.insert(x);
}
//this function checks weather the value (x) exists in the set or not.
bool Exist(oset &s,long long x){
if((s.upper_bound(x))==s.end())
return 0;
return ((*s.upper_bound(x))==x);
}
//this function erases one occurrence of the value (x).
void Erase(oset &s,long long x){
if(Exist(s,x))
s.erase(s.upper_bound(x));
}
//this function returns the first index of the value (x)..(0 indexing).
long long FirstIdx(oset &s,long long x){
if(!Exist(s,x))
return -1;
return (s.order_of_key(x));
}
//this function returns the value at the index (idx)..(0 indexing).
long long Value(oset &s,long long idx){
return (*s.find_by_order(idx));
}
//this function returns the last index of the value (x)..(0 indexing).
long long LastIdx(oset &s,long long x){
if(!Exist(s,x))
return -1;
if(Value(s,(int)s.size()-1)==x)
return (int)(s.size())-1;
return FirstIdx(s,*s.lower_bound(x))-1;
}
//this function returns the number of occurrences of the value (x).
long long Count(oset &s,long long x){
if(!Exist(s,x))
return 0;
return LastIdx(s,x)-FirstIdx(s,x)+1;
}