Submission #1293561

#TimeUsernameProblemLanguageResultExecution timeMemory
1293561escobrandHarbingers (CEOI09_harbingers)C++20
70 / 100
1097 ms22012 KiB
#include <bits/stdc++.h>

using namespace std;
#define all(v) v.begin(),v.end()
#define pb push_back
#define ll long long
#define ld long double
#define fi first
#define se second
#define mk make_pair
typedef pair<int,int> pii;

const int maxn = 1e5 + 10;
ll s[maxn],v[maxn];
vector<pii> adj[maxn];
int i,n,t;

struct Line
{
    ll a,b;
    Line():a(0),b(LLONG_MAX){}
    Line(ll _a,ll _b):a(_a),b(_b){}
    
    ll cal(ll x)const
    {
        return x * a + b;
    }
};

struct LC:deque<Line>
{
    stack<pair<int,Line>> mem;
    stack<int> st;
    inline ld xcut(const Line& a,const Line& b)
    {
        return (ld)(b.b - a.b) / abs(a.a-b.a);
    }
    inline bool check( const Line &a,const Line &b,const Line &c)
    {
        return xcut(a,b) <xcut(b,c);
    }
    LC()
    {
        push(Line(0,0));
        make_checkpoint();
    }
    inline void rollback()
    {
        pair<int,Line> tmp = mem.top();
        mem.pop();
        if(tmp.fi==0)pop_back();
        if(tmp.fi==1)push_back(tmp.se);
    }
    
    void make_checkpoint()
    {
        st.push(mem.size());
    }
    
    void erase_checkpoint()
    {
        st.pop();
        while(mem.size() > st.top())rollback();
    }
    
    void push(Line a)
    {
        while(size()>1 && !check(end()[-2],end()[-1],a))
        {
            mem.push(mk(1,back()));
            pop_back();
        }
        mem.push(mk(0,a));
        push_back(a);
    }
    
    ll get(ll x)
    {
        int l = 0,r = size()-1;
        while(l<r)
        {
            int mid = (l + r)/2;
            if(xcut(at(mid),at(mid+1)) > x) r = mid;
            else l = mid + 1;
        }
        return at(l).cal(x);
    }
}CHT;

ll dp[maxn];

void dfs(int i,int pa,ll d)
{
    if(pa)
    {
        dp[i] = s[i] + d * v[i] + CHT.get(v[i]);
        CHT.push(Line(-d,dp[i] ));
    }
    CHT.make_checkpoint();
    for(pii k : adj[i])
    {
        if(k.fi == pa)continue;
        dfs(k.fi,i,d + k.se);
    }
    CHT.erase_checkpoint();
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    if(fopen("harbingers.in","r"))
    {
        freopen("harbingers.in","r",stdin);
        freopen("harbingers.out","w",stdout);
    }
    
    cin>>n;
    for(i = 1;i<n;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        adj[u].pb(mk(v,w));
        adj[v].pb(mk(u,w));
    }
    
    for(i = 2;i<=n;i++)
    {
        cin>>s[i]>>v[i];
    }
    
    dfs(1,0,0);
    
    for(i =2;i<=n;i++)cout<<dp[i]<<' ';
    
    return 0;
}

Compilation message (stderr)

harbingers.cpp: In function 'int main()':
harbingers.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |         freopen("harbingers.in","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |         freopen("harbingers.out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...