#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int INF = 1e9 + 7;
const int maxn = 1e5 + 5;
vector<pll> adj[maxn];
int a[maxn], b[maxn], ans[maxn];
int n;
struct seg{
int m, k;
seg(){}
seg(int _m, int _k) : m(_m), k(_k){}
int operator ()(int x){
return m * x + k;
}
};
struct node{
int l, r;
seg ans;
// node() : l(0), r(0), ans(seg(0, 0)){}
// node(int m, int k) : l(0), r(0), ans(seg(m, k)){}
// node(seg _ans) : l(0), r(0), ans(_ans){}
} st[maxn * 3];
int cnt = 0;
int newnode(int x = 0){
int id = ++cnt;
st[id].l = st[x].l, st[id].r = st[x].r;
st[id].ans = st[x].ans;
return id;
}
int insert(seg cur, int nd, int l = 0, int r = INF){
if(!nd){
nd = newnode();
st[nd].ans = cur;
return nd;
}
nd = newnode(nd);
if(l == r){
if(st[nd].ans(l) > cur(l)) swap(st[nd].ans, cur);
return nd;
}
int m = (l + r) / 2;
if(st[nd].ans(m) > cur(m)) swap(st[nd].ans, cur);
if(st[nd].ans.m < cur.m) st[nd].l = insert(cur, st[nd].l, l, m);
else st[nd].r = insert(cur, st[nd].r, m + 1, r);
return nd;
}
int insert(int m, int k, int nd){ return insert(seg(m, k), nd);}
int qry(int pos, int nd, int l = 0, int r = INF){
if(!nd) return INF * INF;
if(l == r) return st[nd].ans(pos);
int m = (l + r) / 2;
if(pos <= m) return min(st[nd].ans(pos), qry(pos, st[nd].l, l, m));
else return min(st[nd].ans(pos), qry(pos, st[nd].r, m + 1, r));
}
int rt[maxn];
int dep[maxn];
void dfs(int pos, int prev){
if(pos){
ans[pos] = a[pos] + b[pos] * dep[pos] + qry(b[pos], rt[prev]);
}
rt[pos] = newnode(rt[prev]);
rt[pos] = insert(-dep[pos], ans[pos], rt[pos]);
for(auto [x, w] : adj[pos]){
if(x == prev) continue;
dep[x] = dep[pos] + w;
dfs(x, pos);
}
}
signed main(void){
fastio;
cin>>n;
for(int i = 0; i < n - 1; i++){
int a, b, w;
cin>>a>>b>>w;
a--, b--;
adj[a].eb(b, w);
adj[b].eb(a, w);
}
for(int i = 1; i < n; i++){
cin>>a[i]>>b[i];
}
dfs(0, -1);
for(int i = 1; i < n; i++) cout<<ans[i]<<" ";
cout<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5468 KB |
Output is correct |
2 |
Correct |
4 ms |
8796 KB |
Output is correct |
3 |
Runtime error |
38 ms |
36724 KB |
Execution killed with signal 11 |
4 |
Runtime error |
45 ms |
40584 KB |
Execution killed with signal 11 |
5 |
Runtime error |
63 ms |
43860 KB |
Execution killed with signal 11 |
6 |
Runtime error |
65 ms |
46916 KB |
Execution killed with signal 11 |
7 |
Runtime error |
47 ms |
41308 KB |
Execution killed with signal 11 |
8 |
Runtime error |
63 ms |
47068 KB |
Execution killed with signal 11 |
9 |
Runtime error |
65 ms |
47048 KB |
Execution killed with signal 11 |
10 |
Runtime error |
59 ms |
46156 KB |
Execution killed with signal 11 |