#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
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++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
20308 KB |
Output is correct |
2 |
Correct |
16 ms |
20180 KB |
Output is correct |
3 |
Correct |
18 ms |
20280 KB |
Output is correct |
4 |
Correct |
22 ms |
20308 KB |
Output is correct |
5 |
Correct |
30 ms |
20424 KB |
Output is correct |
6 |
Correct |
87 ms |
24744 KB |
Output is correct |
7 |
Correct |
204 ms |
25740 KB |
Output is correct |
8 |
Correct |
395 ms |
27308 KB |
Output is correct |
9 |
Correct |
591 ms |
28752 KB |
Output is correct |
10 |
Correct |
1010 ms |
33676 KB |
Output is correct |
11 |
Correct |
1161 ms |
33568 KB |
Output is correct |
12 |
Correct |
1484 ms |
36704 KB |
Output is correct |
13 |
Correct |
1492 ms |
36704 KB |
Output is correct |
14 |
Correct |
1864 ms |
39952 KB |
Output is correct |
15 |
Correct |
1690 ms |
39832 KB |
Output is correct |
16 |
Correct |
1922 ms |
39832 KB |
Output is correct |
17 |
Correct |
16 ms |
20180 KB |
Output is correct |
18 |
Correct |
19 ms |
20312 KB |
Output is correct |
19 |
Correct |
19 ms |
20308 KB |
Output is correct |
20 |
Correct |
24 ms |
20308 KB |
Output is correct |
21 |
Correct |
23 ms |
20308 KB |
Output is correct |
22 |
Correct |
30 ms |
20388 KB |
Output is correct |
23 |
Correct |
29 ms |
20316 KB |
Output is correct |
24 |
Correct |
31 ms |
20420 KB |
Output is correct |
25 |
Correct |
347 ms |
25388 KB |
Output is correct |
26 |
Correct |
672 ms |
26188 KB |
Output is correct |
27 |
Correct |
1129 ms |
27544 KB |
Output is correct |
28 |
Correct |
1221 ms |
27972 KB |
Output is correct |
29 |
Correct |
1678 ms |
29052 KB |
Output is correct |
30 |
Correct |
1598 ms |
29248 KB |
Output is correct |