Submission #869168

# Submission time Handle Problem Language Result Execution time Memory
869168 2023-11-03T11:02:57 Z sleepntsheep Harbingers (CEOI09_harbingers) C++17
100 / 100
71 ms 21332 KB
#include <cstdio>
#include <stack>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll = long long;
using ld = long double;
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false)

#define N 100005
#define ALL(x) x.begin(), x.end()

struct line
{
    ll m, c;
    line(ll m, ll c) : m(m), c(c) {}
    line() : m(0), c(0) {}
    inline ll operator[](ll x) { return m*x+c; }
    inline ld intersect(line &l) { return (ld)(c-l.c)/(l.m-m); }
};
ostream& operator<<(ostream &o, line k)
{
    o<<'{'<<k.m<<", "<<k.c<<'}';
    return o;
}

int n, s[N], v[N];
ll rt[N], dp[N];
vector<pair<int, int>> g[N];
line cht[N]; int sz;

inline int add(line k)
{
    if (sz > 1 && cht[sz].intersect(k) >= cht[sz].intersect(cht[sz-1])) return sz+1;
    int l = 2, r = sz, z = sz+1;
    for (; l <= r;)
    {
        int m = (l+r)/2;
        if (cht[m].intersect(cht[m-1]) > cht[m].intersect(k)) z = m, r = m - 1;
        else l = m + 1;
    }
    return z;
}

inline ll qry(ll x)
{
    int l = 1, r = sz - 1;
    for (;l <= r;)
    {
        int m = (l+r)/2;
        if (cht[m].intersect(cht[m+1]) <= x) l = m + 1;
        else r = m - 1;
    }
    return cht[l][x];
}

void dfs(int u, int p)
{
    int at, psz = sz;
    if (u != 1) dp[u] = s[u] + 1ll*v[u]*rt[u] - qry(v[u]);
    line nw{rt[u], -dp[u]};
    at = add(nw);
    line overwrote = cht[at];
    if (at) cht[at] = nw, sz = at;
    //cerr << u << ' ' << at << endl; for (int i = 1; i <= sz; ++i) cerr << "  " << cht[i]; cerr<<endl;
    for (auto [w, v] : g[u]) if (v != p) rt[v] = rt[u] + w, dfs(v, u);
    sz = psz; cht[at] = overwrote;
}

int main()
{
    ShinLena;
    cin >> n;
    for (int u, v, w, i = 1; i < n; ++i) cin >> u >> v >> w, g[u].emplace_back(w, v), g[v].emplace_back(w, u);
    for (int i = 2; i <= n; ++i) cin >> s[i] >> v[i];
    dfs(1, 1);
    for (int i = 2; i <= n; ++i) cout << dp[i] << ' ';

    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 27 ms 11600 KB Output is correct
4 Correct 39 ms 14928 KB Output is correct
5 Correct 48 ms 18352 KB Output is correct
6 Correct 58 ms 21332 KB Output is correct
7 Correct 36 ms 11860 KB Output is correct
8 Correct 67 ms 15636 KB Output is correct
9 Correct 71 ms 17784 KB Output is correct
10 Correct 64 ms 16592 KB Output is correct