Advertisement
Dang_Quan_10_Tin

CDAY

May 16th, 2023
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. constexpr int N = 1e6 + 5;
  5. constexpr int Inf = 1e9 + 7;
  6.  
  7. int n, t, f[N];
  8. int a[N], b[N];
  9. int l[N], r[N];
  10. vector<int> startBy[N],
  11.     endBy[N];
  12.  
  13. multiset<int> listl, listr; // "danh sách"
  14.  
  15. void Read()
  16. {
  17.     cin >> n >> t;
  18.  
  19.     for (int i = 1; i <= t; ++i)
  20.         cin >> a[i] >> b[i];
  21. }
  22.  
  23. struct SegmentTree {
  24.     int n;
  25.     int st[N * 4];
  26.    
  27.     SegmentTree() {
  28.         fill_n(st, N * 4, -Inf);
  29.     }
  30.     void Update(int id, int l, int r, const int &x, const int &v) {
  31.         if(l > x || r < x) return;
  32.         if(l == x && r == x) {
  33.             st[id] = v;
  34.             return;
  35.         }
  36.         Update(id << 1, l, (l + r) / 2, x, v);
  37.         Update(id << 1 | 1, (l + r) / 2 + 1, r, x, v);
  38.         st[id] = max(st[id << 1], st[id << 1 | 1]);
  39.     }
  40.     void Update(int x, int v) {
  41.         Update(1, 1, n, x, v);
  42.     }
  43.  
  44.     int Get(int id, int l, int r, const int &a, const int &b) {
  45.         if(l > b || r < a) return -Inf;
  46.         if(l >= a && r <= b) return st[id];
  47.  
  48.         return max(Get(id << 1, l, (l + r) / 2, a, b), Get(id << 1 | 1, (l + r) / 2 + 1, r, a, b));
  49.     }
  50.  
  51.     int Get(int l, int r) {
  52.         return Get(1, 1, n, l, r);
  53.     }
  54. } g;
  55.  
  56. void Solve()
  57. {
  58.     for (int i = 1; i <= t; ++i)
  59.     {
  60.         startBy[a[i]].push_back(i);
  61.         endBy[b[i]].push_back(i);
  62.     }
  63.  
  64.     for (int i = 1; i <= n + 1; ++i)
  65.     {
  66.         for (auto proj : startBy[i])
  67.             listr.insert(a[proj]); // Thêm phần tử
  68.  
  69.         r[i] = listr.empty() ? i : *listr.begin();
  70.         l[i] = listl.empty() ? 0 : *listl.rbegin();
  71.  
  72.         for (auto proj : endBy[i])
  73.         {
  74.             listr.erase(listr.find(a[proj])); // Chỉ xóa một phần tử
  75.             listl.insert(a[proj]);
  76.         }
  77.     }
  78.  
  79.     g.n = n + 2;
  80.     g.Update(0 + 1, 0);
  81.     //cerr << "ok\n";
  82.  
  83.     for(int i = 1; i <= n + 1; ++i) {
  84.         //cout << i << ": " << l[i] << " " << r[i] << "\n";
  85.         f[i] = g.Get(l[i] + 1, (r[i] - 1) + 1);
  86.        
  87.         if(f[i] < 0)
  88.             f[i] = -1;
  89.         else
  90.         {
  91.             ++f[i];
  92.             g.Update(i + 1, f[i]);
  93.         }
  94.     }
  95.  
  96.     if(f[n + 1] == -1) cout << "-1";
  97.     else cout << f[n + 1] - 1;
  98. }
  99.  
  100. int main() {
  101.     ios_base::sync_with_stdio(0);
  102.     cin.tie(0);
  103.  
  104.     freopen("CDAY.INP", "r", stdin);
  105.     freopen("CDAY.OUT", "w", stdout);
  106.  
  107.     Read();
  108.     Solve();
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement