# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
379314 | ChrisT | Salesman (IOI09_salesman) | C++17 | 1478 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;
const int MN = 5e5 + 5;
struct Seg {
long long tree[MN << 1]; int n;
Seg() {n = MN; memset(tree,-0x3f,sizeof tree);}
void update (int i, long long v) {
for (tree[i += n-1] = v; i > 1; i >>= 1)
tree[i>>1] = max(tree[i],tree[i^1]);
}
long long query (int l, int r) {
long long ret = -1e18;
for (l += n-1, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) ret = max(ret,tree[l++]);
if (r&1) ret = max(ret,tree[--r]);
}
return ret;
}
} segL, segR, segL2, segR2;
vector<array<int,2>> atT[MN];
int main () {
int n,u,d,s;
scanf ("%d %d %d %d",&n,&u,&d,&s);
while (n--) {
int t,l,m; scanf ("%d %d %d",&t,&l,&m);
atT[t].push_back({l,m});
}
atT[MN-1].push_back({s,0});
segL.update(s,d * s); segR.update(s,-u*s);
for (int t = 1; t < MN; t++) if (!atT[t].empty()) {
int m = (int)atT[t].size();
vector<long long> dp1(m), dp2(m);
sort(atT[t].begin(),atT[t].end());
for (int i = 0; i < m; i++) {
auto [l,c] = atT[t][i];
dp1[i] = max(max(segL.query(1,l),segL2.query(1,l)) - d * l + c,max(segR.query(l,MN),segR2.query(l,MN)) + u * l + c);
segL2.update(l,dp1[i] + d * l); segR2.update(l,dp1[i] - u * l);
}
for (int i = 0; i < m; i++) segL2.update(atT[t][i][0],-1e18), segR2.update(atT[t][i][0],-1e18);
for (int i = m-1; i >= 0; i--) {
auto [l,c] = atT[t][i];
dp2[i] = max(max(segL.query(1,l),segL2.query(1,l)) - d * l + c,max(segR.query(l,MN),segR2.query(l,MN)) + u * l + c);
segL2.update(l,dp2[i] + d * l); segR2.update(l,dp2[i] - u * l);
}
for (int i = 0; i < m; i++) {
auto [l,c] = atT[t][i];
segL2.update(l,-1e18); segR2.update(l,-1e18);
segL.update(l,max(dp1[i],dp2[i]) + d * l); segR.update(l,max(dp1[i],dp2[i]) - u * l);
}
if (t + 1 == MN) return !printf ("%lld\n",dp1[0]);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |