# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
733424 | penguin133 | Salesman (IOI09_salesman) | C++17 | 153 ms | 65536 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, u, d, s;
int dp[500005];
pii A[500005];
struct node{
int s, e, val;
node *l, *r;
node(int _s, int _e){
s = _s, e = _e, val = -1e18;
if(s != e)l = new node(s, (s + e) >> 1), r = new node(((s + e) >> 1)+1, e);
}
void upd(int p, int v){
if(s == e)val = max(val, v);
else{
if(p <= (s + e) >> 1)l->upd(p, v);
else r->upd(p, v);
val = max(l->val, r->val);
}
}
int qry(int a, int b){
if(s == a && b == e)return val;
else if(b <= (s + e) >> 1)return l->qry(a, b);
else if(a > (s + e) >> 1)return r->qry(a, b);
else return max(l->qry(a, (s + e) >> 1), r->qry(((s + e) >> 1) +1, b));
}
}*dwn, *up;
void solve(){
cin >> n >> u >> d >> s;
int mx = s;
for(int i=1;i<=n;i++){
cin >> A[i].fi >> A[i].se.fi >> A[i].se.se;
mx = max(mx, A[i].se.fi);
}
dwn = new node(1, mx);
up = new node(1, mx);
sort(A+1, A+n+1);
A[0].se.fi = s;
A[n+1].se.fi = s;
dwn->upd(s, -d * s);
up->upd(s, u * s);
for(int i=1;i<=n+1;i++){
int top = dwn->qry(A[i].se.fi, mx) + d * A[i].se.fi + A[i].se.se;
int bot = up->qry(1, A[i].se.fi) - u * A[i].se.fi + A[i].se.se;
dp[i] = max(top, bot);
/*
for(int j=i-1;j>=0;j--){
int cst;
if(A[j].se.fi > A[i].se.fi)cst = d * (A[j].se.fi - A[i].se.fi);
else cst = u * (A[i].se.fi - A[j].se.fi);
dp[i] = max(dp[i], dp[j] - cst + A[i].se.se);
}
*/
dwn->upd(A[i].se.fi, dp[i] - d * A[i].se.fi);
up->upd(A[i].se.fi, dp[i] + u * A[i].se.fi);
}
cout << dp[n+1];
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |