Submission #446701

#TimeUsernameProblemLanguageResultExecution timeMemory
446701JerryLiu06Salesman (IOI09_salesman)C++17
100 / 100
333 ms11972 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
using ld = long double;
using db = double;
using str = string;
 
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
 
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
 
#define mp make_pair
#define f first
#define s second
 
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert 
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
 
#define lb lower_bound
#define ub upper_bound
 
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
 
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
 
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
 
template<class T> void remDup(vector<T>& v) { sor(v); v.erase(unique(all(v)), v.end()); }
 
const int MOD = 1e9 + 7;
const int MX = 5e5 + 10;
const ll INF = 1e18;

int N, U, D, S; vector<array<int, 3>> A; int down[MX], up[MX], bst[MX];

void updUp(int ind, int val) { while (ind < MX) { ckmax(up[ind], val); ind += (ind & -ind); } }
int querUp(int ind) { int res = -MOD; while (ind > 0) { ckmax(res, up[ind]); ind -= (ind & -ind); } return res; }

void updDown(int ind, int val) { while (ind > 0) { ckmax(down[ind], val); ind -= (ind & -ind); } }
int querDown(int ind) { int res = -MOD; while (ind < MX) { ckmax(res, down[ind]); ind += (ind & -ind); } return res; }

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    
    cin >> N >> U >> D >> S; A.rsz(N); F0R(i, N) cin >> A[i][0] >> A[i][1] >> A[i][2];
    
    F0R(i, MX) down[i] = -MOD, up[i] = -MOD; sor(A); updDown(S, -S * D); updUp(S, S * U);
    
    for (int L = 0; L < N; ) {
        int R = L; while (R < N && A[R][0] == A[L][0]) R++;
        
        FOR(i, L, R) {
            bst[i] = -MOD; 
            
            ckmax(bst[i], querDown(A[i][1]) + A[i][1] * D);
            ckmax(bst[i], querUp(A[i][1]) - A[i][1] * U); 
        }
        int prefMax = -MOD; int pref = 0;

        FOR(i, L, R) {
            ckmax(prefMax, bst[i] + A[i][1] * U - pref);

            pref += A[i][2];

            updDown(A[i][1], prefMax + pref - A[i][1] * U - A[i][1] * D);

            updUp(A[i][1], prefMax + pref - A[i][1] * U + A[i][1] * U);
        }
        int suffMax = -MOD; int suff = 0;

        ROF(i, L, R) {
            ckmax(suffMax, bst[i] - A[i][1] * D - suff);

            suff += A[i][2];

            updDown(A[i][1], suffMax + suff + A[i][1] * D - A[i][1] * D);

            updUp(A[i][1], suffMax + suff + A[i][1] * D + A[i][1] * U);
        }
        L = R;
    }
    cout << max(querDown(S) + S * D, querUp(S) - S * U);
}
#Verdict Execution timeMemoryGrader output
Fetching results...