Submission #985798

#TimeUsernameProblemLanguageResultExecution timeMemory
985798OAleksaHarbingers (CEOI09_harbingers)C++14
70 / 100
59 ms32324 KiB
#include <bits/stdc++.h> #define f first #define s second using namespace std; const int N = 1e5 + 69; struct line { long long k, n; line(long long _k, long long _n) { k = _k; n = _n; } long long val(long long x) { return x * k + n; } }; int n, s[N], w[N]; long long dp[N], dep[N]; vector<pair<int, int>> g[N]; vector<pair<line, int>> dq; int dead[2 * N], lst; void dfs(int v, int p) { int l = 1, r = dq.size() - 1, id = 0; while (l <= r) { int mid = (l + r) / 2; if (dq[mid].f.val(w[v]) <= dq[mid - 1].f.val(w[v])) { id = mid; l = mid + 1; } else r = mid - 1; } dp[v] = dq[id].f.val(w[v]) + s[v] + 1ll * w[v] * dep[v]; line ln = {-dep[v], dp[v]}; int pr = lst, top = lst; while (dq.size() > 1 && (ln.k - dq.back().f.k) * (dq.back().f.n - dq[dq.size() - 2].f.n) <= (dq.back().f.n - ln.n) * (dq[dq.size() - 2].f.k - dq.back().f.k)) { dead[top] = dq.back().s; top++; lst++; dq.pop_back(); } dq.push_back({ln, v}); for (auto u : g[v]) { if (u.f == p) continue; dep[u.f] = dep[v] + u.s; dfs(u.f, v); } dq.pop_back(); while (top > pr) { --top; auto x = dead[top]; dq.push_back({{-dep[x], dp[x]}, x}); } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tt = 1; //cin >> tt; while (tt--) { cin >> n; for (int i = 1;i <= n - 1;i++) { int a, b, c; cin >> a >> b >> c; g[a].push_back({b, c}); g[b].push_back({a, c}); } for (int i = 2;i <= n;i++) cin >> s[i] >> w[i]; dq.push_back({{0, 0}, 1}); for (auto j : g[1]) { dep[j.f] = j.s; dfs(j.f, 1); } for (int i = 2;i <= n;i++) cout << dp[i] << ' '; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...