#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll nx=1e5+5, inf=1e18;
ll n, ds[nx], lz[nx], sp[nx], ans[nx], u, v, w;
vector<pair<int, int>> d[nx];
bool querymode;
struct line
{
ll m, c;
mutable ll p;
line(ll m, ll c, ll p): m(m), c(c), p(p){}
bool operator< (const line &o) const {return querymode?p<o.p:m>o.m;}
};
struct convexhull:multiset<line>
{
ll div(ll a, ll b)
{
if (a%b==0||(a^b)>=0) return a/b;
return a/b-1;
}
bool isect(iterator x, iterator y)
{
if (y==end()) return x->p=inf, false;
if (x->m==y->m) x->p=x->m>=y->m?-inf:inf;
else x->p=div(y->c-x->c, x->m-y->m);
return x->p>=y->p;
}
void add(ll m, ll c)
{
auto x=insert(line(m, c, 0)), y=next(x);
while (isect(x, y)) y=erase(y);
if ((y=x)!=begin()&&isect(--x, y)) isect(x, erase(y));
while ((y=x)!=begin()&&(--x)->p>=y->p) isect(x, erase(y));
}
ll query(ll x)
{
querymode=1;
auto itr=lower_bound(line(0, 0, x));
querymode=0;
return itr->m*x+itr->c;
}
} s;
void pre(int u, int p)
{
for (auto [v, w]:d[u]) if (v!=p) ds[v]=ds[u]+w, pre(v, u);
}
void dfs(int u, int p)
{
if (u!=1) ans[u]=lz[u]+ds[u]*sp[u]+s.query(sp[u]);
s.add(-ds[u], ans[u]);
for (auto [v, w]:d[u]) if (v!=p) dfs(v, u);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=0; i<n-1; i++) cin>>u>>v>>w, d[u].push_back({v, w}), d[v].push_back({u, w});
for (int i=2; i<=n; i++) cin>>lz[i]>>sp[i];
pre(1, 1); dfs(1, 1);
for (int i=2; i<=n; i++) cout<<ans[i]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
29 ms |
10328 KB |
Output is correct |
4 |
Correct |
42 ms |
13136 KB |
Output is correct |
5 |
Correct |
49 ms |
14880 KB |
Output is correct |
6 |
Correct |
69 ms |
16832 KB |
Output is correct |
7 |
Incorrect |
42 ms |
11596 KB |
Output isn't correct |
8 |
Incorrect |
68 ms |
17360 KB |
Output isn't correct |
9 |
Incorrect |
61 ms |
16208 KB |
Output isn't correct |
10 |
Incorrect |
68 ms |
17872 KB |
Output isn't correct |