Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma g++ optimize("Ofast")
- #pragma loop_opt(on)
- #include <bits/stdc++.h>
- #define pb emplace_back
- #define ff first
- #define ss second
- #define mem(v,x) memset((v),(x),sizeof(v))
- #define debug(x) cerr<<#x<<" = "<<x<<'\n'
- using namespace std;
- typedef int64_t ll;
- constexpr int N = 500025, inf = 1e9, MOD = 1000000007;
- #define int ll
- struct Segtree {
- ll mx[N<<1],lz[N];
- int n;
- void init(auto &u, int _n) {
- n = _n;
- for(int i = 0; i < n; i++) lz[i] = 0;
- for(int i = 0; i < n; i++) mx[i+n] = u[i];
- for(int i = n-1; i > 0; i--) mx[i] = max(mx[i<<1], mx[i<<1|1]);
- }
- void upd(int p, ll d) {
- mx[p] += d;
- if(p < n) lz[p] += d;
- }
- void pull(int p) {
- for(; p>1; p>>=1)
- mx[p>>1] = max(mx[p],mx[p^1]) + lz[p>>1];
- }
- void push(int p) {
- for(int h = __lg(n); h >= 0; h--) {
- int i = p>>h;
- upd(i, lz[i>>1]);
- upd(i^1, lz[i>>1]);
- lz[i>>1] = 0;
- }
- }
- void add(int l, int r, ll d) {
- int L = l, R = r;
- for(l+=n, r+=n; l<r; l>>=1, r>>=1) {
- if(l&1) upd(l++, d);
- if(r&1) upd(--r, d);
- }
- pull(L+n), pull(R-1+n);
- }
- } sgt;
- signed main() {
- ios_base::sync_with_stdio(0), cin.tie(0);
- int n, d;
- cin >> n >> d;
- vector<tuple<int,int,int>> v(n);
- vector<int> u;
- for(auto &[x, a, b]: v) cin >> x >> a >> b, u.pb(x);
- u.pb(0), u.pb(d); // 要考慮位置0跟位置d
- sort(u.begin(), u.end()), u.erase(unique(u.begin(), u.end()), u.end());
- for(auto &[x, a, b]: v) x = lower_bound(u.begin(), u.end(), x) - u.begin();
- sgt.init(u, u.size());
- sort(v.begin(), v.end(), [](auto a, auto b){return get<2>(a) > get<2>(b);});
- ll ans = d;
- for(auto [x, a, b]: v) {
- sgt.add(x+1, u.size(), -a); //左閉右開, 0 base
- //debug(a);
- ll f = sgt.mx[1];
- if(f <= b)
- ans = min(ans, f);
- }
- cout << ans << '\n';
- }
- /*
- set all i s.t. B[i] >= B active
- for all i, F + sum(A[j] | X[j] < X[i] and active[j]) >= X[i]
- F = max(X[i] - sum(A[j] | X[j] < X[i] and active[j]))
- check if F <= B
- */
Advertisement
Add Comment
Please, Sign In to add comment