#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair < int , int >
#define fi first
#define se second
constexpr int Nmax = 1e5 + 5;
ll f[Nmax], v[Nmax], d[Nmax], s[Nmax];
vector < ii > adj[Nmax];
struct line {
ll a, b;
ll calc(ll x) {
return a * x + b;
}
double intersect(line x) {
return (double)(x.b - b) / (double)(a - x.a);
}
}a[Nmax + 5];
struct ope {
int id, old_top;
line old_id;
};
stack < ope > st;
int top = 0;
void add(line x) {
if (top <= 1 || (top >= 2 && a[top - 1].intersect(a[top]) < a[top - 1].intersect(x))) {
st.push({top + 1, top, line{0, 0}});
top++;
a[top] = x;
return;
}
int l = 2, r = top, ans = 0;
while (l <= r) {
int mid = (l + r) / 2;
if (a[mid - 1].intersect(x) <= a[mid - 1].intersect(a[mid])) {
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
st.push({ans, top, a[ans]});
top = ans;
a[ans] = x;
}
void undo() {
auto t = st.top();
st.pop();
top = t.old_top;
a[t.id] = t.old_id;
}
ll query(ll x) {
if (top == 1) {
return a[top].calc(x);
}
if (a[1].intersect(a[2]) > x) {
return a[1].calc(x);
}
int l = 2, r = top, ans = 0;
while (l <= r) {
int mid = (l + r) / 2;
if (a[mid - 1].intersect(a[mid]) <= x) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
return a[ans].calc(x);
}
void dfs(int i, int j) {
if (i != 1) {
f[i] = query(v[i]) + s[i] + v[i] * d[i];
}
add(line{-d[i], f[i]});
for (auto x:adj[i]) {
int u = x.fi, w= x.se;
if (u == j) continue;
d[u] = d[i] + w;
dfs(u, i);
}
undo();
}
signed main() {
if (fopen(".inp","r")) {
freopen(".inp","r",stdin);
freopen(".out","w",stdout);
}
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({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 << f[i] << " ";
}
return 0;
}
Compilation message
harbingers.cpp: In function 'int main()':
harbingers.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen(".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
harbingers.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen(".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
26 ms |
12124 KB |
Output is correct |
4 |
Correct |
37 ms |
14932 KB |
Output is correct |
5 |
Correct |
48 ms |
17232 KB |
Output is correct |
6 |
Correct |
74 ms |
19792 KB |
Output is correct |
7 |
Incorrect |
35 ms |
12288 KB |
Output isn't correct |
8 |
Incorrect |
64 ms |
15560 KB |
Output isn't correct |
9 |
Incorrect |
59 ms |
17088 KB |
Output isn't correct |
10 |
Incorrect |
63 ms |
16376 KB |
Output isn't correct |