Submission #562543

#TimeUsernameProblemLanguageResultExecution timeMemory
562543hoanghq2004Harbingers (CEOI09_harbingers)C++14
100 / 100
203 ms28268 KiB
#include <bits/stdc++.h> #pragma GCC optimize ("O3") #pragma GCC optimize ("unroll-loops") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; const int N = 1e5 + 10; int n; vector <pair <int, int> > g[N]; long long f[N]; int s[N], v[N], d[N]; struct Line { long long a, b; Line() { a = 0, b = 1e18; } Line(long long a, long long b): a(a), b(b) {}; long long operator()(long long x) { return a * x + b; } }; struct PersistentConvexHullTrick { Line convex[N]; int bad(Line L1, Line L2, Line L3) { return 1.0L * (L2.b - L1.b) / (L1.a - L2.a) >= 1.0L * (L3.b - L1.b) / (L1.a - L3.a); } int par[N][20]; void add(int u, int v, Line L) { for (int i = 19; i >= 0; --i) if (par[par[u][i]][0] && bad(convex[par[par[u][i]][0]], convex[par[u][i]], L)) u = par[u][i]; while (par[u][0] && bad(convex[par[u][0]], convex[u], L)) u = par[u][0]; convex[v] = L; par[v][0] = u; for (int i = 1; i < 20; ++i) par[v][i] = par[par[v][i - 1]][i - 1]; } long long get(int u, long long x) { for (int i = 19; i >= 0; --i) if (par[par[u][i]][0] && convex[par[par[u][i]][0]](x) < convex[par[u][i]](x)) u = par[u][i]; return min(convex[u](x), convex[par[u][0]](x)); } } DS; void dfs(int u, int p) { f[u] = (u == 1 ? 0 : DS.get(p, v[u]) + 1LL * d[u] * v[u] + s[u]); DS.add(p, u, {- d[u], f[u]}); for (auto [v, w]: g[u]) { if (v == p) continue; d[v] = d[u] + w; dfs(v, u); } } int main() { ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i < n; ++i) { int u, v, w; cin >> u >> v >> w; g[u].push_back({v, w}); g[v].push_back({u, w}); } 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] << " \n"[i == n]; }

Compilation message (stderr)

harbingers.cpp: In function 'void dfs(int, int)':
harbingers.cpp:51:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   51 |     for (auto [v, w]: g[u]) {
      |               ^
#Verdict Execution timeMemoryGrader output
Fetching results...