Submission #337007

#TimeUsernameProblemLanguageResultExecution timeMemory
337007penguinhackerHarbingers (CEOI09_harbingers)C++14
100 / 100
148 ms25548 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ar array int binary_search(function<bool(int)> f, int lb, int rb) { while(lb < rb) { int mb = (lb + rb + 1) / 2; if (f(mb)) lb = mb; else rb = mb - 1; } return lb; } const int mxN = 100000; int n, delay[mxN], speed[mxN]; ll d[mxN], dp[mxN]; vector<pair<int, int>> adj[mxN]; struct Line { ll m = 0, b = 0; ll eval(ll x) {return m * x + b;} }; double isect(Line& a, Line& b) { return (double)(a.b - b.b) / (b.m - a.m); } int sz = 1; Line q[mxN]; void dfs(int u, int p) { int ind = binary_search([&](int i) {return speed[u] > isect(q[i], q[i - 1]);} , 0, sz - 1); dp[u] = d[u] * speed[u] + delay[u] + q[ind].eval(speed[u]); Line cur = {-d[u], dp[u]}; int l = 1, r = sz; while(l < r) { int mid = (l + r) / 2; if (isect(cur, q[mid - 1]) < isect(q[mid], q[mid - 1])) r = mid; else l = mid + 1; } ind = l; swap(cur, q[ind]); int new_size = ind + 1; swap(sz, new_size); for (pair<int, int> v : adj[u]) if (v.first != p) { d[v.first] = d[u] + v.second; dfs(v.first, u); } swap(cur, q[ind]); swap(sz, new_size); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n; ++i) { int a, b, c; cin >> a >> b >> c, --a, --b; adj[a].emplace_back(b, c); adj[b].emplace_back(a, c); } for (int i = 1; i < n; ++i) { cin >> delay[i] >> speed[i]; } for (pair<int, int> p : adj[0]) { d[p.first] = p.second; dfs(p.first, 0); } for (int i = 1; i < n; ++i) { cout << dp[i] << " "; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...