Submission #742119

#TimeUsernameProblemLanguageResultExecution timeMemory
742119siewjhHarbingers (CEOI09_harbingers)C++17
70 / 100
172 ms65536 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int MAXN = 100'005; vector<pair<int, ll>> adj[MAXN]; ll prep[MAXN], tim[MAXN], dp[MAXN], dist = 0; struct line{ ll m, c; ll eval(ll x){ return m * x + c; } }; deque<line> dq; vector<line> changes[MAXN]; ld poi(line a, line b) { return (ld)(a.c - b.c) / (b.m - a.m); } void insert(line a, int ind) { while (dq.size() >= 2 && poi(dq.back(), a) <= poi(dq.back(), dq[dq.size() - 2])) { changes[ind].push_back(dq.back()); dq.pop_back(); } dq.push_back(a); } ll query(ll x){ if (dq.empty()) return 0; int l = 0, r = dq.size() - 2, ans = dq.size() - 1; while (l <= r){ int m = (l + r) >> 1; if (dq[m].eval(x) <= dq[m + 1].eval(x)){ ans = m; r = m - 1; } else l = m + 1; } return dq[ans].eval(x); } void rollback(int ind){ dq.pop_back(); for (int i = changes[ind].size() - 1; i >= 0; i--) dq.push_back(changes[ind][i]); changes[ind].clear(); } void dfs(int x, int par){ if (x == 1) dp[x] = 0; else dp[x] = prep[x] + dist * tim[x] + query(tim[x]); insert({-dist, dp[x]}, x); for (auto nxt : adj[x]){ int nn = nxt.first; ll nd = nxt.second; if (nn == par) continue; dist += nd; dfs(nn, x); dist -= nd; } rollback(x); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int nodes; cin >> nodes; for (int i = 1; i < nodes; i++){ int a, b; ll d; cin >> a >> b >> d; adj[a].push_back({b, d}); adj[b].push_back({a, d}); } for (int i = 2; i <= nodes; i++) cin >> prep[i] >> tim[i]; dfs(1, -1); for (int i = 2; i <= nodes; i++) cout << dp[i] << ' '; }
#Verdict Execution timeMemoryGrader output
Fetching results...