답안 #921974

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
921974 2024-02-04T15:31:55 Z 406 공장들 (JOI14_factories) C++17
100 / 100
1751 ms 128944 KB
#include <bits/stdc++.h>
#include "factories.h"
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

using namespace std;
using ar = array<int, 2>;
const int64_t INF = 1ll << 60;
const int N = 5e5 + 5, LG = 19;

int n, T, st[N], fn[N], pr[N][LG];
long long h[N], dist[N];
vector<ar> adj[N];

bool is_anc(int v, int p) {
        return st[p] <= st[v] && fn[v] <= fn[p];
}

void dfs(int v, int p) {
        pr[v][0] = p;
        FOR(i, 0, LG - 1)
                pr[v][i + 1] = pr[pr[v][i]][i];
        st[v] = ++T;
        for (auto [u, w]: adj[v]) if (u != p) {
                h[u] = h[v] + w;
                dfs(u, v);
        }
        fn[v] = ++T;
}

int LCA(int u, int v) {
        if (is_anc(u, v)) return v;
        if (is_anc(v, u)) return u;
        for (int i = LG - 1; i >= 0; --i) if (!is_anc(u, pr[v][i]))
                v = pr[v][i];
        assert(!is_anc(u, v) && is_anc(u, pr[v][0]));
        return pr[v][0];
}

void Init(int NV, int A[], int B[], int D[]) {
        n = NV;
        FOR(i, 0, n - 1) {
                adj[A[i]].push_back({B[i], D[i]});
                adj[B[i]].push_back({A[i], D[i]});
        }
        dfs(0, 0);
        fill(dist, dist + N, INF);
}

int all[2 * N];

long long Query(int S, int X[], int T, int Y[]) {
        FOR(i, 0, S) dist[X[i]] = 0;

        copy(X, X + S, all);
        copy(Y, Y + T, all + S);

        int q = S + T, qq = q - 1;
        sort(all, all + q, [&](const int u, const int v) {return st[u] < st[v];});
        FOR(i, 0, q - 1) all[++qq] = LCA(all[i], all[i + 1]);

        q = qq + 1;
        sort(all, all + q, [&](const int u, const int v) {return fn[u] < fn[v];});
        vector<int> cand;
        FOR(i, 0, q) {
                int t = all[i], w;
                while (cand.size() && is_anc(w = cand.back(), t)) {
                        dist[t] = min(dist[t], dist[w] + h[w] - h[t]);
                        cand.pop_back();
                }
                cand.push_back(all[i]);
        }
        cand.clear();
        reverse(all, all + q);
        //sort(all, all + q, [&](const int u, const int v) {return st[u] < st[v];}); //reverse fn doroste aya?
        FOR(i, 0, q) {
                int t = all[i], w;
                while (cand.size() && !is_anc(t, w = cand.back()))
                        cand.pop_back();
                if (cand.size())
                        dist[t] = min(dist[t], dist[w] + h[t] - h[w]);
                cand.push_back(all[i]);
        }
        long long mn = dist[Y[0]];
        FOR(i, 1, T) mn = min(mn, dist[Y[i]]);
        FOR(i, 0, q) dist[all[i]] = INF;
        return mn;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 42332 KB Output is correct
2 Correct 651 ms 46676 KB Output is correct
3 Correct 617 ms 46736 KB Output is correct
4 Correct 598 ms 46772 KB Output is correct
5 Correct 443 ms 47072 KB Output is correct
6 Correct 516 ms 46836 KB Output is correct
7 Correct 618 ms 46932 KB Output is correct
8 Correct 595 ms 46816 KB Output is correct
9 Correct 443 ms 46932 KB Output is correct
10 Correct 517 ms 46928 KB Output is correct
11 Correct 613 ms 46928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 42332 KB Output is correct
2 Correct 882 ms 104964 KB Output is correct
3 Correct 1082 ms 107448 KB Output is correct
4 Correct 648 ms 105532 KB Output is correct
5 Correct 732 ms 128944 KB Output is correct
6 Correct 1151 ms 107548 KB Output is correct
7 Correct 861 ms 58256 KB Output is correct
8 Correct 531 ms 58132 KB Output is correct
9 Correct 387 ms 60948 KB Output is correct
10 Correct 847 ms 58676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 42332 KB Output is correct
2 Correct 651 ms 46676 KB Output is correct
3 Correct 617 ms 46736 KB Output is correct
4 Correct 598 ms 46772 KB Output is correct
5 Correct 443 ms 47072 KB Output is correct
6 Correct 516 ms 46836 KB Output is correct
7 Correct 618 ms 46932 KB Output is correct
8 Correct 595 ms 46816 KB Output is correct
9 Correct 443 ms 46932 KB Output is correct
10 Correct 517 ms 46928 KB Output is correct
11 Correct 613 ms 46928 KB Output is correct
12 Correct 8 ms 42332 KB Output is correct
13 Correct 882 ms 104964 KB Output is correct
14 Correct 1082 ms 107448 KB Output is correct
15 Correct 648 ms 105532 KB Output is correct
16 Correct 732 ms 128944 KB Output is correct
17 Correct 1151 ms 107548 KB Output is correct
18 Correct 861 ms 58256 KB Output is correct
19 Correct 531 ms 58132 KB Output is correct
20 Correct 387 ms 60948 KB Output is correct
21 Correct 847 ms 58676 KB Output is correct
22 Correct 1673 ms 106064 KB Output is correct
23 Correct 1549 ms 107468 KB Output is correct
24 Correct 1646 ms 108780 KB Output is correct
25 Correct 1619 ms 110108 KB Output is correct
26 Correct 1704 ms 106428 KB Output is correct
27 Correct 1167 ms 125732 KB Output is correct
28 Correct 1103 ms 108552 KB Output is correct
29 Correct 1751 ms 106188 KB Output is correct
30 Correct 1698 ms 105780 KB Output is correct
31 Correct 1714 ms 106132 KB Output is correct
32 Correct 630 ms 64384 KB Output is correct
33 Correct 691 ms 60964 KB Output is correct
34 Correct 835 ms 57192 KB Output is correct
35 Correct 826 ms 57188 KB Output is correct
36 Correct 882 ms 57692 KB Output is correct
37 Correct 876 ms 57684 KB Output is correct