Submission #649578

#TimeUsernameProblemLanguageResultExecution timeMemory
649578KoalaMuchSalesman (IOI09_salesman)C++14
100 / 100
1922 ms39952 KiB
#include<bits/stdc++.h> using namespace std; const int N = 5e5+4; int sm[N*4][2]; vector<pair<int,int>> fair[N]; int old[N]; int can[N]; void build(int l,int r,int now,int u,int d){ if(l==r){ sm[now][0] = -2e9+l*d; sm[now][1] = -2e9-l*u; return ; } int mid = (l+r) >> 1; build(l,mid,now<<1,u,d),build(mid+1,r,now<<1|1,u,d); sm[now][0] = max(sm[now<<1][0],sm[now<<1|1][0]); sm[now][1] = max(sm[now<<1][1],sm[now<<1|1][1]); } void update(int l,int r,int id,int now,int v,int t,bool rep){ if(l>id||r<id) return ; if(l==r){ if(!rep) sm[now][t] = max(sm[now][t],v); else sm[now][t] = v; return ; } int mid = (l+r) >> 1; update(l,mid,id,now<<1,v,t,rep),update(mid+1,r,id,now<<1|1,v,t,rep); sm[now][t] = max(sm[now<<1][t],sm[now<<1|1][t]); } int query(int l,int r,int ll,int rr,int now,int t){ if(l>rr||r<ll) return -INT_MAX; if(l>=ll&&r<=rr) return sm[now][t]; int mid = (l+r) >> 1; return max(query(l,mid,ll,rr,now<<1,t),query(mid+1,r,ll,rr,now<<1|1,t)); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n,u,d,s,day,loc,pro; cin >> n >> u >> d >> s; for(int i=0;i<n;i++){ cin >> day >> loc >> pro; fair[day].push_back({loc,pro}); } int ans = 0; build(1,500001,1,u,d); update(1,500001,s,1,s*d,0,false); update(1,500001,s,1,-s*u,1,false); for(int i=1;i<=500001;i++){ sort(fair[i].begin(),fair[i].end()); for(int j=0;j<fair[i].size();j++){ loc = fair[i][j].first,pro = fair[i][j].second; old[loc] = query(1,500001,loc,loc,1,0)-loc*d; int fromd = query(1,500001,1,loc,1,0)+pro-loc*d,fromu = query(1,500001,loc,500001,1,1)+pro+loc*u; int bestV = max(fromu,fromd); can[loc] = bestV; //if(loc==80) cout << fromd << " " << fromu << " " << query(1,500001,loc,500001,1,1) << "\n"; update(1,500001,loc,1,bestV+d*loc,0,false); update(1,500001,loc,1,bestV-u*loc,1,false); } for(int j=0;j<fair[i].size();j++){ loc = fair[i][j].first; update(1,500001,loc,1,old[loc]+d*loc,0,true); update(1,500001,loc,1,old[loc]-u*loc,1,true); } for(int j=fair[i].size()-1;j>=0;j--){ loc = fair[i][j].first,pro=fair[i][j].second; int fromd = query(1,500001,1,loc,1,0)+pro-loc*d,fromu = query(1,500001,loc,500001,1,1)+pro+loc*u; int bestV = max(fromu,fromd); can[loc] = max(can[loc],bestV); update(1,500001,loc,1,bestV+d*loc,0,false); update(1,500001,loc,1,bestV-u*loc,1,false); } for(int j=0;j<fair[i].size();j++){ loc = fair[i][j].first; update(1,500001,loc,1,can[loc]+d*loc,0,false); update(1,500001,loc,1,can[loc]-u*loc,1,false); if(loc<s) ans = max(ans,can[loc]-d*(s-loc)); else ans = max(ans,can[loc]-u*(loc-s)); } //if(fair[i].size()) cout << ans << "\n"; } cout << ans << "\n"; return 0; }

Compilation message (stderr)

salesman.cpp: In function 'int main()':
salesman.cpp:51:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         for(int j=0;j<fair[i].size();j++){
      |                     ~^~~~~~~~~~~~~~~
salesman.cpp:61:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |         for(int j=0;j<fair[i].size();j++){
      |                     ~^~~~~~~~~~~~~~~
salesman.cpp:74:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |         for(int j=0;j<fair[i].size();j++){
      |                     ~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...