답안 #796275

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796275 2023-07-28T08:44:48 Z finn__ Tourism (JOI23_tourism) C++17
18 / 100
5000 ms 25848 KB
#include <bits/stdc++.h>
using namespace std;

constexpr size_t N = 100000, LGN = 18;

size_t c[N], anc[LGN][N], h[N], preorder[N];
vector<size_t> g[N];

size_t traverse(size_t u, size_t p, size_t i = 0)
{
    preorder[u] = i++;
    for (auto const &v : g[u])
        if (v != p)
        {
            h[v] = h[u] + 1;
            anc[0][v] = u;
            for (size_t i = 1; i < LGN; ++i)
                anc[i][v] = anc[i - 1][anc[i - 1][v]];
            i = traverse(v, u, i);
        }
    return i;
}

size_t lift(size_t u, size_t d)
{
    size_t z = 0;
    while (d)
    {
        if (d & 1)
            u = anc[z][u];
        ++z;
        d >>= 1;
    }
    return u;
}

size_t lca(size_t u, size_t v)
{
    if (h[u] < h[v])
        swap(u, v);
    u = lift(u, h[u] - h[v]);
    if (u == v)
        return u;
    for (size_t i = LGN - 1; i < LGN; --i)
        if (anc[i][u] != anc[i][v])
            u = anc[i][u], v = anc[i][v];
    return anc[0][u];
}

size_t distance(size_t u, size_t v)
{
    return h[u] + h[v] - (h[lca(u, v)] << 1);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    size_t n, m, q;
    cin >> n >> m >> q;

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

    traverse(0, -1);

    for (size_t i = 0; i < m; ++i)
        cin >> c[i], --c[i];

    while (q--)
    {
        size_t l, r;
        cin >> l >> r, --l, --r;
        sort(c + l, c + r + 1, [](size_t u, size_t v)
             { return preorder[u] < preorder[v]; });
        size_t distance_sum = distance(c[l], c[r]);
        for (size_t i = l; i < r; ++i)
            distance_sum += distance(c[i], c[i + 1]);
        cout << (distance_sum >> 1) + 1 << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Incorrect 1 ms 2772 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Incorrect 1 ms 2772 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 51 ms 14760 KB Output is correct
3 Correct 61 ms 16176 KB Output is correct
4 Correct 73 ms 17512 KB Output is correct
5 Correct 104 ms 24476 KB Output is correct
6 Correct 112 ms 24524 KB Output is correct
7 Correct 151 ms 24556 KB Output is correct
8 Correct 132 ms 24568 KB Output is correct
9 Correct 155 ms 24564 KB Output is correct
10 Correct 127 ms 24568 KB Output is correct
11 Correct 132 ms 24528 KB Output is correct
12 Correct 133 ms 24612 KB Output is correct
13 Correct 126 ms 24652 KB Output is correct
14 Correct 157 ms 25000 KB Output is correct
15 Correct 88 ms 25848 KB Output is correct
16 Correct 130 ms 24704 KB Output is correct
17 Correct 128 ms 24604 KB Output is correct
18 Correct 121 ms 24604 KB Output is correct
19 Correct 77 ms 24004 KB Output is correct
20 Correct 75 ms 24000 KB Output is correct
21 Correct 78 ms 24000 KB Output is correct
22 Correct 88 ms 23988 KB Output is correct
23 Correct 96 ms 24068 KB Output is correct
24 Correct 100 ms 24012 KB Output is correct
25 Correct 104 ms 24072 KB Output is correct
26 Correct 115 ms 24096 KB Output is correct
27 Correct 115 ms 24088 KB Output is correct
28 Correct 133 ms 24068 KB Output is correct
29 Correct 123 ms 24100 KB Output is correct
30 Correct 147 ms 24260 KB Output is correct
31 Correct 165 ms 24192 KB Output is correct
32 Correct 128 ms 24400 KB Output is correct
33 Correct 118 ms 24780 KB Output is correct
34 Correct 89 ms 25396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 14 ms 2724 KB Output is correct
4 Execution timed out 5055 ms 15952 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Incorrect 1 ms 2772 KB Output isn't correct
3 Halted 0 ms 0 KB -