Untitled

 avatar
unknown
c_cpp
2 months ago
1.0 kB
2
Indexable
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) ::begin(x), ::end(x)
void _d(auto... x) { ((cerr << ' ' << x), ...) << endl; }
#define debug(x...) cerr << "["#x"]:", _d(x)

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n;
    ll S;
    cin >> n >> S;

    vector<ll> c(n);
    vector<array<ll, 3>> events;
    for (int i=0; i<n; i++) {
        ll l, r;
        cin >> l >> r >> c[i];
        events.push_back({l, 1, i});
        events.push_back({r+1, 0, i});
    }
    sort(all(events));

    set<pair<ll, ll>> s;
    vector<ll> idk(n);
    ll offset = 0;
    ll last = 0;
    ll mx = 0;
    for (auto [x, type, ind] : events) {
        offset += S * (x - last);
        if (!s.empty()) mx = max(mx, s.rbegin()->first + offset);
        last = x;
        if (type == 0) {
            s.erase({idk[ind], ind});
        } else {
            idk[ind] = mx - offset - c[ind];
            s.emplace(idk[ind], ind);
        }
    }
    cout << mx << '\n';
}
Leave a Comment