# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
146177 | popovicirobert | Salesman (IOI09_salesman) | C++14 | 1089 ms | 60636 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>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#if 0
const int MOD = ;
inline int lgput(int a, int b) {
int ans = 1;
while(b > 0) {
if(b & 1) ans = (1LL * ans * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return ans;
}
inline void mod(int &x) {
if(x >= MOD)
x -= MOD;
}
inline void add(int &x, int y) {
x += y;
mod(x);
}
inline void sub(int &x, int y) {
x += MOD - y;
mod(x);
}
inline void mul(int &x, int y) {
x = (1LL * x * y) % MOD;
}
inline int inv(int x) {
return lgput(x, MOD - 2);
}
#endif
#if 0
int fact[], invfact[];
inline void prec(int n) {
fact[0] = 1;
for(int i = 1; i <= n; i++) {
fact[i] = (1LL * fact[i - 1] * i) % MOD;
}
invfact[n] = lgput(fact[n], MOD - 2);
for(int i = n - 1; i >= 0; i--) {
invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
}
}
inline int comb(int n, int k) {
if(n < k) return 0;
return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif
using namespace std;
const ll INF = 1e18;
const int MAXN = (int) 5e5;
vector < pair <int, int> > offers[MAXN + 1];
struct SegTree {
vector <ll> aint;
inline void init(int n) {
int pw = 1;
while(pw <= 2 * n) {
pw *= 2;
}
aint.resize(pw + 1, -INF);
}
inline void refresh(int nod) {
aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
}
void update(int nod, int left, int right, int pos, ll val) {
if(left == right) {
aint[nod] = max(aint[nod], val);
}
else {
int mid = (left + right) / 2;
if(pos <= mid) update(2 * nod, left, mid, pos, val);
else update(2 * nod + 1, mid + 1, right, pos, val);
refresh(nod);
}
}
ll query(int nod, int left, int right, int l, int r) {
if(l <= left && right <= r) {
return aint[nod];
}
else {
int mid = (left + right) / 2;
ll ans = -INF;
if(l <= mid) ans = max(ans, query(2 * nod, left, mid, l, r));
if(mid < r) ans = max(ans, query(2 * nod + 1, mid + 1, right, l, r));
return ans;
}
}
}st1, st2, st3, st4;
int main() {
#if 0
ifstream cin("A.in");
ofstream cout("A.out");
#endif
int i, n, u, d, s;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> u >> d >> s;
for(i = 1; i <= n; i++) {
int t, x, c;
cin >> t >> x >> c;
offers[t].push_back({x, c});
}
st1.init(MAXN), st2.init(MAXN);
st3.init(MAXN), st4.init(MAXN);
st1.update(1, 1, MAXN, s, -u * s);
st2.update(1, 1, MAXN, s, d * s);
st3.update(1, 1, MAXN, s, -u * s);
st4.update(1, 1, MAXN, s, d * s);
for(int t = 1; t <= MAXN; t++) {
sort(offers[t].begin(), offers[t].end());
int sz = offers[t].size();
for(i = 1; i < sz; i++) {
offers[t][i].second += offers[t][i - 1].second;
}
vector < pair <int, ll> > upd;
for(i = 0; i < sz; i++) {
int x = offers[t][i].first;
ll cur = st4.query(1, 1, MAXN, 1, x) - x * d + offers[t][i].second;
//cerr << st4.query(1, 1, MAXN, 1, x) - x * d + offers[t][i].second << "\n";
upd.push_back({i, cur});
cur = st3.query(1, 1, MAXN, x, MAXN) + x * u - (i > 0 ? offers[t][i - 1].second : 0);
upd.push_back({i, cur});
}
ll mx = -INF;
for(i = 0; i < sz; i++) {
int x = offers[t][i].first;
mx = max(mx, x * (d + u) - (i > 0 ? offers[t][i - 1].second : 0) + st1.query(1, 1, MAXN, x, MAXN));
ll cur = mx + offers[t][i].second - d * x;
upd.push_back({i, cur});
}
mx = -INF;
for(i = sz - 1; i >= 0; i--) {
int x = offers[t][i].first;
mx = max(mx, offers[t][i].second - x * (d + u) + st2.query(1, 1, MAXN, 1, x));
ll cur = mx + u * x - (i > 0 ? offers[t][i - 1].second : 0);
upd.push_back({i, cur});
}
for(auto it : upd) {
int x = offers[t][it.first].first;
st1.update(1, 1, MAXN, x, it.second - x * u);
st2.update(1, 1, MAXN, x, it.second + x * d);
//cerr << x << " " << it.second << "\n";
st3.update(1, 1, MAXN, x, it.second - x * u + offers[t][it.first].second);
st4.update(1, 1, MAXN, x, it.second + x * d - (it.first > 0 ? offers[t][it.first - 1].second : 0));
}
}
auto get = [&](int a, int b) {
if(a <= b) return (b - a) * d;
return (a - b) * u;
};
ll ans = -INF;
for(i = 1; i <= MAXN; i++) {
ans = max(ans, st1.query(1, 1, MAXN, i, i) + u * i - get(i, s));
ans = max(ans, st2.query(1, 1, MAXN, i, i) - d * i - get(i, s));
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |