Submission #422405

# Submission time Handle Problem Language Result Execution time Memory
422405 2021-06-10T05:50:14 Z tengiz05 Factories (JOI14_factories) C++17
0 / 100
1334 ms 244160 KB
#include "factories.h"
#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 500000, K = 22, M = 2 * N + 5;
std::vector<std::pair<int, i64>> e[N];
i64 st[M][K + 1];
int lg[M + 1], n, P[N], timer;
i64 D[N];
int pos[M];
void dfs(int u, int p, i64 d = 0){
    P[u] = timer;
    pos[timer++] = d;
    D[u] = d;
    for (auto [v, w] : e[u]) {
        if (v != p) {
            dfs(v, u, d + w);
            pos[timer++] = d;
        }
    }
}
inline int range(int L, int R) {
    if (L > R) std::swap(L, R);
    int j = lg[R - L + 1];
    return std::min(st[L][j], st[R - (1 << j) + 1][j]);
}
inline i64 dist(int u, int v) {
    return D[u] + D[v] - 2 * range(P[u], P[v]);
}
void Init(int n, int A[], int B[], int D[]) {
    lg[1] = 0;
    for (int i = 2; i <= M; i++)
        lg[i] = lg[i / 2] + 1;
    ::n = n;
    for (int i = 0; i < n - 1; i++) {
        e[A[i]].emplace_back(B[i], D[i]);
        e[B[i]].emplace_back(A[i], D[i]);
    }
    dfs(0, 0);
    for (int i = 0; i < M; i++)
        st[i][0] = pos[i];
    for (int j = 1; j <= K; j++)
        for (int i = 0; i + (1 << j) <= M; i++)
            st[i][j] = std::min(st[i][j-1], st[i + (1 << (j - 1))][j - 1]);
}
i64 dp[N];
void dfs2(int u, int p) {
    for (auto [v, w] : e[u]) {
        if (v != p) {
            dfs2(v, u);
            dp[u] = std::min(dp[u], dp[v] + w);
        }
    }
}
void dfs3(int u, int p) {
    for (auto [v, w] : e[u]) {
        if (v != p) {
            dp[v] = std::min(dp[v], dp[u] + w);
            dfs3(v, u);
        }
    }
}
long long Query(int S, int X[], int T, int Y[]) {
    if (S + T <= 1100) {
        i64 ans = 1e16;
        for (int i = 0; i < S; i++) {
            for (int j = 0; j < T; j++) {
                ans = std::min(ans, dist(X[i], Y[j]));
            }
        }
        return ans;
    }
    memset(dp, 0x3f, sizeof dp);
    for (int i = 0; i < S; i++) {
        dp[X[i]] = 0;
    }
    dfs2(0, 0);
    dfs3(0, 0);
    i64 ans =  1e16;
    for (int i = 0; i < T; i++) {
        ans = std::min(ans, dp[Y[i]]);
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 338 ms 196316 KB Output is correct
2 Incorrect 1334 ms 208252 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 308 ms 196132 KB Output is correct
2 Incorrect 1209 ms 244160 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 338 ms 196316 KB Output is correct
2 Incorrect 1334 ms 208252 KB Output isn't correct
3 Halted 0 ms 0 KB -