Submission #1111773

#TimeUsernameProblemLanguageResultExecution timeMemory
1111773Marko2604Harbingers (CEOI09_harbingers)C++14
0 / 100
121 ms65536 KiB
#include<bits/stdc++.h>
#define ll long long
#define MAXN 100007
using namespace std;
vector<vector<pair<int,ll>>>T(MAXN);
ll s[MAXN], v[MAXN];
struct line
{
    int node;
    ll k,n;
    ll eval(ll x) 
    {
        return k*x+n;
    }
};
ll depth[MAXN];
void depth_dfs(int node, int p)
{
    for(auto i:T[node])
    {
        if(i.first!=p)
        {
            depth[i.first]=i.second+depth[node];
            depth_dfs(i.first, node);
        }
    }
}
vector<stack<line>>lichao;
vector<vector<int>>loc(MAXN);
map<int,int>xfind;
vector<ll>xx;
int sz;
void make_lichao(int n)
{
    sz = (1<<((int)ceil(log2(n))));
    for(int i=1;i<=n;i++) xx.push_back(v[i]);
    while(xx.size()<sz) xx.push_back(0);
    sort(xx.begin(),xx.end());
    for(int i=0;i<sz;i++) xfind[xx[i]]=i;
    stack<line>st;
    for(int i=0;i<2*sz+5;i++) lichao.push_back(st);
}
void add_line(line ln, int node, int l, int r)
{
    if(lichao[node].empty())
    {
        lichao[node].push(ln);
        loc[ln.node].push_back(node);
        return;
    }
    line lo = lichao[node].top();
    ll lold = lo.eval(xx[l]), rold = lo.eval(xx[r]);
    ll lnew = ln.eval(xx[l]), rnew = ln.eval(xx[r]);
    if(lnew>=lold && rnew>=rold) return;
    if(lnew<=lold && rnew<=rold)
    {
        lichao[node].push(ln);
        loc[ln.node].push_back(node);
        return;
    }

    int mid = (l+r)/2;
    ll mold = lo.eval(xx[mid]), mnew = ln.eval(xx[mid]);
    if(lnew<=lold)
    {
        if(mnew<=mold)
        {
            lichao[node].push(ln);
            loc[ln.node].push_back(node);
            add_line(ln, 2*node+1, mid+1, r);
            return;
        }
        else
        {
            add_line(ln, 2*node, l, mid);
            return;
        }
    }
    else
    {
        if(mnew<=mold)
        {
            lichao[node].push(ln);
            loc[ln.node].push_back(node);
            add_line(ln, 2*node, l, mid);
            return;
        }
        else
        {
            add_line(ln, 2*node+1, mid+1, r);
            return;
        }
    }
    return;
}
void pop_line(line l)
{
    for(auto i:loc[l.node]) lichao[i].pop();
    return;
}
ll get_val(ll x)
{
    ll res = 10000000000000; //PROVERITI JOS
    ll t = xfind[x] + sz;
    while(t>0)
    {
        if(!lichao[t].empty()) res = min(res, lichao[t].top().eval(x));
        t/=2;
    }
    return res;
}
ll dp[MAXN];
void dp_dfs(int node, int p)
{
    if(node != 1 ) dp[node]=s[node]+v[node]*depth[node]+get_val(v[node]);

    line l;
    l.node=node;
    l.k=-depth[node];
    l.n=dp[node];
    add_line(l, 1, 0, sz-1);
    for(auto i:T[node])
    {
        if(i.first!=p)
        {
            dp_dfs(i.first,node);
        }
    }
    pop_line(l);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<n;i++)
    {
        int u1,v1;
        ll w;
        cin>>u1>>v1>>w;
        T[u1].emplace_back(v1,w);
        T[v1].emplace_back(u1,w);
    }
    s[1]=0, v[1]=0;
    for(int i=2;i<=n;i++)
        cin>>s[i]>>v[i];
    depth[1]=0;
    depth_dfs(1,1);
    make_lichao(n);


    dp[1]=0;
    dp_dfs(1,1);
    for(int i=2;i<=n;i++)
        cout<<dp[i]<<" ";
    cout<<endl;
    return 0;
}

Compilation message (stderr)

harbingers.cpp: In function 'void make_lichao(int)':
harbingers.cpp:37:20: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   37 |     while(xx.size()<sz) xx.push_back(0);
      |           ~~~~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...