Submission #146187

#TimeUsernameProblemLanguageResultExecution timeMemory
146187popovicirobertSalesman (IOI09_salesman)C++14
77 / 100
977 ms35832 KiB
#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 int INF = 2e9; const int MAXN = (int) 5e5; vector < pair <int, int> > offers[MAXN + 1]; struct SegTree { int aint[2 * MAXN + 1]; inline void init() { fill(aint, aint + 2 * MAXN, -INF); } inline void update(int pos, int val) { pos += MAXN - 1; aint[pos] = max(aint[pos], val); while(pos > 1) { pos /= 2; aint[pos] = max(aint[2 * pos], aint[2 * pos + 1]); } } inline int query(int l, int r) { l += MAXN - 1, r += MAXN; int ans = -INF; while(l < r) { if(l & 1) { ans = max(ans, aint[l++]); } if(r & 1) { ans = max(ans, aint[--r]); } l >>= 1, r >>= 1; } return ans; } }st1, st2; 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; scanf("%d%d%d%d" ,&n,&u,&d,&s); for(i = 1; i <= n; i++) { int t, x, c; //cin >> t >> x >> c; scanf("%d%d%d" ,&t,&x,&c); offers[t].push_back({x, c}); } st1.init(), st2.init(); st1.update(s, -u * s); st2.update(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; } unordered_map <int, int> upd; int mx = -INF; for(i = 0; i < sz; i++) { int x = offers[t][i].first; mx = max(mx, st2.query(1, x) - (i > 0 ? offers[t][i - 1].second : 0)); ll cur = mx + offers[t][i].second - d * x; if(upd.find(x) == upd.end() || upd[x] < cur) { upd[x] = cur; } } mx = -INF; for(i = sz - 1; i >= 0; i--) { int x = offers[t][i].first; mx = max(mx, st1.query(x, MAXN) + offers[t][i].second); ll cur = mx - (i > 0 ? offers[t][i - 1].second : 0) + x * u; if(upd.find(x) == upd.end() || upd[x] < cur) { upd[x] = cur; } } 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(x, MAXN)); ll cur = mx + offers[t][i].second - d * x; if(upd.find(x) == upd.end() || upd[x] < cur) { upd[x] = 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, x)); ll cur = mx + u * x - (i > 0 ? offers[t][i - 1].second : 0); if(upd.find(x) == upd.end() || upd[x] < cur) { upd[x] = cur; } } for(auto it : upd) { st1.update(it.first, it.second - it.first * u); st2.update(it.first, it.second + it.first * d); } } auto get = [&](int a, int b) { if(a <= b) return (b - a) * d; return (a - b) * u; }; int ans = -INF; for(i = 1; i <= MAXN; i++) { ans = max(ans, st1.query(i, i) + u * i - get(i, s)); ans = max(ans, st2.query(i, i) - d * i - get(i, s)); } cout << ans; return 0; }

Compilation message (stderr)

salesman.cpp: In function 'int main()':
salesman.cpp:114:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d%d" ,&n,&u,&d,&s);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
salesman.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d" ,&t,&x,&c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...