Submission #1124031

#TimeUsernameProblemLanguageResultExecution timeMemory
1124031luvnaHarbingers (CEOI09_harbingers)C++20
50 / 100
92 ms27464 KiB
#include<bits/stdc++.h>

#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define dbg(x) "[" #x " = " << (x) << "]"

using namespace std;

template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}

typedef long long ll;
typedef long double ld;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}

const int N = 2e5 + 15;
const ll INF = 1e18;

int n;
vector<pii> g[N];
int wait[N], speed[N];

struct line{
    ll slope, c;
    line() {}
    line(ll slope, ll c) : slope(slope), c(c) {}
    ll f(ll x) {return slope*x + c;}
};

struct CHT{
    int top = 0;
    vector<line> lines;
    stack<pair<pii, line>> st;

    void luvna(int n){
        lines.resize(n + 15, line(0,INF));
    }

    bool dump(line d1, line d2, line d3){
        return 1.0l*(d1.c - d2.c) / (d2.slope - d1.slope) <= 1.0l*(d2.c - d3.c) / (d3.slope - d2.slope);
    }

    ll query(ll x){
        int l = 0, r = top - 1;
        while(l < r){
            int mid = (l+r) >> 1;
            if(lines[mid].f(x) >= lines[mid+1].f(x)) l = mid+1;
            else r = mid;
        }
        return lines[l].f(x);
    }

    void add(line d){
        int l = 1, r = top - 1, pos = top;
        while(l <= r){
            int mid = (l+r) >> 1;
            if(dump(d, lines[mid], lines[mid-1])){
                pos = mid;
                r = mid-1;
            }
            else l = mid+1;
        }
        top = pos + 1;
        st.push({pii(pos,top), lines[pos]});
        lines[pos] = d;
    }

    void rollback(){
        int pos = st.top().fi.fi;
        top = st.top().fi.se;
        line d = st.top().se;
        lines[pos] = d;
        st.pop();
    }
};

int h[N];
ll dp[N];
CHT solver;

void dfs(int u, int p){
    //cout << solver.top << endl;
    if(u > 1) dp[u] = 1LL*speed[u]*h[u] + wait[u] + solver.query(speed[u]);
    solver.add(line(-h[u], dp[u]));
    for(auto [v, w] : g[u]) if(v != p){
        h[v] = h[u] + w;
        dfs(v,u);
    }
    solver.rollback();
}

void solve(){
    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 >> wait[i] >> speed[i];

    solver.luvna(n);

    dfs(1,-1);

    for(int i = 2; i <= n; i++) cout << dp[i] << " ";
}

signed main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(0); cout.tie(0);

    #define task "task"

    if(fopen(task".INP", "r")){
        freopen(task".INP", "r", stdin);
        freopen(task".OUT", "w", stdout);
    }

    int t; t = 1; //cin >> t;
    while(t--) solve();
}

Compilation message (stderr)

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