Submission #146296

#TimeUsernameProblemLanguageResultExecution timeMemory
146296popovicirobertSalesman (IOI09_salesman)C++14
100 / 100
496 ms43672 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 int INF = 2e9;
const int MAXN = (int) 5e5 + 5;

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

struct Fenwick {
    vector <int> aib;
    int n;

    inline void init(int _n) {
        n = _n;
        aib.resize(n + 1, -INF);
    }

    inline void update(int pos, int val) {
        for(int i = pos; i <= n; i += lsb(i)) {
            aib[i] = max(aib[i], val);
        }
    }

    inline int query(int pos) {
        int ans = -INF;
        for(int i = pos; i > 0; i -= lsb(i)) {
            ans = max(ans, aib[i]);
        }
        return ans;
    }
};

int dp[MAXN + 1], aux[MAXN + 1];

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;
    for(i = 1; i <= n; i++) {
        int t, x, c;
        cin >> t >> x >> c;
        offers[t].push_back({x, c});
    }

    fill(dp, dp + MAXN + 1, -INF);
    dp[s] = 0;

    Fenwick fen1, fen2;
    fen1.init(MAXN), fen2.init(MAXN);
    fen1.update(MAXN - s, -u * s);
    fen2.update(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;
        }
        for(auto it : offers[t]) {
            aux[it.first] = -INF;
        }
        for(auto it : offers[t]) {
            aux[it.first] = max(aux[it.first], max(-it.first * d + fen2.query(it.first), it.first * u + fen1.query(MAXN - it.first)));
        }

        int mx = -INF;
        for(i = sz - 1; i >= 0; i--) {
            int x = offers[t][i].first;
            mx = max(mx, aux[x] - u * x + offers[t][i].second);
            dp[x] = max(dp[x], mx - (i > 0 ? offers[t][i - 1].second : 0) + u * x);
        }
        mx = -INF;
        for(i = 0; i < sz; i++) {
            int x = offers[t][i].first;
            mx = max(mx, aux[x] + d * x - (i > 0 ? offers[t][i - 1].second : 0));
            dp[x] = max(dp[x], mx - d * x + offers[t][i].second);
        }

        for(auto it : offers[t]) {
            int x = it.first;
            fen1.update(MAXN - x, dp[x] - u * x);
            fen2.update(x, dp[x] + d * x);
        }
    }

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

    int ans = -INF;
    for(i = 1; i <= MAXN; i++) {
        ans = max(ans, dp[i] - get(i, s));
    }
    cout << ans;

    return 0;
}

/*
dp[t][x]
x1 <= x2

dp[t + 1][x2] = dp[t][x] + sp[t + 1][x2] - sp[t + 1][x1 - 1] - U * (x - x1) - D * (x2 - x1)
dp[t + 1][x1] = dp[t][x] + sp[t + 1][x2] - sp[t + 1][x1 - 1] - D * (x2 - x) - U * (x2 - x1)

(x1 <= x)
dp[t + 1][x2] =

sp[t + 1][x2] - D * x2
dp[t][x] - U * x
-sp[t + 1][x1 - 1] + x1 * (D + U)




(x2 >= x)
dp[t + 1][x1] =

sp[t + 1][x2] - x2 * (D + U)
dp[t][x] + D * x
-sp[t + 1][x1 - 1] + U * x1
*/
#Verdict Execution timeMemoryGrader output
Fetching results...