Advertisement
Gosunov

Untitled

Aug 30th, 2023
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn = 100050;
  5. int a[maxn];
  6. int b[maxn];
  7.  
  8. int n;
  9.  
  10. struct tree {
  11.     int mx = 0;
  12.  
  13.     tree* left = nullptr;
  14.     tree* right = nullptr;
  15.  
  16.     tree() {}
  17. };
  18.  
  19. tree* t[2 * maxn];
  20.  
  21. int x, y, v;
  22. void upd_y(tree* node, int l, int r) {
  23.     if (r - l == 1) {
  24.         node->mx = max(node->mx, v);
  25.         return;
  26.     }
  27.     int m = (l + r) / 2;
  28.     if (y < m) {
  29.         if (!node->left)
  30.             node->left = new tree();
  31.         upd_y(node->left, l, m);
  32.     } else {
  33.         if (!node->right)
  34.             node->right = new tree();
  35.         upd_y(node->right, m, r);
  36.     }
  37.     node->mx = max(node->mx, v);
  38. }
  39.  
  40. void upd_x() {
  41.     x += n;
  42.     if (!t[x])
  43.         t[x] = new tree();
  44.     upd_y(t[x], 0, n);
  45.     for (; x > 1; x >>= 1) {
  46.         if (!t[x])
  47.             t[x] = new tree();
  48.         upd_y(t[x], 0, n);
  49.     }
  50. }
  51.  
  52. int rx, ry;
  53. int get_y(tree* node, int l, int r) {
  54.     if (r <= ry) {
  55.         return node->mx;
  56.     }
  57.     int m = (l + r) / 2;
  58.     int lc = 0;
  59.     int rc = 0;
  60.     if (node->left)
  61.         lc = get_y(node->left, l, m);
  62.     if (node->right && m < ry)
  63.         rc = get_y(node->right, m, r);
  64.     return max(lc, rc);
  65. }
  66.  
  67. int get_x() {
  68.     int res = 0;
  69.     int l = 0;
  70.     int r = rx;
  71.     for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
  72.         if (l&1) {
  73.             if (t[l])
  74.                 res = max(res, get_y(t[l], 0, n));
  75.             ++l;
  76.         }
  77.         if (r&1) {
  78.             --r;
  79.             if (t[r])
  80.                 res = max(res, get_y(t[r], 0, n));
  81.         }
  82.     }
  83.     return res;
  84. }
  85.  
  86. void solve() {
  87.     cin >> n;
  88.     for (int i = 0; i < n; ++i) {
  89.         cin >> a[i] >> b[i];
  90.         a[i]--, b[i]--;
  91.     }
  92.     int ans = 0;
  93.     for (int i = 0; i < n; ++i) {
  94.         int dp_i = 1;
  95.         rx = a[i];
  96.         ry = b[i];
  97.         dp_i = get_x() + 1;
  98.         ans = max(ans, dp_i);
  99.         x = a[i];
  100.         y = b[i];
  101.         v = dp_i;
  102.         upd_x();
  103.     }
  104.     cout << ans << '\n';
  105. }
  106.  
  107. signed main() {
  108.     ios::sync_with_stdio(0); cin.tie(0);
  109.     solve();
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement