Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 100050;
- int a[maxn];
- int b[maxn];
- int n;
- struct tree {
- int mx = 0;
- tree* left = nullptr;
- tree* right = nullptr;
- tree() {}
- };
- tree* t[2 * maxn];
- int x, y, v;
- void upd_y(tree* node, int l, int r) {
- if (r - l == 1) {
- node->mx = max(node->mx, v);
- return;
- }
- int m = (l + r) / 2;
- if (y < m) {
- if (!node->left)
- node->left = new tree();
- upd_y(node->left, l, m);
- } else {
- if (!node->right)
- node->right = new tree();
- upd_y(node->right, m, r);
- }
- node->mx = max(node->mx, v);
- }
- void upd_x() {
- x += n;
- if (!t[x])
- t[x] = new tree();
- upd_y(t[x], 0, n);
- for (; x > 1; x >>= 1) {
- if (!t[x])
- t[x] = new tree();
- upd_y(t[x], 0, n);
- }
- }
- int rx, ry;
- int get_y(tree* node, int l, int r) {
- if (r <= ry) {
- return node->mx;
- }
- int m = (l + r) / 2;
- int lc = 0;
- int rc = 0;
- if (node->left)
- lc = get_y(node->left, l, m);
- if (node->right && m < ry)
- rc = get_y(node->right, m, r);
- return max(lc, rc);
- }
- int get_x() {
- int res = 0;
- int l = 0;
- int r = rx;
- for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
- if (l&1) {
- if (t[l])
- res = max(res, get_y(t[l], 0, n));
- ++l;
- }
- if (r&1) {
- --r;
- if (t[r])
- res = max(res, get_y(t[r], 0, n));
- }
- }
- return res;
- }
- void solve() {
- cin >> n;
- for (int i = 0; i < n; ++i) {
- cin >> a[i] >> b[i];
- a[i]--, b[i]--;
- }
- int ans = 0;
- for (int i = 0; i < n; ++i) {
- int dp_i = 1;
- rx = a[i];
- ry = b[i];
- dp_i = get_x() + 1;
- ans = max(ans, dp_i);
- x = a[i];
- y = b[i];
- v = dp_i;
- upd_x();
- }
- cout << ans << '\n';
- }
- signed main() {
- ios::sync_with_stdio(0); cin.tie(0);
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement