Submission #1041886

#TimeUsernameProblemLanguageResultExecution timeMemory
1041886onbertSalesman (IOI09_salesman)C++17
0 / 100
389 ms65536 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 5e5 + 5, maxN = 2e6 + 5, INF = 1e18; int n, u, d, s; vector<pair<int,int>> a[maxn+1]; int seg[maxN][2]; void build(int id, int l, int r) { if (l==r) { if (l==s) seg[id][0] = -(l-1) * u, seg[id][1] = -(maxn-l) * d; else seg[id][0] = seg[id][1] = -INF; return; } int mid = (l+r)/2; build(id*2, l, mid); build(id*2+1, mid+1, r); seg[id][0] = max(seg[id*2][0], seg[id*2+1][0]); seg[id][1] = max(seg[id*2][1], seg[id*2+1][1]); } void update(int id, int l, int r, int target, int val) { if (r<target || target<l) return; if (l==r) { seg[id][0] = val - (l-1) * u, seg[id][1] = -(maxn-l) * d; return; } int mid = (l+r)/2; update(id*2, l, mid, target, val); update(id*2+1, mid+1, r, target, val); seg[id][0] = max(seg[id*2][0], seg[id*2+1][0]); seg[id][1] = max(seg[id*2][1], seg[id*2+1][1]); } int qry(int id, int l, int r, int findl, int findr, int j) { if (r<findl || findr<l) return -INF; if (findl<=l && r<=findr) return seg[id][j]; int mid = (l+r)/2; return max(qry(id*2, l, mid, findl, findr, j), qry(id*2+1, mid+1, r, findl, findr, j)); } signed main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> u >> d >> s; for (int i=1;i<=n;i++) { int t, pos, val; cin >> t >> pos >> val; a[t].push_back({pos, val}); } for (int i=1;i<=maxn;i++) { a[i].push_back({0, -INF}); a[i].push_back({maxn+1, -INF}); sort(a[i].begin(), a[i].end()); } build(1, 1, maxn); for (int i=1;i<=maxn;i++) { int sz = a[i].size(); if (sz==2) continue; vector<int> ans0(sz), ans1(sz); for (int j=1;j<=sz-2;j++) { int val1 = qry(1, 1, maxn, a[i][j-1].first+1, a[i][j].first, 1) + (maxn-a[i][j].first) * d; int val2 = qry(1, 1, maxn, a[i][j].first, a[i][j+1].first-1, 0) + (a[i][j].first-1) * u; ans0[j] = ans1[j] = max(val1, val2); } // cout << i << endl; // for (int j=1;j<=sz-2;j++) cout << ans0[j] << " "; cout << endl; ans1[0] = ans0[sz-1] = -INF; for (int j=1;j<=sz-2;j++) ans1[j] = max(ans1[j-1] + (a[i][j].first - a[i][j-1].first) * d, ans1[j]) + a[i][j].second; for (int j=sz-2;j>=1;j--) ans0[j] = max(ans0[j+1] + (a[i][j+1].first - a[i][j].first) * u, ans0[j]) + a[i][j].second; for (int j=1;j<=sz-2;j++) update(1, 1, maxn, a[i][j].first, max(ans1[j], ans0[j])); } int ans = 0; for (int i=1;i<=maxn;i++) { int val = qry(1, 1, maxn, i, i, 0) + (i-1) * u; if (i<s) val -= (s-i) * d; else val -= (i-s) * u; ans = max(val, ans); } cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...