Submission #687545

# Submission time Handle Problem Language Result Execution time Memory
687545 2023-01-26T14:16:44 Z stanislavpolyn Valley (BOI19_valley) C++17
100 / 100
151 ms 23796 KB
#include <bits/stdc++.h>

#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple

#define all(x) (x).begin(), (x).end()
#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()

using namespace std;

mt19937_64 rng(228);

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key

template <typename T>
bool umn(T& a, T b) {
    return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
    return a < b ? a = b, 1 : 0;
}

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;

const int N = 1e5 + 5;

int n, s, q, root;
ve<pii> g[N];
int w[N];
int p[N], pe[N];
int tin[N], tout[N], timer;
int who[N];
ll dep[N];
bool mark[N];
ll dp[N];
int sz[N];
int d[N];

void dfs(int v = root, int p = 0) {
    ::p[v] = p;
    tin[v] = timer++;

    if (mark[v]) {
        dp[v] = 0;
    } else {
        dp[v] = 1e18;
    }

    sz[v] = 1;

    fe (to, g[v]) {
        if (to.fi == p) continue;
        dep[to.fi] = w[to.se] + dep[v];
        who[to.se] = to.fi;
        pe[to.fi] = to.se;
        d[to.fi] = d[v] + 1;

        dfs(to.fi, v);

        sz[v] += sz[to.fi];
        umn(dp[v], dp[to.fi] + w[to.se]);
    }
    tout[v] = timer;
}

bool isUpper(int a, int b) {
    return tin[a] <= tin[b] && tout[a] >= tout[b];
}

int top[N];
int pos[N];
ve<int> st;

void hld(int v = root, int p = 0) {
    st.pb(v);
    pos[v] = sz(st) - 1;
    fe (to, g[v]) {
        if (to.fi == p) continue;

        if (g[v][0].fi == p || sz[to.fi] > sz[g[v][0].fi]) {
            swap(to, g[v][0]);
        }
    }
//    assert(g[v][0].fi != p);
    fe (to, g[v]) {
        if (to.fi == p) continue;

        if (to.fi == g[v][0].fi) {
            top[to.fi] = top[v];
            hld(to.fi, v);
        } else {
            top[to.fi] = to.fi;
            hld(to.fi, v);
        }
    }
}

ll add[N];

ll t[4 * N];

void build(int v = 1, int tl = 0, int tr = sz(st) - 1) {
    if (tl == tr) {
        t[v] = add[st[tl]] + dp[st[tl]];
        return;
    }
    int tm = (tl + tr) >> 1;
    build(v << 1, tl, tm);
    build(v << 1 | 1, tm + 1, tr);
    t[v] = min(t[v << 1], t[v << 1 | 1]);
}

ll get(int v, int tl, int tr, int l, int r) {
    if (l > r) {
        return 2e18;
    }
    if (tl == l && tr == r) {
        return t[v];
    }
    int tm = (tl + tr) >> 1;
    return min(get(v << 1, tl, tm, l, min(r, tm)),
               get(v << 1 | 1, tm + 1, tr, max(tm + 1, l), r));
}

ll get(int l, int r) {
    return get(1, 0, sz(st) - 1, l, r);
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    ios::sync_with_stdio(0);
    cin.tie(0);
#endif

    cin >> n >> s >> q >> root;

    fr (i, 1, n - 1) {
        int a, b;
        cin >> a >> b >> w[i];

        g[a].pb({b, i});
        g[b].pb({a, i});
    }

    fr (i, 1, s) {
        int x;
        cin >> x;
        mark[x] = 1;
    }

    dfs();
    top[root] = root;
    hld();

    {
        rf (i, sz(st) - 2, 0) {
            if (top[st[i + 1]] == top[st[i]]) {
                assert(p[st[i + 1]] == st[i]);
                add[st[i]] = add[st[i + 1]] + w[pe[st[i + 1]]];
            }
        }
    }

    build();

    fr (i, 1, q) {
        int idx, v;
        cin >> idx >> v;

        int u = who[idx];

        if (!isUpper(u, v)) {
            cout << "escaped\n";
        } else {
            if (mark[v]) {
                cout << "0\n";
                continue;
            }

            // faster
            {
                int cur = v;
                ll best = 1e18;
                ll len = 0;

                int jump = 0;
                while (1) {
                    if (isUpper(top[cur], u)) {
                        assert(top[cur] == top[cur]);
                        ll mn = get(pos[u], pos[cur]);
                        mn -= add[cur];
                        umn(best, mn + len);

                        break;
                    }
                    ll mn = get(pos[top[cur]], pos[cur]);
                    mn -= add[cur];
                    umn(best, mn + len);

                    len += add[top[cur]] - add[cur];
                    len += w[pe[top[cur]]];
                    cur = p[top[cur]];
                    jump++;
//                    cout << cur << " " << top[cur] << " " << p[top[cur]] << "\n";
                }
                assert(jump <= 20);

                if (best == 1e18) {
                    cout << "oo\n";
                } else {
                    cout << best << "\n";
                }
            }
//
//            int cur = v;
//            ll best = 1e18;
//            ll len = 0;
//            while (1) {
//                umn(best, len + dp[cur]);
//                if (cur == u) {
//                    break;
//                }
//                len += w[pe[cur]];
//                cur = p[cur];
//            }
//
//            if (best == 1e18) {
//                cout << "oo\n";
//            } else {
//                cout << best << "\n";
//            }
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2772 KB Output is correct
2 Correct 3 ms 2772 KB Output is correct
3 Correct 3 ms 2772 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2772 KB Output is correct
2 Correct 3 ms 2772 KB Output is correct
3 Correct 3 ms 2772 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2900 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 3 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 16152 KB Output is correct
2 Correct 98 ms 15804 KB Output is correct
3 Correct 102 ms 15756 KB Output is correct
4 Correct 107 ms 17548 KB Output is correct
5 Correct 110 ms 17572 KB Output is correct
6 Correct 112 ms 19528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2772 KB Output is correct
2 Correct 3 ms 2772 KB Output is correct
3 Correct 3 ms 2772 KB Output is correct
4 Correct 3 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2900 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 3 ms 2900 KB Output is correct
15 Correct 97 ms 16152 KB Output is correct
16 Correct 98 ms 15804 KB Output is correct
17 Correct 102 ms 15756 KB Output is correct
18 Correct 107 ms 17548 KB Output is correct
19 Correct 110 ms 17572 KB Output is correct
20 Correct 112 ms 19528 KB Output is correct
21 Correct 95 ms 16104 KB Output is correct
22 Correct 106 ms 15744 KB Output is correct
23 Correct 151 ms 15684 KB Output is correct
24 Correct 114 ms 17732 KB Output is correct
25 Correct 111 ms 21320 KB Output is correct
26 Correct 103 ms 19460 KB Output is correct
27 Correct 114 ms 19188 KB Output is correct
28 Correct 118 ms 19012 KB Output is correct
29 Correct 122 ms 20440 KB Output is correct
30 Correct 126 ms 21836 KB Output is correct
31 Correct 101 ms 19584 KB Output is correct
32 Correct 108 ms 19268 KB Output is correct
33 Correct 112 ms 19216 KB Output is correct
34 Correct 141 ms 21060 KB Output is correct
35 Correct 116 ms 23796 KB Output is correct
36 Correct 128 ms 21216 KB Output is correct