-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedRangesCheck.cpp
More file actions
62 lines (52 loc) · 1.46 KB
/
NestedRangesCheck.cpp
File metadata and controls
62 lines (52 loc) · 1.46 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
//Nested Ranges Check - https://cses.fi/problemset/task/2168
#include <bits/stdc++.h>
using namespace std;
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); //[left, right, index]
for (int i = 0; i < n; i++) {
cin >> ranges[i][0] >> ranges[i][1];
ranges[i][2] = i;
}
vector<int> containing(n, 0), contained(n, 0);
//sorting by starting time and then by reversed end time.
sort(ranges.begin(), ranges.end(), [&](array<int, 3> &a, array<int, 3> &b) {
return (a[0] == b[0] ? a[1] > b[1] : a[0] < b[0]);
});
int max_right = ranges[0][1];
for (int i = 1; i < n; i++) {
auto [left, right, index] = ranges[i];
if (right <= max_right) {
contained[index] = 1;
}
max_right = max(max_right, right);
}
int min_right = ranges[n - 1][1];
for (int i = n - 2; i >= 0; i--) {
auto [left, right, index] = ranges[i];
if (right >= min_right) {
containing[index] = 1;
}
min_right = min(min_right, right);
}
for (int i = 0; i < n; i++) {
cout << containing[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;
}