Submission #146182

#TimeUsernameProblemLanguageResultExecution timeMemory
146182popovicirobertSalesman (IOI09_salesman)C++14
60 / 100
1082 ms44280 KiB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long


#if 0
const int MOD = ;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}

inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}

inline int inv(int x) {
    return lgput(x, MOD - 2);
}
#endif

#if 0
int fact[], invfact[];

inline void prec(int n) {
    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    invfact[n] = lgput(fact[n], MOD - 2);
    for(int i = n - 1; i >= 0; i--) {
        invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif

using namespace std;

const ll INF = 1e18;
const int MAXN = (int) 5e5;

vector < pair <int, int> > offers[MAXN + 1];

struct SegTree {
    vector <ll> aint;

    inline void init(int n) {
        int pw = 1;
        while(pw <= 2 * n) {
            pw *= 2;
        }
        aint.resize(pw + 1, -INF);
    }

    inline void refresh(int nod) {
        aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
    }

    void update(int nod, int left, int right, int pos, ll val) {
        if(left == right) {
            aint[nod] = max(aint[nod], val);
        }
        else {
            int mid = (left + right) / 2;
            if(pos <= mid) update(2 * nod, left, mid, pos, val);
            else update(2 * nod + 1, mid + 1, right, pos, val);
            refresh(nod);
        }
    }

    ll query(int nod, int left, int right, int l, int r) {
        if(l <= left && right <= r) {
            return aint[nod];
        }
        else {
            int mid = (left + right) / 2;
            ll ans = -INF;
            if(l <= mid) ans = max(ans, query(2 * nod, left, mid, l, r));
            if(mid < r) ans = max(ans, query(2 * nod + 1, mid + 1, right, l, r));
            return ans;
        }
    }
}st1, st2;

int main() {
#if 0
    ifstream cin("A.in");
    ofstream cout("A.out");
#endif
    int i, n, u, d, s;
    //ios::sync_with_stdio(false);
    //cin.tie(0), cout.tie(0);

    //cin >> n >> u >> d >> s;
    scanf("%d%d%d%d" ,&n,&u,&d,&s);
    for(i = 1; i <= n; i++) {
        int t, x, c;
        //cin >> t >> x >> c;
        scanf("%d%d%d" ,&t,&x,&c);
        offers[t].push_back({x, c});
    }

    st1.init(MAXN), st2.init(MAXN);
    st1.update(1, 1, MAXN, s, -u * s);
    st2.update(1, 1, MAXN, s, d * s);

    for(int t = 1; t <= MAXN; t++) {
        sort(offers[t].begin(), offers[t].end());

        int sz = offers[t].size();
        for(i = 1; i < sz; i++) {
            offers[t][i].second += offers[t][i - 1].second;
        }

        vector < pair <int, ll> > upd;
        ll mx = -INF;
        for(i = 0; i < sz; i++) {
            int x = offers[t][i].first;
            mx = max(mx, st2.query(1, 1, MAXN, 1, x) - (i > 0 ? offers[t][i - 1].second : 0));
            upd.push_back({i, mx + offers[t][i].second - d * x});
        }

        mx = -INF;
        for(i = sz - 1; i >= 0; i--) {
            int x = offers[t][i].first;
            mx = max(mx, st1.query(1, 1, MAXN, x, MAXN) + offers[t][i].second);
            upd.push_back({i, mx - (i > 0 ? offers[t][i - 1].second : 0) + x * u});
        }

        mx = -INF;
        for(i = 0; i < sz; i++) {
            int x = offers[t][i].first;
            mx = max(mx, x * (d + u) - (i > 0 ? offers[t][i - 1].second : 0) + st1.query(1, 1, MAXN, x, MAXN));
            ll cur = mx + offers[t][i].second - d * x;
            upd.push_back({i, cur});
        }

        mx = -INF;
        for(i = sz - 1; i >= 0; i--) {
            int x = offers[t][i].first;
            mx = max(mx, offers[t][i].second - x * (d + u) + st2.query(1, 1, MAXN, 1, x));
            ll cur = mx + u * x - (i > 0 ? offers[t][i - 1].second : 0);
            upd.push_back({i, cur});
        }
        for(auto it : upd) {
            int x = offers[t][it.first].first;
            st1.update(1, 1, MAXN, x, it.second - x * u);
            st2.update(1, 1, MAXN, x, it.second + x * d);
        }
    }

    auto get = [&](int a, int b) {
        if(a <= b) return (b - a) * d;
        return (a - b) * u;
    };

    ll ans = -INF;
    for(i = 1; i <= MAXN; i++) {
        ans = max(ans, st1.query(1, 1, MAXN, i, i) + u * i - get(i, s));
        ans = max(ans, st2.query(1, 1, MAXN, i, i) - d * i - get(i, s));
    }
    cout << ans;

    return 0;
}

Compilation message (stderr)

salesman.cpp: In function 'int main()':
salesman.cpp:122:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d%d" ,&n,&u,&d,&s);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
salesman.cpp:126:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d" ,&t,&x,&c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...