제출 #1104436

#제출 시각아이디문제언어결과실행 시간메모리
1104436BF001Harbingers (CEOI09_harbingers)C++17
20 / 100
163 ms65536 KiB
#include<bits/stdc++.h> using namespace std; #define N 100005 #define oo 1e18 #define fi first #define se second typedef pair<int, int> ii; struct line{ int a; long long b; line(){ a = 0; b = oo; } line(int x, long long y){ a = x; b = y; } long long operator ()(int x){ return a * x + b; } }; int n, ma, v[N], s[N]; long long dep[N], dp[N]; line bit[1 << 18]; stack<pair<int, line>> st; vector<int> val; vector<ii> adj[N]; void ud(int id, int l, int r, line nw){ if (l == r){ if (bit[id](val[l]) > nw(val[l])){ st.push({id, bit[id]}); bit[id] = nw; } return; } int mid = (l + r) >> 1; if (bit[id].a > nw.a) {st.push({id, bit[id]}); swap(bit[id], nw);} if (bit[id](val[mid]) > nw(val[mid])){ st.push({id, bit[id]}); swap(bit[id], nw); ud(id * 2 + 1, mid + 1, r, nw); } else ud(id * 2, l, mid, nw); } long long get(int id, int l, int r, int pos){ if (l == r){ return bit[id](pos); } int mid = (l + r) >> 1; if (pos <= val[mid]) return min(bit[id](pos), get(id * 2, l, mid, pos)); else return min(bit[id](pos), get(id * 2 + 1, mid + 1, r, pos)); } void rollback(int si){ while ((int) st.size() > si){ int id = st.top().fi; line tmp = st.top().se; bit[id] = tmp; st.pop(); } } void dfs(int u, int p){ if (u != 1) dp[u] = get(1, 0, ma, v[u]) + dep[u] * v[u] + s[u]; int si = (int) st.size(); ud(1, 0, ma, line(-dep[u], dp[u])); for (auto x : adj[u]){ if (x.fi == p) continue; dep[x.fi] = dep[u] + x.se; dfs(x.fi, u); } rollback(si); } signed main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n; for (int i = 1; i <= n - 1; i++){ int u, v, w; cin >> u >> v >> w; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } for (int i = 2; i <= n; i++){ cin >> s[i] >> v[i]; val.push_back(v[i]); } sort(val.begin(), val.end()); val.resize(unique(val.begin(), val.end()) - val.begin()); ma = (int) val.size() - 1; dfs(1, 0); for (int i = 2; i <= n; i++) cout << dp[i] << " "; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...