Submission #1198776

#TimeUsernameProblemLanguageResultExecution timeMemory
1198776steveonalexSalesman (IOI09_salesman)C++20
100 / 100
526 ms53212 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double wow = (double) ((ull) rng()) / ((ull)(0-1));
    return wow * (r - l) + l;
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const ll INF = 1e18 + 69;

struct SegmentTree{
    int n;
    vector<ll> a;

    SegmentTree(int _n){
        n = _n;
        a.resize(n * 2 + 2, -INF);
    }

    void update(int i, ll v){
        i += n; if (!maximize(a[i], v)) return;
        while(i > 1){
            i >>= 1;
            a[i] = max(a[i * 2], a[i * 2 + 1]);
        }
    }

    ll get(int l, int r){
        l += n; r += n + 1;
        ll ans = -INF;
        while(l < r){
            if (l & 1) maximize(ans, a[l++]);
            if (r & 1) maximize(ans, a[--r]);
            l >>= 1; r >>= 1;
        }
        return ans;
    }
};

void solve(){
    int n, u, d, s; cin >> n >> u >> d >> s;
    vector<array<int, 3>> a(n);
    for(int i = 0; i < n; ++i) 
    for(int j = 0; j < 3; ++j) 
        cin >> a[i][j];

    vector<int> T, X; 
    for(auto i: a) {
        T.push_back(i[0]);
        X.push_back(i[1]);
    }
    X.push_back(s); X.push_back(0);

    remove_dup(T); remove_dup(X);

    for(auto &i: a){
        i[0] = lower_bound(ALL(T), i[0]) - T.begin();
        i[1] = lower_bound(ALL(X), i[1]) - X.begin();
    }
    s = lower_bound(ALL(X), s) - X.begin();

    vector<vector<pair<int, int>>> dih(T.size());
    for(auto i: a){
        dih[i[0]].push_back(make_pair(i[1], i[2]));
    }

    int m = X.size() - 1;
    SegmentTree st_pref(m), st_suff(m);
    // st_pref: away from the stream
    // st_suff: into the stream

    st_pref.update(s, X[s] * d);
    st_suff.update(s, -X[s] * u);
    for(int i = 0; i < (int) T.size(); ++i){
        sort(ALL(dih[i]));
        vector<ll> cost;
        for(pair<int, int> j: dih[i]){
            ll cur = max(st_pref.get(1, j.first) - X[j.first] * d, st_suff.get(j.first, m) + X[j.first] * u);
            cur += j.second;
            cost.push_back(cur);

            // st_pref.update(j.first, cur + X[j.first] * d);
            // st_suff.update(j.first, cur - X[j.first] * u);
        }

        vector<ll> pref = cost, suff = cost;
        for(int j = 1; j < (int) pref.size(); ++j){
            ll dis = X[dih[i][j].first] - X[dih[i][j-1].first];
            maximize(pref[j], pref[j-1] + dih[i][j].second - dis * d);
        }
        for(int j = (int) suff.size() - 1; j >= 1; --j){
            ll dis = X[dih[i][j].first] - X[dih[i][j-1].first];
            maximize(suff[j-1], suff[j] + dih[i][j-1].second - dis * u);
        }

        for(int j = 0; j < (int) pref.size(); ++j){
            ll ma = max(pref[j], suff[j]);
            st_pref.update(dih[i][j].first, ma + X[dih[i][j].first] * d);
            st_suff.update(dih[i][j].first, ma - X[dih[i][j].first] * u);
        }
    }

    ll cur = max(st_pref.get(1, s) - X[s] * d, st_suff.get(s, m) + X[s] * u);

    cout << cur << "\n";
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    clock_t start = clock();

    solve();

    cerr << "Time elapsed: " << clock() - start << " ms\n";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...