bingxuan9112

Untitled

Apr 19th, 2020
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #pragma g++ optimize("Ofast")
  2. #pragma loop_opt(on)
  3. #include <bits/stdc++.h>
  4. #define pb emplace_back
  5. #define ff first
  6. #define ss second
  7. #define mem(v,x) memset((v),(x),sizeof(v))
  8. #define debug(x) cerr<<#x<<" = "<<x<<'\n'
  9.  
  10. using namespace std;
  11. typedef int64_t ll;
  12. constexpr int N = 500025, inf = 1e9, MOD = 1000000007;
  13.  
  14. #define int ll
  15. struct Segtree {
  16.     ll mx[N<<1],lz[N];
  17.     int n;
  18.     void init(auto &u, int _n) {
  19.         n = _n;
  20.         for(int i = 0; i < n; i++) lz[i] = 0;
  21.         for(int i = 0; i < n; i++) mx[i+n] = u[i];
  22.         for(int i = n-1; i > 0; i--) mx[i] = max(mx[i<<1], mx[i<<1|1]);
  23.     }
  24.     void upd(int p, ll d) {
  25.         mx[p] += d;
  26.         if(p < n) lz[p] += d;
  27.     }
  28.     void pull(int p) {
  29.         for(; p>1; p>>=1)
  30.             mx[p>>1] = max(mx[p],mx[p^1]) + lz[p>>1];
  31.     }
  32.     void push(int p) {
  33.         for(int h = __lg(n); h >= 0; h--) {
  34.             int i = p>>h;
  35.             upd(i, lz[i>>1]);
  36.             upd(i^1, lz[i>>1]);
  37.             lz[i>>1] = 0;
  38.         }
  39.     }
  40.     void add(int l, int r, ll d) {
  41.         int L = l, R = r;
  42.         for(l+=n, r+=n; l<r; l>>=1, r>>=1) {
  43.             if(l&1) upd(l++, d);
  44.             if(r&1) upd(--r, d);
  45.         }
  46.         pull(L+n), pull(R-1+n);
  47.     }
  48. } sgt;
  49. signed main() {
  50.     ios_base::sync_with_stdio(0), cin.tie(0);
  51.     int n, d;
  52.     cin >> n >> d;
  53.     vector<tuple<int,int,int>> v(n);
  54.     vector<int> u;
  55.     for(auto &[x, a, b]: v) cin >> x >> a >> b, u.pb(x);
  56.     u.pb(0), u.pb(d); // 要考慮位置0跟位置d
  57.     sort(u.begin(), u.end()), u.erase(unique(u.begin(), u.end()), u.end());
  58.     for(auto &[x, a, b]: v) x = lower_bound(u.begin(), u.end(), x) - u.begin();
  59.     sgt.init(u, u.size());
  60.     sort(v.begin(), v.end(), [](auto a, auto b){return get<2>(a) > get<2>(b);});
  61.     ll ans = d;
  62.     for(auto [x, a, b]: v) {
  63.         sgt.add(x+1, u.size(), -a); //左閉右開, 0 base
  64.         //debug(a);
  65.         ll f = sgt.mx[1];
  66.         if(f <= b)
  67.             ans = min(ans, f);
  68.     }
  69.     cout << ans << '\n';
  70. }
  71.  
  72. /*
  73. set all i s.t. B[i] >= B active
  74. for all i, F + sum(A[j] | X[j] < X[i] and active[j]) >= X[i]
  75. F = max(X[i] - sum(A[j] | X[j] < X[i] and active[j]))
  76. check if F <= B
  77. */
Advertisement
Add Comment
Please, Sign In to add comment