dgc9715

Trees and Decrees

Mar 30th, 2020
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #ifdef NeverBeRed
  6. #include "debug.h"
  7. #else
  8. #define debug(...) 9715
  9. #endif
  10. typedef long long ll;
  11. typedef long double ld;
  12. typedef complex<ll> point;
  13. #define F first
  14. #define S second
  15.  
  16.  
  17.  
  18. int main()
  19. {
  20.     #ifdef TurnRed
  21.         //freopen("a.in", "r", stdin);
  22.         //freopen("a.out", "w", stdout);
  23.     #endif
  24.  
  25.     ios_base::sync_with_stdio(0), cin.tie(0);
  26.  
  27.     int n, m;
  28.     cin >> n >> m;
  29.     typedef tuple<int, int, int> thr;
  30.     vector<thr> a(m);
  31.     for (auto &i : a)
  32.     {
  33.         int x, y, z;
  34.         cin >> x >> y >> z;
  35.         i = { x, y, z };
  36.     }
  37.  
  38.     sort(a.begin(), a.end(), [](const thr &x, const thr &y)
  39.     {
  40.         return get<1>(x) < get<1>(y);
  41.     });
  42.  
  43.  
  44.     auto f = [&](ll k)
  45.     {
  46.         vector<ll> v(n+2, k);
  47.         vector<int> go(n+2);
  48.         iota(go.begin(), go.end(), 0);
  49.  
  50.         function<int(int)> run = [&](int x)
  51.         {
  52.             if (v[x] > 0) return x;
  53.             return go[x] = run(go[x+1]);
  54.         };
  55.  
  56.         for (auto i : a)
  57.         {
  58.             int x, y, z;
  59.             tie(x, y, z) = i;
  60.  
  61.             for (int g = run(x); g <= y && z > 0; )
  62.             {
  63.                 ll t = min((ll)z, v[g]);
  64.                 v[g] -= t;
  65.                 z -= t;
  66.                 if (v[g] == 0)
  67.                     g = run(g+1);
  68.             }
  69.  
  70.             if (z > 0) return false;
  71.         }
  72.  
  73.         return true;
  74.     };
  75.  
  76.     ll l = 0, r = 1e12;
  77.     while (l < r)
  78.     {
  79.         ll md = (l + r) >> 1;
  80.         if (f(md))
  81.             r = md;
  82.         else
  83.             l = md+1;
  84.     }
  85.     cout << l << "\n";
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment