Submission #963867

# Submission time Handle Problem Language Result Execution time Memory
963867 2024-04-15T20:41:01 Z sheldon Harbingers (CEOI09_harbingers) C++14
100 / 100
93 ms 21540 KB
#include <bits/stdc++.h>

using namespace std;


const int nax = 1e5 + 5;

struct line {
    long long m, b;

    long long eval (long long x) {
        return x * m + b;
    }

    pair<long long, long long> intersect (line ln) {
        // m1 * x + b1 = m2 * x = b2
        return {ln.b - b, m - ln.m};
    }
};

bool check (line x, line y, line z) {
    pair<long long, long long> p1 = x.intersect (y), p2 = y.intersect (z);

    return (__int128) p1.first * p2.second >= (__int128) p2.first * p1.second;
}

struct convex_hull {
    vector<line> st;

    void insert (line ln) {
        if (st.empty ()) {
            st.push_back (ln);
            return;
        }
        while (st.size () >= 2 && check (st[st.size () - 2], st.back (), ln)) {
            st.pop_back ();
        }
        st.push_back (ln);
    }

    long long query (long long x) {
        int l = 0, r = (int) st.size () - 2;
        long long ans = 2e18;
        while (l <= r) {
            int mid = (l + r) / 2;
            pair<long long, long long> p1 = st[mid].intersect (st[mid + 1]);

            if (p1.first < x * p1.second) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return st[r + 1].eval (x);
    }
};

long long start[nax], per_km[nax];

long long ans[nax];

int sz[nax], heavy[nax], parent[nax], head[nax], cost_heavy[nax];

convex_hull hulls[nax];

vector<pair<int, int>> edges[nax];

void dfs (int u, int p) {
    sz[u] = 1;
    int mx = 0;
    parent[u] = p;
    for (auto &it : edges[u]) {
        int v = it.first;
        if (v != p) {
            dfs (v, u);
            if (sz[v] > mx) {
                mx = sz[v];
                heavy[u] = v;
                cost_heavy[u] = it.second;
            }
            sz[u] += sz[v];
        }
    }
}


void decompose (int u, int p, int h, long long path) {

    head[u] = h;

    ans[u] = start[u] + path * per_km[u] + hulls[h].query (per_km[u]);
    int node = parent[head[u]];
    while (node) {
        ans[u] = min (ans[u], start[u] + path * per_km[u] + hulls[head[node]].query (per_km[u]));
        node = parent[head[node]];
    }
    hulls[h].insert (line {-path, ans[u]});

    for (auto &it : edges[u]) {
        if (it.first != p && it.first != heavy[u]) {
            int v = it.first;
            hulls[v].st.push_back ({0, 0});
            decompose (v, u, v, path + it.second);
        }
    }

    if (heavy[u]) {
        decompose (heavy[u], u, h, path + cost_heavy[u]);
    }
}

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n - 1; ++i) {
        int u, v, x;
        cin >> u >> v >> x;
        edges[u].push_back ({v, x});
        edges[v].push_back ({u, x});
    }
    for (int i = 2; i <= n; ++i) {
        cin >> start[i] >> per_km[i];
    }
    dfs (1, 0);
    hulls[1].st.push_back ({0, 0});
    decompose (1, 0, 1, 0);
    for (int i = 2; i <= n; ++i) {
        cout << ans[i] << ' ';
    }

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
}

Compilation message

harbingers.cpp: In member function 'long long int convex_hull::query(long long int)':
harbingers.cpp:43:19: warning: unused variable 'ans' [-Wunused-variable]
   43 |         long long ans = 2e18;
      |                   ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 3 ms 8892 KB Output is correct
3 Correct 27 ms 13988 KB Output is correct
4 Correct 38 ms 16776 KB Output is correct
5 Correct 48 ms 18772 KB Output is correct
6 Correct 62 ms 21076 KB Output is correct
7 Correct 53 ms 14300 KB Output is correct
8 Correct 93 ms 19108 KB Output is correct
9 Correct 83 ms 21540 KB Output is correct
10 Correct 65 ms 20556 KB Output is correct