Submission #1105742

#TimeUsernameProblemLanguageResultExecution timeMemory
1105742tuannmFuel Station (NOI20_fuelstation)C++17
100 / 100
282 ms20928 KiB
#include<bits/stdc++.h>
#define ii pair<int, int>
#define ll pair<long long, long long>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod[2] = {1000000007, 998244353};
const int N = 3e5 + 2;
const string NAME = "ffuel";
int n, D;
vector<int> vct;

struct SegTree{
    int n, h;
    vector<long long> tree, lazy;

    SegTree(int _n) : n(_n){
        tree.resize(2 * n, 0);
        lazy.resize(n, 0);
        h = sizeof(int) * 8 - __builtin_clz(n);
    }

    void build(){
        for(int i = 0; i < 2 * n; ++i)
            tree[i] = 0;

        for(int i = 0; i < n; ++i)
            lazy[i] = 0;
    }

    void up(int u){
        while(u > 1)
            u >>= 1, tree[u] = max(tree[u << 1], tree[u << 1 | 1]) + lazy[u];
    }

    void apply(int u, long long val){
        tree[u] += val;
        if(u < n)
            lazy[u] += val;
    }

    void down(int u){
        for(int s = h; s; --s){
            int i = u >> s;

            if(lazy[i]){
              apply(i << 1, lazy[i]);
              apply(i << 1 | 1, lazy[i]);
              lazy[i] = 0;
            }
        }
    }

    void update(int l, int r, long long val){
        l += n, r += n;
        int l0 = l, r0 = r;

        for(; l < r; l >>= 1, r >>= 1){
            if(l & 1)
                apply(l++, val);
            if(r & 1)
                apply(--r, val);
        }

        up(l0);
        up(r0 - 1);
    }

    long long query(int l, int r){
        l += n, r += n;
        down(l);
        down(r - 1);

        long long res = -2e16;

        for(; l < r; l >>= 1, r >>= 1){
            if(l & 1)
                res = max(res, tree[l++]);
            if(r & 1)
                res = max(tree[--r], res);
        }
        return res;
    }
};

struct pnt{
    int x, a, b;

    bool operator < (const pnt &other) const{
        if(b != other.b)
            return b > other.b;
        return x > other.x;
    }
} a[N];

void inp(){
    cin >> n >> D;
    vct.pb(D);

    for(int i = 1; i <= n; ++i)
        cin >> a[i].x >> a[i].a >> a[i].b, vct.pb(a[i].x);
}

void solve(){
    sort(a + 1, a + n + 1);

    sort(vct.begin(), vct.end());
    vct.resize(unique(vct.begin(), vct.end()) - vct.begin());

    int m = vct.size();
    SegTree IT(m);

    for(int i = 0; i < m; ++i)
        IT.update(i, i + 1, vct[i]);

    for(int i = 1; i <= n; ++i)
        a[i].x = lower_bound(vct.begin(), vct.end(), a[i].x) - vct.begin();

    long long ans = D;

    for(int i = 1; i <= n; ++i){
        IT.update(a[i].x + 1, m, -a[i].a);
        long long val = max(0LL, IT.query(0, m));

        if(val <= a[i].b)
            ans = min(ans, val);
    }

    cout << ans;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    if(fopen((NAME + ".inp").c_str(), "r")){
        freopen((NAME + ".inp").c_str(), "r", stdin);
        freopen((NAME + ".out").c_str(), "w", stdout);
    }

    inp();
    solve();
}

Compilation message (stderr)

FuelStation.cpp: In function 'int main()':
FuelStation.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         freopen((NAME + ".inp").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FuelStation.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen((NAME + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...