Submission #963867

#TimeUsernameProblemLanguageResultExecution timeMemory
963867sheldonHarbingers (CEOI09_harbingers)C++14
100 / 100
93 ms21540 KiB
#include <bits/stdc++.h> using namespace std; const int nax = 1e5 + 5; struct line { long long m, b; long long eval (long long x) { return x * m + b; } pair<long long, long long> intersect (line ln) { // m1 * x + b1 = m2 * x = b2 return {ln.b - b, m - ln.m}; } }; bool check (line x, line y, line z) { pair<long long, long long> p1 = x.intersect (y), p2 = y.intersect (z); return (__int128) p1.first * p2.second >= (__int128) p2.first * p1.second; } struct convex_hull { vector<line> st; void insert (line ln) { if (st.empty ()) { st.push_back (ln); return; } while (st.size () >= 2 && check (st[st.size () - 2], st.back (), ln)) { st.pop_back (); } st.push_back (ln); } long long query (long long x) { int l = 0, r = (int) st.size () - 2; long long ans = 2e18; while (l <= r) { int mid = (l + r) / 2; pair<long long, long long> p1 = st[mid].intersect (st[mid + 1]); if (p1.first < x * p1.second) { l = mid + 1; } else { r = mid - 1; } } return st[r + 1].eval (x); } }; long long start[nax], per_km[nax]; long long ans[nax]; int sz[nax], heavy[nax], parent[nax], head[nax], cost_heavy[nax]; convex_hull hulls[nax]; vector<pair<int, int>> edges[nax]; void dfs (int u, int p) { sz[u] = 1; int mx = 0; parent[u] = p; for (auto &it : edges[u]) { int v = it.first; if (v != p) { dfs (v, u); if (sz[v] > mx) { mx = sz[v]; heavy[u] = v; cost_heavy[u] = it.second; } sz[u] += sz[v]; } } } void decompose (int u, int p, int h, long long path) { head[u] = h; ans[u] = start[u] + path * per_km[u] + hulls[h].query (per_km[u]); int node = parent[head[u]]; while (node) { ans[u] = min (ans[u], start[u] + path * per_km[u] + hulls[head[node]].query (per_km[u])); node = parent[head[node]]; } hulls[h].insert (line {-path, ans[u]}); for (auto &it : edges[u]) { if (it.first != p && it.first != heavy[u]) { int v = it.first; hulls[v].st.push_back ({0, 0}); decompose (v, u, v, path + it.second); } } if (heavy[u]) { decompose (heavy[u], u, h, path + cost_heavy[u]); } } void solve() { int n; cin >> n; for (int i = 0; i < n - 1; ++i) { int u, v, x; cin >> u >> v >> x; edges[u].push_back ({v, x}); edges[v].push_back ({u, x}); } for (int i = 2; i <= n; ++i) { cin >> start[i] >> per_km[i]; } dfs (1, 0); hulls[1].st.push_back ({0, 0}); decompose (1, 0, 1, 0); for (int i = 2; i <= n; ++i) { cout << ans[i] << ' '; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); }

Compilation message (stderr)

harbingers.cpp: In member function 'long long int convex_hull::query(long long int)':
harbingers.cpp:43:19: warning: unused variable 'ans' [-Wunused-variable]
   43 |         long long ans = 2e18;
      |                   ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...