///***LTT***///
/// ->NHAT QUOC GIA<- ///
#include<bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
#define int long long
#define endl "\n"
#define F first
#define S second
#define pb push_back
using namespace std;
//vector <int> lg2;
//void LOG_ARR(int n){
// lg2.resize(n + 3);
// for (int i = 1;i <= n;++i){
// lg2[i] = __lg(i);
// }
//}
const long long oo = 1e9+7;
const int N = 2 * 1e5 + 10;
int n;
int s[N], v[N];
struct line{
long long a, b;
};
line c[N];
long double giao[N];
vector <pair <int ,int>> adj[N];
int sz;
long long d[N];
long long dp[N];
long long query(int x){
int l = 1, r = sz;
int res = 0;
giao[1] = -oo * oo;
while (l <= r){
int mid = (l + r)/2;
if (giao[mid] <= x){
l = mid + 1;
res = mid;
} else r = mid - 1;
}
if (res - 1)
return c[res].a * x + c[res].b;
return 0;
}
bool b_isbad(line a, line b, line c){
return 1.0 * (c.b - a.b)/(a.a - c.a) < 1.0 * (b.b - a.b)/(a.a - b.a);
}
long double insec(line a, line b){
return 1.0 * (b.b - a.b)/(a.a - b.a);
}
int findsz(line x){
if (sz == 1 or !b_isbad(c[sz - 1], c[sz], x)){
return sz + 1;
}
int l = 2, r = sz;
int res = sz;
while (l <= r){
int mid = (l + r)/2;
if (b_isbad(c[mid - 1], c[mid], x)){
r = mid - 1;
res = mid;
} else l = mid + 1;
}
return res;
}
void dfs(int u, int p){
dp[u] = 1ll * query(v[u]) + s[u] + d[u] * v[u];
// cout << u <<" "<< query(v[u]) <<" "<<+ s[u] + d[u] * v[u] <<"\n";
// for (int i = 1;i <= sz;++i){
// cout << giao[i] <<" ";
// }
// cout <<"\n";
int tmpsz = sz;
sz = findsz({-d[u], dp[u]});
line tmp = c[sz];
c[sz] = {-d[u], dp[u]};
long double tmpgiao = giao[sz];
giao[sz] = insec(c[sz], c[sz - 1]);
for (int i = 0;i < adj[u].size();++i){
int v = adj[u][i].F;
int w = adj[u][i].S;
if (v == p) continue;
d[v] = d[u] + w;
dfs(v, u);
}
giao[sz] = tmpgiao;
c[sz] = tmp;
sz = tmpsz;
return;
}
void solve(){
cin >> n;
for (int i = 1;i < n;++i){
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({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 << dp[i] << " ";
}
return;
}
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
if (fopen("harb.inp", "r")){
freopen("harb.inp", "r", stdin);
freopen("harb.out", "w", stdout);
}
// int t;
// cin >> t;
////dsadsa
// while(t--){
solve();
// }
return 0;
}
Compilation message (stderr)
harbingers.cpp: In function 'int main()':
harbingers.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen("harb.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen("harb.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |