Submission #1349826

#TimeUsernameProblemLanguageResultExecution timeMemory
1349826chithanhnguyenHarbingers (CEOI09_harbingers)C++20
50 / 100
52 ms20168 KiB
/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;

/* START OF TEMPALTE */

// #define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) (1ll << (x))
#define SZ(a) ((int32_t)a.size())

#define debug(a, l, r) {for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';}

template<class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x > y) {
        x = y;
        return true;
    } else return false;
}

template<class X, class Y>
bool maximize(X &x, const Y &y) {
    if (x < y) {
        x = y;
        return true;
    } else return false;
}

/* END OF TEMPALTE */

const ll INF = (ll)1e18 + 5;
struct Line {
    ll a, b;
    Line(ll _a = 0, ll _b = INF) : a(_a), b(_b) {}
    ll get(ll x) const { return a * x + b; }
};

ll ceil_div(ll a, ll b) {
    assert(b > 0);
    if (a >= 0) return (a + b - 1) / b;
    return a / b;
}

struct RollbackCHT {
    struct State {
        int old_top, pos;
        Line old_line;
        ll old_start;
        bool overwritten;
    };

    vector<Line> hull;
    vector<ll> start;
    vector<State> history;
    int top;

    RollbackCHT() {
        hull.assign(1, Line());
        start.assign(1, -INF);
        top = 0;
    }

    int checkpoint() {
        return (int)history.size();
    }

    void rollback(int snap) {
        while ((int)history.size() > snap) {
            State cur = history.back();
            history.pop_back();
            if (cur.overwritten) {
                hull[cur.pos] = cur.old_line;
                start[cur.pos] = cur.old_start;
            }
            top = cur.old_top;
        }
    }

    ll first_better(const Line &old, const Line &nw) {
        return ceil_div(nw.b - old.b, old.a - nw.a);
    }

    bool keep_pos(int pos, const Line &nw) {
        if (pos == 1) return true;
        return hull[pos].get(start[pos]) < nw.get(start[pos]);
    }

    void add_line(Line nw) {
        if (top == 0) {
            history.push_back({top, 1, Line(), 0, false});
            if ((int)hull.size() <= 1) {
                hull.push_back(nw);
                start.push_back(-INF);
            } else {
                hull[1] = nw;
                start[1] = -INF;
            }
            top = 1;
            return;
        }

        int l = 1, r = top, ans = 1;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (keep_pos(mid, nw)) {
                ans = mid;
                l = mid + 1;
            } else r = mid - 1;
        }

        int pos = ans + 1;
        ll st = first_better(hull[ans], nw);

        State cur;
        cur.old_top = top;
        cur.pos = pos;
        cur.overwritten = (pos <= top);
        if (cur.overwritten) {
            cur.old_line = hull[pos];
            cur.old_start = start[pos];
        }
        history.push_back(cur);

        if ((int)hull.size() <= pos) {
            hull.push_back(nw);
            start.push_back(st);
        } else {
            hull[pos] = nw;
            start[pos] = st;
        }
        top = pos;
    }

    ll query(ll x) {
        int l = 1, r = top, ans = 1;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (start[mid] <= x) {
                ans = mid;
                l = mid + 1;
            } else r = mid - 1;
        }
        return hull[ans].get(x);
    }
};

const int MAXN = 1e5 + 5;
int n, s[MAXN], v[MAXN], parent[MAXN];
ll dp[MAXN], distToRoot[MAXN];
vector<pii> adj[MAXN];
RollbackCHT cht;

void init() {
    cin >> n;
    for (int i = 1; i < n; ++i) {
        int u, v, w; cin >> u >> v >> w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }

    for (int i = 2; i <= n; ++i) {
        cin >> s[i] >> v[i];
    }

    cht.add_line(Line(0, 0));
}

void dfsPrepare(int u, int par = 0) {
    parent[u] = par;
    for (auto &e : adj[u]) {
        int v = e.fi, w = e.se;
        if (v == par) continue;
        distToRoot[v] = distToRoot[u] + w;
        dfsPrepare(v, u);
    }
}

void dfsDP(int u, int par = 0) {
    int snapshot = cht.checkpoint();

    if (u != 1) {
        // int pre = parent[u];
        // while (pre) {
        //     minimize(dp[u], s[u] + 1ll * v[u] * (distToRoot[u] - distToRoot[pre]) + dp[pre]);
        //     // s[u] + v[u] * distToRoot[u] (fixed)
        //     // -distToRoot[pre] * v[u] + dp[pre]
        //     pre = parent[pre];
        // }
        dp[u] = s[u] + 1ll * v[u] * distToRoot[u] + cht.query(v[u]);
        cht.add_line(Line(-distToRoot[u], dp[u]));
    }

    for (auto &e : adj[u]) {
        int v = e.fi;
        if (v == par) continue;
        dfsDP(v, u); 
    }

    cht.rollback(snapshot);
}

void solve() {
    dfsPrepare(1);

    fill(dp + 1, dp + n + 1, INF);
    dp[1] = 0;
    dfsDP(1);

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

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    init();
    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...