#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 = 0;
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);
if(s1 < s2) {
res = s1;
r = mid - 1;
} else {
res = s2;
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:72:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
72 | mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2772 KB |
Output isn't correct |
2 |
Incorrect |
3 ms |
3284 KB |
Output isn't correct |
3 |
Incorrect |
29 ms |
14104 KB |
Output isn't correct |
4 |
Incorrect |
40 ms |
19168 KB |
Output isn't correct |
5 |
Incorrect |
52 ms |
24588 KB |
Output isn't correct |
6 |
Incorrect |
66 ms |
29520 KB |
Output isn't correct |
7 |
Incorrect |
38 ms |
13896 KB |
Output isn't correct |
8 |
Incorrect |
75 ms |
19924 KB |
Output isn't correct |
9 |
Incorrect |
73 ms |
23628 KB |
Output isn't correct |
10 |
Incorrect |
62 ms |
20904 KB |
Output isn't correct |