#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 7;
const ll oo = 1e18 + 69;
int n, s[maxn], p[maxn], u, v, w;
vector<pa> g[maxn];
ll dp[maxn], f[maxn];
struct line {
ll m, b;
};
double inter(line &c, line &d) {
return (double) (d.b - c.b) / (double) (c.m - d.m);
}
struct cht {
struct opr {
line ow;
int sz, pos;
};
stack<opr> undo_list;
line lines[maxn];
int sz = 1;
bool bad(line &a, line &b, line &c) {
return inter(a, c) > inter(a, b);
}
ll calc(line a, ll x) {
return a.m * x + a.b;
}
ll query(ll x) {
ll res = calc(lines[0], x);
int l = 0, r = sz - 1, mid;
while(l < r) {
mid = l + r >> 1;
ll s1 = calc(lines[mid], x);
ll s2 = calc(lines[mid + 1], x);
mini(res, min({s1, s2}));
if(s1 < s2) {
r = mid;
} else {
l = mid + 1;
}
}
return res;
}
void add(line new_line) {
int l = 1, r = sz - 1, mid, res = sz;
while(l <= r) {
mid = l + r >> 1;
if(bad(lines[mid - 1], lines[mid], new_line)) {
r = mid - 1;
}
else {
res = mid;
l = mid + 1;
}
}
undo_list.push({lines[res], sz, res});
lines[res] = new_line;
sz = res + 1;
}
void undo() {
auto top = undo_list.top();
undo_list.pop();
lines[top.pos] = top.ow;
sz = top.sz;
}
} cht;
void dfs(int u, int par) {
for(auto e:g[u]) {
int v = e.fi, w = e.se;
if(v == par) continue;
f[v] = f[u] + w;
dp[v] = f[v] * s[v] + p[v] + cht.query(s[v]);
cht.add({-f[v], dp[v]});
dfs(v, u);
cht.undo();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n;
for(int i = 1; i < n; i++) {
cin>>u>>v>>w;
g[u].pb({v, w});
g[v].pb({u, w});
}
for(int i = 2; i <= n; i++) cin>>p[i]>>s[i];
cht.lines[0] = {0, 0};
dfs(1, 0);
for(int i = 2; i <= n; i++) cout<<dp[i]<<" ";
return 0;
}
Compilation message
harbingers.cpp: In member function 'long long int cht::query(long long int)':
harbingers.cpp:55:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | mid = l + r >> 1;
| ~~^~~
harbingers.cpp: In member function 'void cht::add(line)':
harbingers.cpp:71:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Incorrect |
3 ms |
3156 KB |
Output isn't correct |
3 |
Incorrect |
28 ms |
12024 KB |
Output isn't correct |
4 |
Incorrect |
52 ms |
16620 KB |
Output isn't correct |
5 |
Incorrect |
62 ms |
21100 KB |
Output isn't correct |
6 |
Incorrect |
72 ms |
25348 KB |
Output isn't correct |
7 |
Incorrect |
38 ms |
11568 KB |
Output isn't correct |
8 |
Incorrect |
73 ms |
16812 KB |
Output isn't correct |
9 |
Incorrect |
71 ms |
20292 KB |
Output isn't correct |
10 |
Incorrect |
91 ms |
18632 KB |
Output isn't correct |