# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
717976 | guagua0407 | Harbingers (CEOI09_harbingers) | C++17 | 164 ms | 21180 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct line{
ll a,b;
ll operator()(const ll x){
return a*x+b;
}
};
const ll mxn=1e5+5;
vector<pair<ll,ll>> adj[mxn];
ll S[mxn];
ll V[mxn];
line li[mxn];
ll depth[mxn];
ll dp[mxn];
int vec[mxn];
int cur_size=1;
void dfs0(ll v,ll p){
for(auto u:adj[v]){
if(u.f==p) continue;
depth[u.f]=depth[v]+u.s;
dfs0(u.f,v);
}
}
bool check(ll a,ll b,ll c){
line l1=li[a];
line l2=li[b];
line l3=li[c];
return (long double)(l2.b-l3.b)/(l3.a-l2.a)<(long double)(l1.b-l2.b)/(l2.a-l1.a);
}
void dfs(ll v,ll p){
ll l=0,r=cur_size-1;
while(l<r){
ll mid=(l+r)/2;
if(li[vec[mid]](V[v])<=li[vec[mid+1]](V[v])){
r=mid;
}
else{
l=mid+1;
}
}
dp[v]=S[v]+depth[v]*V[v]+li[vec[l]](V[v]);
li[v]={-depth[v],dp[v]};
l=0,r=cur_size-1;
while(l<r){
ll mid=(l+r)/2;
if(check(vec[mid],vec[mid+1],v)){
r=mid;
}
else{
l=mid+1;
}
}
int prv_size=cur_size;
int prv_index=vec[l+1];
cur_size=l+2;
vec[l+1]=v;
for(auto u:adj[v]){
if(u.f==p) continue;
dfs(u.f,v);
}
cur_size=prv_size;
vec[l+1]=prv_index;
}
int main() {_
ll n;
cin>>n;
for(ll i=0;i<n-1;i++){
ll d,u,v;
cin>>u>>v>>d;
adj[u].push_back({v,d});
adj[v].push_back({u,d});
}
for(ll i=2;i<=n;i++){
cin>>S[i]>>V[i];
}
dfs0(1,0);
vec[0]=0;
dfs(1,0);
for(ll i=2;i<=n;i++){
cout<<dp[i]<<' ';
}
return 0;
}
//maybe its multiset not set
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |