-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomizeDS.cpp
More file actions
29 lines (24 loc) · 797 Bytes
/
CustomizeDS.cpp
File metadata and controls
29 lines (24 loc) · 797 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
/*
Set with custom operator
Use case: Create a set of pairs where set is sorted in decreasing order first element
and if first elements are same then in increasing order of second element
*/
// Method 1
auto cmp = [](pair<int, int> a, pair<int, int> b) {
// YOUR ORDERING LOGIC
if(a.first > b.first) return true;
if(a.first < b.first) return false;
return a.second < b.second;
};
std::set<pll, decltype(cmp)> sp(cmp);
// Method 2 : https://codeforces.com/blog/entry/77373
struct cmp {
bool operator() (const pair<int, int> &a, const pair<int, int> &b) const {
// YOUR ORDERING LOGIC
int lena = a.second - a.first + 1;
int lenb = b.second - b.first + 1;
if (lena == lenb) return a.first < b.first;
return lena > lenb;
}
};
set<pair<int, int>, cmp> segs;