제출 #1306156

#제출 시각아이디문제언어결과실행 시간메모리
1306156huypham2009Harbingers (CEOI09_harbingers)C++20
10 / 100
128 ms99068 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e18;
const int maxn=1e5+5;
struct line{
    int a,b;
    line(int x=0,int y=inf)
    {
        a=x;
        b=y;
    }
    int operator()(int x)   const
    {
        return a*x+b;
    }
};
struct node{
    line d;
    node *L,*R;
    node()
    {
        L=NULL;
        R=NULL;
        d=line();
    }
    node(line x)
    {
        L=NULL;
        R=NULL;
        d=x;
    }
    node(node *&cur)
    {
        d=cur->d;
        L=cur->L;
        R=cur->R;
    }
};
struct lichao{
    node *root;
    int ml=0,mr=1e4;
    lichao()
    {
        root=new node();
    }
    void update(node *&cur, line newl, int l, int r)
    {
        if (!cur) {
            cur = new node(newl);
            return;
        }
        cur = new node(cur);
        int mid = (l + r) >> 1;
        bool lef = newl(l) < cur->d(l);
        bool m   = newl(mid) < cur->d(mid);
        if (m) swap(newl, cur->d);
        if (l == r) return;
        if (lef != m) {
            update(cur->L, newl, l, mid);
        } else {
            update(cur->R, newl, mid + 1, r);
        }
    }

    void update(line newl)
    {
        update(root,newl,ml,mr);
    }
    int get(node *&cur,int x,int l,int r)
    {
        if(l>x||r<x||cur==NULL)    return inf;
        int mid=l+r>>1;
        if(x>mid)   return min(cur->d(x),get(cur->R,x,mid+1,r));
        else return min(cur->d(x),get(cur->L,x,l,mid-1));
    }
    int get(int x)
    {
        return get(root,x,ml,mr);
    }
}   town[maxn];
int h[maxn],n,dp[maxn],S[maxn],V[maxn];
line Line[maxn];
vector<pair<int,int>>g[maxn];
void dfs(int u,int p)
{
    town[u].root=new node(town[p].root);
    dp[u]=town[p].get(V[u])+h[u]*V[u]+S[u];
    // cout<<u<<' '<<town[u].get(V[u])<<' '<<dp[u]<<'\n';
    town[u].update({-h[u],dp[u]});
    for(pair<int,int>v:g[u])
    {
        if(v.first==p)  continue;
        h[v.first]=h[u]+v.second;
        dfs(v.first,u);
    }
}
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    for(int i=1;i<n;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        g[u].push_back({v,w});
        g[v].push_back({u,w});
    }
    for(int i=2;i<=n;i++)   cin>>S[i]>>V[i];    
    town[1].update({0,0});
    dfs(1,1);
    for(int i=2;i<=n;i++)   cout<<dp[i]<<' ';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...