제출 #377447

#제출 시각아이디문제언어결과실행 시간메모리
377447lycHarbingers (CEOI09_harbingers)C++14
100 / 100
155 ms26476 KiB
#include <bits/stdc++.h> using namespace std; #define TRACE(x) cerr << #x << " :: " << x << endl #define _ << " " << #define SZ(x) (int)(x).size() #define ALL(x) (x).begin(),(x).end() #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define RFOR(i,a,b) for (int i=(a);i>=(b);--i) typedef long long ll; typedef pair<int,int> ii; const int mxN = 1e5+5; const int lgN = 17; int N, S[mxN], V[mxN]; vector<ii> al[mxN]; int d[mxN]; ll dp[mxN]; struct Line { ll m, c; ll eval(int x) { return (ll)m*x + c; } }; struct PConvexHull { Line ch[mxN]; int pa[mxN][lgN], n; PConvexHull() { n = 0; } long double POI(Line p, Line q) { return (long double)(p.c-q.c)/(q.m-p.m); } bool redundant(Line p, Line q, Line l) { return POI(p,l) >= POI(q,l); } int add(int p, Line l) { if (p >= 0) { if (pa[p][0] != -1 && redundant(ch[pa[p][0]], ch[p], l)) { RFOR(k,lgN-1,0) if (pa[p][k] != -1 && pa[pa[p][k]][0] != -1) { if (redundant(ch[pa[pa[p][k]][0]], ch[pa[p][k]], l)) { p = pa[p][k]; } } p = pa[p][0]; } if (l.m == ch[p].m) return p; // same grad doesn't make pa redundant => redundant } ch[n] = l; pa[n][0] = p; FOR(k,1,lgN-1) pa[n][k] = (pa[n][k-1] == -1 ? -1 : pa[pa[n][k-1]][k-1]); return n++; } ll query(int p, int x) { if (pa[p][0] != -1 && ch[pa[p][0]].eval(x) >= ch[p].eval(x)) { RFOR(k,lgN-1,0) if (pa[p][k] != -1 && pa[pa[p][k]][0] != -1) { if (ch[pa[pa[p][k]][0]].eval(x) >= ch[pa[p][k]].eval(x)) { p = pa[p][k]; } } p = pa[p][0]; } return ch[p].eval(x); } } ch; void dfs(int u, int p, int chi) { if (u > 1) { //dp[u] = 1e18; //for (int v = p; v != 0; v = pa[v]) { // dp[u] = min(dp[u], dp[v] + (ll)-d[v]*V[u]); //} //dp[u] += (ll)V[u]*d[u] + S[u]; dp[u] = -ch.query(chi, V[u]) + (ll)V[u]*d[u] + S[u]; } chi = ch.add(chi, {d[u], -dp[u]}); // negate -d[u], dp[u] for min //TRACE(u _ d[u] _ dp[u] _ chi _ ch.pa[chi][0]); for (ii& v : al[u]) if (v.first != p) { //pa[v.first] = u; d[v.first] = d[u]+v.second; dfs(v.first,u,chi); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N; FOR(i,1,N-1){ int Ui, Vi, Di; cin >> Ui >> Vi >> Di; al[Ui].push_back(ii(Vi,Di)); al[Vi].push_back(ii(Ui,Di)); } FOR(i,2,N){ cin >> S[i] >> V[i]; } d[1] = 0; dfs(1,0,-1); FOR(i,2,N){ cout << dp[i] << ' '; } }
#Verdict Execution timeMemoryGrader output
Fetching results...