//https://cses.fi/problemset/task/2087/
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
#pragma GCC optimize("O1,O2,O3")
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const long double EPS = 0.000001;
const int INF = 1e17+500;
const int N = 1e5+5;
const int ALPH = 26;
const int LGN = 25;
const int MOD = 1e9+7;
int n,k;
vector<vector<array<int,2> > > adj;
vector<int> s,v;
vector<int> dp;
vector<int> p, pat;
vector<int> ints;
struct Line {
int a,b;
Line() {
a = 0; b = 0;
}
Line(int a, int b) {
this->a = a;
this->b = b;
}
int eval(int x) {
return a*x + b;
}
long double intersect(Line y) {
return (long double)(b - y.b) / (long double)(y.a - a);
}
};
deque<Line> cht;
vector<vector<pair<Line, int> > > upds;
bool comp(int idx, int x) {
return cht[idx].intersect(cht[idx + 1]) >= x;
}
void update(Line nw) {
upds.pb(vector<pair<Line, int> >());
while(cht.size() > 1 && cht.front().intersect(cht[1]) >= cht.front().intersect(nw)) {
upds[upds.size() - 1].pb(MP(cht.front(), 0));
cht.pop_front();
}
upds[upds.size() - 1].pb(MP(nw, 1));
cht.push_front(nw);
}
void restore_last() {
while(upds.back().size() > 0) {
auto cur = upds.back().back();
if(cur.second) {
cht.pop_front();
}
else {
cht.push_front(cur.first);
}
upds.back().pop_back();
}
upds.pop_back();
}
int query(int x) {
int idx = *lower_bound(ints.begin(), ints.begin() + cht.size() - 1, x, comp);
return cht[idx].eval(x);
}
void dfs(int node) {
for(auto c : adj[node]) {
if(c[0] == pat[node]) continue;
pat[c[0]] = node;
p[c[0]] = p[node] + c[1];
dfs(c[0]);
}
}
void dfs2(int node) {
update(Line(-p[node], dp[node]));
for(auto c : adj[node]) {
if(c[0] == pat[node]) continue;
dp[c[0]] = query(v[c[0]]) + v[c[0]] * p[c[0]] + s[c[0]];
dfs2(c[0]);
}
restore_last();
}
inline void solve() {
cin >> n;
cht.clear(); upds.clear();
adj.resize(n+1, vector<array<int,2> >());
ints.resize(n);
iota(ints.begin(), ints.end(), 0);
for(int i = 0; i<n-1; i++) {
int u,v,d;
cin>>u>>v>>d;
adj[u].pb({v,d});
adj[v].pb({u,d});
}
s.resize(n+1);
v.resize(n+1);
for(int i = 2; i<=n; i++) {
cin>>s[i]>>v[i];
}
dp.resize(n+1);
dp[1] = 0;
pat.resize(n+1); p.resize(n+1);
pat[1] = 0;
p[1] = 0;
dfs(1);
dfs2(1);
for(int i = 2; i<=n; i++) {
cout<<dp[i]<<" ";
}
cout<<"\n";
}
signed main() {
fastio();
int test = 1;
//cin>>test;
while(test--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1116 KB |
Output is correct |
3 |
Correct |
46 ms |
11768 KB |
Output is correct |
4 |
Correct |
52 ms |
17108 KB |
Output is correct |
5 |
Correct |
69 ms |
23044 KB |
Output is correct |
6 |
Correct |
94 ms |
28304 KB |
Output is correct |
7 |
Correct |
44 ms |
13324 KB |
Output is correct |
8 |
Execution timed out |
1036 ms |
20692 KB |
Time limit exceeded |
9 |
Execution timed out |
1064 ms |
17256 KB |
Time limit exceeded |
10 |
Execution timed out |
1055 ms |
21972 KB |
Time limit exceeded |