#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
#define N 500005
#define pii pair<int, int>
#define inf 2000000005
set<pii> in;
int n, u, d, s, t, l, m;
vector<pii> fair[N];
int dis(int f, int t){
return f <= t ? (t - f) * d : (f - t) * u;
}
int query(int loc){
int res1, res2;
auto it = in.lower_bound({loc, -inf});
res1 = it == in.end() ? -inf : it -> ss - dis(it -> ff, loc);
res2 = it == in.begin() ? -inf : (--it) -> ss - dis(it -> ff, loc);
return max(res1, res2);
}
void update(int loc, int val){
if(query(loc) >= val) return;
auto it = in.insert({loc, val}).ff;
it++;
while(it != in.end() && it -> ss <= val - dis(loc, it -> ff)) it = in.erase(it);
it--;
while(it != in.begin() && (--it) -> ss <= val - dis(loc, it -> ff)) it = in.erase(it);
}
vector<pii> tmp;
int main(){
scanf("%d%d%d%d", &n, &u, &d, &s);
for(int i = 1; i <= n; i++){
scanf("%d%d%d", &t, &l, &m);
fair[t].push_back({l, m});
}
update(s, 0);
int sz;
for(int i = 1; i < N; i++){
sz = fair[i].size();
if(sz == 0) continue;
tmp.clear();
sort(fair[i].begin(), fair[i].end());
for(int j = 0; j < sz; j++){
int res = fair[i][j].ss + query(fair[i][j].ff);
tmp.push_back({res, res});
}
for(int j = 1; j < sz; j++)
tmp[j].ff = max(tmp[j].ff, tmp[j - 1].ff - dis(fair[i][j - 1].ff, fair[i][j].ff) + fair[i][j].ss);
for(int j = sz - 2; j >= 0; j--)
tmp[j].ss = max(tmp[j].ss, tmp[j + 1].ss - dis(fair[i][j + 1].ff, fair[i][j].ff) + fair[i][j].ss);
for(int j = 0; j < sz; j++)
update(fair[i][j].ff, max(tmp[j].ff, tmp[j].ss));
}
printf("%d", query(s));
}
Compilation message
salesman.cpp: In function 'int main()':
salesman.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &n, &u, &d, &s);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
salesman.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &t, &l, &m);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
11996 KB |
Output is correct |
2 |
Correct |
12 ms |
12032 KB |
Output is correct |
3 |
Correct |
13 ms |
12160 KB |
Output is correct |
4 |
Correct |
14 ms |
12160 KB |
Output is correct |
5 |
Correct |
17 ms |
12288 KB |
Output is correct |
6 |
Correct |
27 ms |
12664 KB |
Output is correct |
7 |
Correct |
56 ms |
13668 KB |
Output is correct |
8 |
Correct |
110 ms |
15224 KB |
Output is correct |
9 |
Correct |
161 ms |
16884 KB |
Output is correct |
10 |
Correct |
346 ms |
22620 KB |
Output is correct |
11 |
Correct |
333 ms |
21576 KB |
Output is correct |
12 |
Correct |
450 ms |
24824 KB |
Output is correct |
13 |
Correct |
443 ms |
24724 KB |
Output is correct |
14 |
Correct |
487 ms |
27768 KB |
Output is correct |
15 |
Correct |
561 ms |
29804 KB |
Output is correct |
16 |
Correct |
563 ms |
27896 KB |
Output is correct |
17 |
Correct |
13 ms |
12032 KB |
Output is correct |
18 |
Correct |
13 ms |
12032 KB |
Output is correct |
19 |
Correct |
13 ms |
12160 KB |
Output is correct |
20 |
Correct |
14 ms |
12160 KB |
Output is correct |
21 |
Correct |
14 ms |
12160 KB |
Output is correct |
22 |
Correct |
15 ms |
12180 KB |
Output is correct |
23 |
Correct |
16 ms |
12160 KB |
Output is correct |
24 |
Correct |
17 ms |
12160 KB |
Output is correct |
25 |
Correct |
56 ms |
13048 KB |
Output is correct |
26 |
Correct |
143 ms |
14764 KB |
Output is correct |
27 |
Correct |
192 ms |
17000 KB |
Output is correct |
28 |
Correct |
304 ms |
17144 KB |
Output is correct |
29 |
Correct |
319 ms |
17116 KB |
Output is correct |
30 |
Correct |
322 ms |
18804 KB |
Output is correct |