Submission #1209220

#TimeUsernameProblemLanguageResultExecution timeMemory
1209220russvn123Harbingers (CEOI09_harbingers)C++20
100 / 100
68 ms19880 KiB
#include <bits/stdc++.h> #define int long long using namespace std; typedef long long ll; typedef pair<int, int> Line; #define X first #define Y second const int N = 1e5 + 9; const ll INF = (ll)1e18; struct operation { int pos, top; Line overwrite; operation(int _p, int _t, Line _o) { pos = _p; top = _t; overwrite = _o; } }; vector<operation> undoLst; Line lines[N]; int n, top; ll eval(Line line, ll x) { return line.X * x + line.Y; } bool bad(Line a, Line b, Line c) { return (double)(b.Y - a.Y) / (a.X - b.X) >= (double)(c.Y - a.Y) / (a.X - c.X); } ll getMin(ll coord) { int l = 0, r = top - 1; ll ans = eval(lines[l], coord); while (l < r) { int mid = l + r >> 1; ll x = eval(lines[mid], coord); ll y = eval(lines[mid + 1], coord); if (x > y) l = mid + 1; else r = mid; ans = min(ans, min(x, y)); } return ans; } bool insertLine(Line newLine) { //cout<<newLine.first<<' '<<newLine.second<<endl; int l = 1, r = top - 1, k = top; while (l <= r) { int mid = l + r >> 1; if (bad(lines[mid - 1], lines[mid], newLine)) { k = mid; r = mid - 1; } else l = mid + 1; } undoLst.push_back(operation(k, top, lines[k])); top = k + 1; lines[k] = newLine; return true; } void undo() { operation ope = undoLst.back(); undoLst.pop_back(); top = ope.top; lines[ope.pos] = ope.overwrite; } ll f[N], S[N], V[N], d[N]; vector<Line> a[N]; void dfs(int u, int par) { if (u > 1) { f[u] = getMin(V[u]) + S[u] + V[u] * d[u]; //cout<<u<<' '<<getMin(V[u])<<endl; } //cout<<u<<' '<<-d[u]<<' '<<(int)f[u]<<endl; insertLine(make_pair(-d[u], f[u])); for (vector<Line>::iterator it = a[u].begin(); it != a[u].end(); ++it) { int v = it->X; int uv = it->Y; if (v == par) continue; d[v] = d[u] + uv; dfs(v, u); } undo(); } int32_t main() { cin.tie(NULL)->sync_with_stdio(false); cin >> n; for (int i = 1; i < n; ++i) { int u, v, c; cin >> u >> v >> c; a[u].emplace_back(v, c); a[v].emplace_back(u, c); } for (int i = 2; i <= n; ++i) cin >> S[i] >> V[i]; dfs(1, 0); for (int i = 2; i <= n; ++i) cout << f[i] << ' '; }
#Verdict Execution timeMemoryGrader output
Fetching results...