Submission #1000941

#TimeUsernameProblemLanguageResultExecution timeMemory
1000941toilanvdSalesman (IOI09_salesman)C++14
100 / 100
557 ms65396 KiB
#include <cstdio> #include <algorithm> #include <vector> #include <iostream> #define x first #define y second using namespace std; typedef long long ll; typedef pair<ll,ll> ii; const ll INF= 1000000000000005LL, cons= 500001; ll n, upcost, downcost, sta; vector<ii> a[500005]; ll g[500005][2], f[500005][2], s[500005]; ll d[2][2000505]; void update(ll type, ll k, ll p, ll q, ll x, ll z){ if(p>q || p>x || q<x) return; if(p==x && x==q){ d[type][k]= z; return; } update(type,2*k,p,(p+q)/2,x,z); update(type,2*k+1,(p+q)/2+1,q,x,z); d[type][k]= max(d[type][2*k], d[type][2*k+1]); } ll get(ll type, ll k, ll p, ll q, ll x, ll y){ if(p>q || p>y || q<x) return -INF; if(x<=p && q<=y) return d[type][k]; return max(get(type,2*k,p,(p+q)/2,x,y),get(type,2*k+1,(p+q)/2+1,q,x,y)); } int main(){ ios_base::sync_with_stdio(false); cin >> n >> upcost >> downcost >> sta; a[0].push_back(ii(sta,0)); a[cons].push_back(ii(sta,0)); for(ll i=1;i<=n;i++){ ll x, y, z; cin >> x >> y >> z; a[x].push_back(ii(y,z)); } for(ll i=1;i<=4*cons;i++){d[0][i]= -INF; d[1][i]= -INF;} update(0,1,1,cons,sta,upcost*sta); update(1,1,1,cons,sta,-downcost*sta); for(ll i=cons-1;i>=0;i--){ if(a[i].empty()) continue; sort(a[i].begin(),a[i].end()); ll maxi= -INF; for(ll j=0;j<(ll)a[i].size();j++){ s[j+1]= s[j]+a[i][j].y; maxi= max(maxi, -s[j]+(upcost+downcost)*a[i][j].x); g[j+1][0]= maxi+(s[j+1]-(upcost+downcost)*a[i][j].x); } maxi= -INF; for(ll j=(ll)a[i].size()-1;j>=0;j--){ maxi= max(maxi, s[j+1]-(upcost+downcost)*a[i][j].x); g[j+1][1]= maxi+(-s[j]+(upcost+downcost)*a[i][j].x); } for(ll j=0;j<(ll)a[i].size();j++){ f[j+1][1]= get(0,1,1,cons,1,a[i][j].x)-upcost*a[i][j].x; f[j+1][1]= max(f[j+1][1], get(1,1,1,cons,a[i][j].x,cons)+downcost*a[i][j].x); } maxi= -INF; for(ll j=0;j<(ll)a[i].size();j++){ maxi= max(maxi, f[j+1][1]+upcost*a[i][j].x-s[j]); f[j+1][0]= maxi+(-upcost*a[i][j].x+s[j]+g[j+1][1]); } maxi= -INF; for(ll j=(ll)a[i].size()-1;j>=0;j--){ maxi= max(maxi, f[j+1][1]-downcost*a[i][j].x+s[j+1]); f[j+1][0]= max(f[j+1][0], maxi+(downcost*a[i][j].x-s[j+1]+g[j+1][0])); } for(ll j=0;j<(ll)a[i].size();j++){ update(0,1,1,cons,a[i][j].x,f[j+1][0]+upcost*a[i][j].x); update(1,1,1,cons,a[i][j].x,f[j+1][0]-downcost*a[i][j].x); } } cout << f[1][0] << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...