제출 #490124

#제출 시각아이디문제언어결과실행 시간메모리
490124macktvzHarbingers (CEOI09_harbingers)C++17
70 / 100
1088 ms21036 KiB
#include <bits/stdc++.h> using namespace std; struct line { long long a,b; double intersectX(line l) {return (b-l.b)/(l.a-a);} }; const int N = 1e5+1; stack<pair<int,int>> ch; vector<int> id(N); deque<int> slop; line l[N]; long long dp[N]; vector<pair<int,int>> adj[N]; int A[N]; int B[N]; void rollback(int i) { while (ch.top().first != i || ch.top().second != 1) { auto op = ch.top(); ch.pop(); if (op.second == 1) { slop.pop_back(); } else { slop.push_back(op.first); } } } long double intersect(int i, int j) { if (l[i].a > l[j].a) swap(i,j); return (l[i].b-l[j].b)/(l[j].a-l[i].a); } bool cmp(int idx, long long a) { return intersect(slop[idx],slop[idx+1]) < a; } void insert(int idx) { while (slop.size() > 1 && intersect(slop[slop.size()-2],slop.back()) >= intersect(slop.back(),idx)) { ch.push({slop.back(),-1}); slop.pop_back(); } ch.push({idx,1}); slop.push_back(idx); } void dfs(int i, int p,long long curr) { if (i != 0) { int j = slop[*lower_bound(id.begin(),id.begin()+slop.size()-1,A[i],cmp)]; dp[i] = curr*A[i] + B[i] -(l[j].a*A[i] + l[j].b); } l[i].a = curr; l[i].b = -dp[i]; insert(i); for(pair<int,int> u : adj[i]) { if (u.first != p) { dfs(u.first,i,curr+u.second); rollback(i); } } } int main() { ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr); int n; cin >> n; iota(id.begin(),id.begin()+n,0); int a,b,c; dp[0]=0; for(int i = 1; i < n; i++) { cin >> a >> b >>c; --a; --b; adj[a].push_back({b,c}); adj[b].push_back({a,c}); } for(int i = 1; i< n; i++) { cin >> B[i] >> A[i]; } dfs(0,-1,0); for(int i = 1; i < n; i++) { cout << dp[i] << endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...