# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
862012 | 12345678 | Salesman (IOI09_salesman) | C++17 | 618 ms | 48084 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 nx=5e5+5;
int n, u, d, s, x, y, z;
vector<pair<int, int>> t[nx];
struct segtree
{
int d[4*nx];
void build(int l, int r, int i)
{
if (l==r) return void(d[i]=-2e9);
int md=(l+r)/2;
build(l, md, 2*i);
build(md+1, r, 2*i+1);
d[i]=max(d[2*i], d[2*i+1]);
}
void update(int l, int r, int i, int idx, int vl)
{
if (idx<l||r<idx) return;
if (l==r) return void(d[i]=max(d[i], vl));
int md=(l+r)/2;
update(l, md, 2*i, idx, vl);
update(md+1, r, 2*i+1, idx, vl);
d[i]=max(d[2*i], d[2*i+1]);
}
int query(int l, int r, int i, int ql, int qr)
{
if (r<ql||qr<l) return INT_MIN;
if (ql<=l&&r<=qr) return d[i];
int md=(l+r)/2;
return max(query(l, md, 2*i, ql, qr), query(md+1, r, 2*i+1, ql, qr));
}
} up, dn;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>u>>d>>s;
for (int i=0; i<n; i++) cin>>x>>y>>z, t[x].push_back({y, z});
up.build(1, nx-1, 1); dn.build(1, nx-1, 1);
up.update(1, nx-1, 1, s, -s*u); dn.update(1, nx-1, 1, s, s*d);
for (int i=1; i<nx; i++)
{
sort(t[i].begin(), t[i].end());
vector<int> cal(t[i].size()), tmp(t[i].size()), rtmp(t[i].size());
for (int j=0; j<t[i].size(); j++)
{
int y=t[i][j].first, z=t[i][j].second;
int qup=up.query(1, nx-1, 1, y, nx-1);
int qdn=dn.query(1, nx-1, 1, 1, y);
cal[j]=tmp[j]=rtmp[j]=max(qup+y*u, qdn-y*d)+z;
}
for (int j=1; j<t[i].size(); j++) tmp[j]=max(tmp[j], tmp[j-1]+(t[i][j].first-t[i][j-1].first)*(-d)+t[i][j].second);
for (int j=(int)t[i].size()-2; j>=0; j--) rtmp[j]=max(rtmp[j], rtmp[j+1]+(t[i][j+1].first-t[i][j].first)*(-u)+t[i][j].second);
for (int j=0; j<t[i].size(); j++)
{
int y=t[i][j].first;
//cout<<j<<' '<<tmp[j]<<' '<<rtmp[j]<<'\n';
up.update(1, nx-1, 1, y, max(tmp[j], rtmp[j])-y*u);
dn.update(1, nx-1, 1, y, max(tmp[j], rtmp[j])+y*d);
}
}
cout<<max(up.query(1, nx-1, 1, s, nx-1)+s*u, dn.query(1, nx-1, 1, 1, s)-s*d);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |