Submission #705177

# Submission time Handle Problem Language Result Execution time Memory
705177 2023-03-03T13:56:34 Z finn__ Harbingers (CEOI09_harbingers) C++17
50 / 100
195 ms 22596 KB
#include <bits/stdc++.h>
using namespace std;

struct LinearFn
{
    int64_t m, t;

    LinearFn() { m = t = 0; }

    LinearFn(int64_t m_, int64_t t_) { m = m_, t = t_; }

    int64_t operator()(int64_t x) { return m * x + t; }
};

bool intersects_earlier(LinearFn f, LinearFn g, LinearFn h)
{
    return (double)(h.t - f.t) / (double)(f.m - h.m) <
           (double)(g.t - f.t) / (double)(f.m - g.m);
}

#define N 100000

int64_t s[N], v[N], d[N], ans[N];
vector<pair<size_t, int64_t>> g[N];
LinearFn q[N];
size_t l = 0;

void find_shortest_route(size_t u, size_t p)
{
    bool removed_fn = 0;
    LinearFn removed(0, 0);
    size_t ol = SIZE_MAX;
    if (l)
    {
        size_t a = 0, b = l - 1;
        while (a < b)
        {
            size_t mid = (a + b) / 2;
            if (mid < l - 1 && q[mid](v[u]) <= q[mid + 1](v[u]))
                b = mid;
            else
                a = mid + 1;
        }
        ans[u] = q[a](v[u]) + d[u] * v[u] + s[u];
        LinearFn f(-d[u], ans[u]);
        a = 0, b = l - 1;
        while (a < b)
        {
            size_t mid = (a + b) / 2;
            if (mid && intersects_earlier(q[mid - 1], q[mid], f))
                b = mid;
            else
                a = mid + 1;
        }
        if (a == l - 1 && (!a || !intersects_earlier(q[a - 1], q[a], f)))
            q[l++] = f;
        else
        {
            removed_fn = 1;
            removed = q[a];
            q[a] = f;
            ol = l;
            l = a + 1;
        }
    }
    else
        l++;

    for (auto [x, w] : g[u])
    {
        if (x != p)
        {
            d[x] = d[u] + w;
            find_shortest_route(x, u);
        }
    }

    if (removed_fn)
    {
        q[l - 1] = removed;
        l = ol;
    }
    else
        l--;
}

int main()
{
    size_t n;
    cin >> n;

    for (size_t i = 0; i + 1 < n; i++)
    {
        size_t u, v, w;
        cin >> u >> v >> w;
        g[u - 1].emplace_back(v - 1, w);
        g[v - 1].emplace_back(u - 1, w);
    }

    for (size_t i = 1; i < n; i++)
        cin >> s[i] >> v[i];

    find_shortest_route(0, SIZE_MAX);

    for (size_t i = 1; i < n; i++)
        cout << ans[i] << ' ';
    cout << '\n';
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4180 KB Output isn't correct
2 Correct 5 ms 4692 KB Output is correct
3 Correct 83 ms 11976 KB Output is correct
4 Correct 119 ms 15528 KB Output is correct
5 Correct 139 ms 19116 KB Output is correct
6 Correct 162 ms 22596 KB Output is correct
7 Incorrect 107 ms 13004 KB Output isn't correct
8 Incorrect 174 ms 17568 KB Output isn't correct
9 Incorrect 195 ms 19056 KB Output isn't correct
10 Incorrect 186 ms 17856 KB Output isn't correct