# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1043429 | onbert | Salesman (IOI09_salesman) | C++17 | 451 ms | 57168 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;
#define ll long long
const ll maxn = 5e5 + 5, maxN = 2e6 + 5, INF = 1e15;
int n, u, d, s;
vector<pair<int,int>> a[maxn+1];
ll 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] = val - (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]);
}
ll 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<ll> ans0(sz), ans1(sz);
for (int j=1;j<=sz-2;j++) {
ll val1 = qry(1, 1, maxn, 1, a[i][j].first, 1) + (maxn-a[i][j].first) * d;
ll val2 = qry(1, 1, maxn, a[i][j].first, maxn, 0) + (a[i][j].first-1) * u;
ans0[j] = ans1[j] = max(val1, val2);
}
ll last = -maxn * d;
for (int j=1;j<=sz-2;j++) {
last = max(last - (ll)(a[i][j].first - a[i][j-1].first) * u, -((ll)maxn - (ll)a[i][j].first) * d) + a[i][j].second;
ans1[j] += last + (maxn-a[i][j].first) * d;
// cout << last + (maxn-a[i][j].first) * d << endl;
}
last = -maxn * u;
for (int j=sz-2;j>=1;j--) {
last = max(last - (ll)(a[i][j+1].first - a[i][j].first) * d, -((ll)a[i][j].first - 1) * u) + a[i][j].second;
ans0[j] += last + (a[i][j].first-1) * u;
}
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 + a[i][j].second, ans1[j]);
for (int j=sz-2;j>=1;j--) ans0[j] = max(ans0[j+1] - (a[i][j+1].first - a[i][j].first) * u + a[i][j].second, ans0[j]);
for (int j=1;j<=sz-2;j++) update(1, 1, maxn, a[i][j].first, max(ans1[j], ans0[j]));
}
ll ans = 0;
for (int i=1;i<=maxn;i++) {
ll 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 time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |