|
| 1 | +```cpp |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <vector> |
| 5 | +#include <algorithm> |
| 6 | +#include <set> |
| 7 | +#include <ext/pb_ds/assoc_container.hpp> |
| 8 | +#include <ext/pb_ds/tree_policy.hpp> |
| 9 | +using namespace std; |
| 10 | +using namespace __gnu_pbds; |
| 11 | +using ll = long long; |
| 12 | + |
| 13 | +int seg[262145][2]{}; |
| 14 | + |
| 15 | +#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> |
| 16 | +ordered_set os; |
| 17 | + |
| 18 | +void upt(int s, int e, int i, int n, int x) { |
| 19 | + if (s == e) { |
| 20 | + seg[n][x]++; |
| 21 | + return; |
| 22 | + } |
| 23 | + int m = (s + e) >> 1; |
| 24 | + if (i <= m) upt(s, m, i, n * 2, x); |
| 25 | + else upt(m + 1, e, i, n * 2 + 1, x); |
| 26 | + seg[n][x]++; |
| 27 | +} |
| 28 | + |
| 29 | +int find(int s, int e, int l, int r, int n, int x) { |
| 30 | + if (l > r || l > e || r < s) return 0; |
| 31 | + if (l <= s && e <= r) return seg[n][x]; |
| 32 | + int m = (s + e) >> 1; |
| 33 | + return find(s, m, l, r, n * 2, x) + find(m + 1, e, l, r, n * 2 + 1, x); |
| 34 | +} |
| 35 | + |
| 36 | +int main() { |
| 37 | + cin.tie(0)->sync_with_stdio(0); |
| 38 | + |
| 39 | + int N; |
| 40 | + cin >> N; |
| 41 | + |
| 42 | + vector<vector<int>> in(100001), out(100001); |
| 43 | + int H[200001]{}; |
| 44 | + |
| 45 | + vector<pair<int, int>> V; |
| 46 | + for (int i = 1, a, b; i <= N; i++) { |
| 47 | + cin >> a >> b; |
| 48 | + in[a].push_back(i); |
| 49 | + out[b].push_back(i); |
| 50 | + V.emplace_back(a, b); |
| 51 | + upt(1, 100000, a, 1, 0); |
| 52 | + upt(1, 100000, b, 1, 1); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + int s1 = 0; |
| 57 | + sort(V.begin(), V.end(), [](auto a, auto b) -> bool { |
| 58 | + if (a.second == b.second) return a.first > b.first; |
| 59 | + return a.second < b.second; |
| 60 | + }); |
| 61 | + |
| 62 | + for (int i=0;i<V.size();i++) { |
| 63 | + while(i<V.size()-1 && V[i].first == V[i+1].first){ |
| 64 | + os.insert(V[i++].first); |
| 65 | + } |
| 66 | + int a = V[i].first, b = V[i].second; |
| 67 | + int res = find(1, 100000, a, b-1, 1, 0) + find(1, 100000, a+1, b, 1, 1) - 1; |
| 68 | + |
| 69 | + |
| 70 | + res -= (os.size() - os.order_of_key(a)); |
| 71 | + os.insert(a); |
| 72 | + |
| 73 | + s1 = max(s1, res); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + set<int> S = { 0 }; |
| 78 | + int s2 = 0, mex = 0; |
| 79 | + for (int x = 1; x <= 100000; x++) { |
| 80 | + for (int i : out[x]) { |
| 81 | + S.erase(H[i]); |
| 82 | + mex = min(mex, H[i]); |
| 83 | + } |
| 84 | + for (int i : in[x]) { |
| 85 | + while (S.count(mex)) mex++; |
| 86 | + S.insert(H[i] = mex++); |
| 87 | + } |
| 88 | + s2 = max(s2, *S.rbegin()); |
| 89 | + } |
| 90 | + |
| 91 | + cout << s1 << ' ' << s2; |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +``` |
0 commit comments