제출 #985785

#제출 시각아이디문제언어결과실행 시간메모리
985785OAleksaHarbingers (CEOI09_harbingers)C++14
60 / 100
1061 ms27032 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; } double inter(line t) { return (double)(t.n - n) / (k - t.k); } }; long long n, s[N], w[N], dp[N], dep[N]; vector<pair<int, int>> g[N]; vector<pair<line, double>> dq; void dfs(int v, int p) { int l = 0, r = dq.size() - 1, id = -1; while (l <= r) { int mid = (l + r) / 2; if ((double)w[v] >= dq[mid].s) { id = mid; l = mid + 1; } else r = mid - 1; } assert(id != -1); dp[v] = dq[id].f.val(w[v]) + s[v] + w[v] * dep[v]; line ln = {-dep[v], dp[v]}; vector<pair<line, long long>> dead; 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.push_back(dq.back()); dq.pop_back(); } double in = ln.inter(dq.back().f); dq.push_back({ln, in}); for (auto u : g[v]) { if (u.f == p) continue; dep[u.f] = dep[v] + u.s; dfs(u.f, v); } if (dq.back().f.k == ln.k && dq.back().f.n == ln.n) dq.pop_back(); while (!dead.empty()) { dq.push_back(dead.back()); dead.pop_back(); } } 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}, 0}); 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...