-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedRangesCount.cpp
More file actions
64 lines (50 loc) · 1.43 KB
/
NestedRangesCount.cpp
File metadata and controls
64 lines (50 loc) · 1.43 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
//Nested Ranges Count - https://cses.fi/problemset/task/2169
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less_equal<>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
void solve() {
int n;
cin >> n;
vector<array<int, 3>> ranges(n);
for (int i = 0; i < n; i++) {
cin >> ranges[i][0] >> ranges[i][1];
ranges[i][2] = i;
}
sort(ranges.begin(), ranges.end(), [&](const array<int, 3>& range1, const array<int, 3>& range2) {
return range1[0] != range2[0] ? range1[0] < range2[0] : range1[1] > range2[1];
});
vector<int> contained(n, 0), contains(n, 0);
indexed_set st;
for (auto [l, r, idx]: ranges) {
contained[idx] = (int) st.size() - st.order_of_key(r);
st.insert(r);
}
st.clear();
reverse(ranges.begin(), ranges.end());
for (auto [l, r, idx]: ranges) {
contains[idx] = st.order_of_key(r + 1);
st.insert(r);
}
for (int i = 0; i < n; i++) {
cout << contains[i] << " \n"[i == n - 1];
}
for (int i = 0; i < n; i++) {
cout << contained[i] << " \n"[i == n - 1];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}