제출 #705181

#제출 시각아이디문제언어결과실행 시간메모리
705181finn__Harbingers (CEOI09_harbingers)C++17
100 / 100
190 ms21076 KiB
#include <bits/stdc++.h> using namespace std; struct LinearFn { int64_t m, t; LinearFn() { m = t = 0; } LinearFn(int64_t m_, int64_t t_) { m = m_, t = t_; } int64_t operator()(int64_t x) { return m * x + t; } }; bool intersects_earlier(LinearFn f, LinearFn g, LinearFn h) { return (double)(h.t - f.t) / (double)(f.m - h.m) < (double)(g.t - f.t) / (double)(f.m - g.m); } #define N 100001 int64_t s[N], v[N], d[N], ans[N]; vector<pair<size_t, int64_t>> g[N]; LinearFn q[N]; size_t l = 0; void find_shortest_route(size_t u, size_t p) { LinearFn removed(0, 0); size_t ol = SIZE_MAX; if (l) { size_t a = 0, b = l - 1; while (a < b) { size_t mid = (a + b) / 2; if (mid < l - 1 && q[mid](v[u]) <= q[mid + 1](v[u])) b = mid; else a = mid + 1; } ans[u] = q[a](v[u]) + d[u] * v[u] + s[u]; LinearFn f(-d[u], ans[u]); a = 0, b = l - 1; while (a < b) { size_t mid = (a + b) / 2; if (mid && intersects_earlier(q[mid - 1], q[mid], f)) b = mid; else a = mid + 1; } if (a == l - 1 && (!a || !intersects_earlier(q[a - 1], q[a], f))) a = l; removed = q[a]; q[a] = f; ol = l; l = a + 1; } else l++; for (auto [x, w] : g[u]) { if (x != p) { d[x] = d[u] + w; find_shortest_route(x, u); } } q[l - 1] = removed; l = ol; } int main() { size_t n; cin >> n; for (size_t i = 0; i + 1 < n; i++) { size_t u, v, w; cin >> u >> v >> w; g[u - 1].emplace_back(v - 1, w); g[v - 1].emplace_back(u - 1, w); } for (size_t i = 1; i < n; i++) cin >> s[i] >> v[i]; find_shortest_route(0, SIZE_MAX); for (size_t i = 1; i < n; i++) cout << ans[i] << ' '; cout << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...