답안 #883589

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
883589 2023-12-05T12:59:35 Z nghiaaa Harbingers (CEOI09_harbingers) C++14
100 / 100
104 ms 18260 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define ii pair<int,int>
#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define all(v) v.begin(),v.end()
#define BIT(i) ((ll)1<<(i))
#define endl "\n"
#define debug(x) for (auto p: x) cout<<p<<' ';cout<<endl
#define forw(i,j,z) for(int i=(int)j;i<=(int)z;i++)
#define forw2(i,j,z,k) for(int i=(int)j;i<=(int)z;i+=k)
#define ford(i,j,z) for (int i=(int)j;i>=(int)z;i--)
#define ford2(i,j,z,k) for (int i=(int)j;i>=(int)z;i-=k)
#define sz(a) (int)a.size()
const int N=1e5+10;
struct Line{
    ll a,b;
    Line(){}
    Line(ll a1,ll b1):a(a1),b(b1){}
    ll operator ()(const int x) const{
        return a*x+b;
    }
};
int n,prepare[N+1],velocity[N+1],SZ=-1;
vector<ii> adj[N+1];
ll dp[N+1],h[N+1];
Line hull[N+1];
long double point(Line &u,Line &v)
{
    return (long double)(u.b-v.b)/(long double)(v.a-u.a);
}
ll query(int x)
{
    if (SZ==0) return hull[0](x);
    int l=0,r=SZ-1;
    while(l<r)
    {
        int mid=((l+r)>>1)+1;
        if (x<point(hull[mid],hull[mid+1]))
            r=mid-1;
        else l=mid;
    }
    return min(hull[l](x),hull[l+1](x));
}
void update(Line u)
{
    if (SZ==0) return ;
    int l=1,r=SZ;
    while(l<r)
    {
        int mid=((l+r)>>1)+1;
        if (point(hull[mid-1],hull[mid])>=point(hull[mid],u))
            r=mid-1;
        else l=mid;
    }
    if (point(hull[l-1],hull[l])>=point(hull[l],u))
        SZ=0;
    else SZ=l;
//    while(SZ>0&&point(hull[SZ-1],hull[SZ])>=point(hull[SZ],u))
//        SZ--;
}
void dfs(int u,int dad)
{
    int preSZ=SZ;Line preLine;
    if (u>1){
        dp[u]=query(velocity[u])+h[u]*velocity[u]+prepare[u];
            update(Line(-h[u],dp[u]));
            preLine=hull[++SZ];
            hull[SZ]=Line(-h[u],dp[u]);
    }
    forw(i,0,sz(adj[u])-1) if (adj[u][i].f!=dad)
    {
        int v=adj[u][i].f,d=adj[u][i].s;
        h[v]=h[u]+d;
        dfs(v,u);
    }
    if (u>1){
        hull[SZ]=preLine;
        SZ=preSZ;
    }
}
void solve()
{
    cin>>n;
    forw(i,2,n)
    {
        int u,v,d;
        cin>>u>>v>>d;
        adj[u].push_back(mp(v,d));
        adj[v].push_back(mp(u,d));
    }
    forw(i,2,n){
        cin>>prepare[i]>>velocity[i];
    }
    hull[++SZ]=Line(0,0);
    dfs(1,1);
    forw(i,2,n) cout<<dp[i]<<' ';
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //cout<<setprecision(6)<<fixed;
    //time_t TIME_TU=clock();
    int t=1;
    //cin>>t;
    while(t--)
        solve();
    //time_t TIME_TV=clock();
    //cerr<<(db)(TIME_TV-TIME_TU)/CLOCKS_PER_SEC<<endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3676 KB Output is correct
2 Correct 2 ms 3932 KB Output is correct
3 Correct 37 ms 9824 KB Output is correct
4 Correct 61 ms 13256 KB Output is correct
5 Correct 51 ms 15604 KB Output is correct
6 Correct 68 ms 18260 KB Output is correct
7 Correct 40 ms 10216 KB Output is correct
8 Correct 95 ms 14100 KB Output is correct
9 Correct 104 ms 16112 KB Output is correct
10 Correct 88 ms 15084 KB Output is correct