답안 #789310

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
789310 2023-07-21T09:07:05 Z 이동현(#10044) Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 31576 KB
#include <bits/stdc++.h>
#define int long long
#define mi(x, y) (x = min(x, y))
#define ma(x, y) (x = max(x, y))
using namespace std;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> way(n);
    for(int i = 1; i < n; ++i){
        int x, y;
        cin >> x >> y;
        --x, --y;
        way[x].push_back(y);
        way[y].push_back(x);
    }

    vector<int> a(m);
    for(int i = 0; i < m; ++i){
        cin >> a[i];
        --a[i];
    }

    vector<int> dep(n);
    vector<vector<int>> spa(n, vector<int>(20));
    vector<int> in(n), ed(n);
    int inN = 0;

    auto dfs = [&](auto&&self, int x, int pr)->void{
        in[x] = ++inN;
        for(auto&nxt:way[x]){
            if(nxt == pr){
                continue;
            }

            dep[nxt] = dep[x] + 1;
            spa[nxt][0] = x;
            self(self, nxt, x);
        }
        ed[x] = inN;
    };

    dfs(dfs, 0, -1);
    for(int j = 1; j < 20; ++j){
        for(int i = 0; i < n; ++i){
            spa[i][j] = spa[spa[i][j - 1]][j - 1];
        }
    }

    auto getlca = [&](int x, int y){
        if(dep[x] > dep[y]){
            swap(x, y);
        }

        for(int i = 19; i >= 0; --i){
            if(dep[y] - (1 << i) >= dep[x]){
                y = spa[y][i];
            }
        }
        if(x == y) return x;

        for(int i = 19; i >= 0; --i){
            if(spa[x][i] != spa[y][i]){
                x = spa[x][i];
                y = spa[y][i];
            }
        }

        return spa[x][0];
    };

    while(q--){
        int l, r;
        cin >> l >> r;
        --l, --r;

        vector<int> id;
        for(int i = l; i <= r; ++i){
            id.push_back(a[i]);
        }
        sort(id.begin(), id.end(), [&](int&x, int&y){return in[x] < in[y];});
        for(int i = 1; i < r - l + 1; ++i){
            id.push_back(getlca(id[i - 1], id[i]));
        }

        sort(id.begin(), id.end(), [&](int&x, int&y){return in[x] < in[y];});
        // for(auto&i:id){
        //     cout << i << ' ';
        // }
        // cout << endl;

        int ans = 0;
        int nxt = 1;
        auto sol = [&](auto&&self, int x)->void{
            while(nxt < (int)id.size() && in[id[nxt]] <= ed[id[x]]){
                // if(id[nxt] != id[x]) cout << id[nxt] << ' ' << id[x] << ' ' << dep[id[nxt]] << ' ' << dep[id[x]] << endl;
                ans += dep[id[nxt]] - dep[id[x]];
                ++nxt;
                self(self, nxt - 1);
            }
        };
        sol(sol, 0);

        cout << ans + 1 << '\n';
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 3 ms 372 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 5 ms 340 KB Output is correct
10 Correct 5 ms 420 KB Output is correct
11 Correct 5 ms 340 KB Output is correct
12 Correct 12 ms 420 KB Output is correct
13 Correct 12 ms 428 KB Output is correct
14 Correct 11 ms 424 KB Output is correct
15 Correct 4 ms 424 KB Output is correct
16 Correct 4 ms 340 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 340 KB Output is correct
19 Correct 4 ms 340 KB Output is correct
20 Correct 4 ms 340 KB Output is correct
21 Correct 4 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 4 ms 340 KB Output is correct
24 Correct 4 ms 340 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 4 ms 424 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 3 ms 372 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 5 ms 340 KB Output is correct
10 Correct 5 ms 420 KB Output is correct
11 Correct 5 ms 340 KB Output is correct
12 Correct 12 ms 420 KB Output is correct
13 Correct 12 ms 428 KB Output is correct
14 Correct 11 ms 424 KB Output is correct
15 Correct 4 ms 424 KB Output is correct
16 Correct 4 ms 340 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 340 KB Output is correct
19 Correct 4 ms 340 KB Output is correct
20 Correct 4 ms 340 KB Output is correct
21 Correct 4 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 4 ms 340 KB Output is correct
24 Correct 4 ms 340 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 4 ms 424 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 107 ms 788 KB Output is correct
31 Correct 157 ms 780 KB Output is correct
32 Correct 235 ms 940 KB Output is correct
33 Correct 226 ms 952 KB Output is correct
34 Correct 226 ms 952 KB Output is correct
35 Correct 685 ms 948 KB Output is correct
36 Correct 663 ms 948 KB Output is correct
37 Correct 683 ms 940 KB Output is correct
38 Correct 216 ms 988 KB Output is correct
39 Correct 201 ms 992 KB Output is correct
40 Correct 203 ms 988 KB Output is correct
41 Correct 689 ms 984 KB Output is correct
42 Correct 691 ms 1000 KB Output is correct
43 Correct 661 ms 980 KB Output is correct
44 Correct 212 ms 956 KB Output is correct
45 Correct 205 ms 956 KB Output is correct
46 Correct 206 ms 964 KB Output is correct
47 Correct 655 ms 944 KB Output is correct
48 Correct 651 ms 952 KB Output is correct
49 Correct 651 ms 960 KB Output is correct
50 Correct 173 ms 976 KB Output is correct
51 Correct 189 ms 968 KB Output is correct
52 Correct 181 ms 960 KB Output is correct
53 Correct 172 ms 980 KB Output is correct
54 Correct 173 ms 960 KB Output is correct
55 Correct 171 ms 976 KB Output is correct
56 Correct 54 ms 428 KB Output is correct
57 Correct 1 ms 852 KB Output is correct
58 Correct 2 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 55 ms 340 KB Output is correct
4 Execution timed out 5067 ms 22052 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 62 ms 16096 KB Output is correct
3 Correct 62 ms 17512 KB Output is correct
4 Correct 60 ms 19684 KB Output is correct
5 Correct 112 ms 31200 KB Output is correct
6 Correct 121 ms 30660 KB Output is correct
7 Correct 144 ms 29528 KB Output is correct
8 Correct 125 ms 29240 KB Output is correct
9 Correct 131 ms 29092 KB Output is correct
10 Correct 125 ms 29504 KB Output is correct
11 Correct 112 ms 29080 KB Output is correct
12 Correct 109 ms 29112 KB Output is correct
13 Correct 109 ms 30024 KB Output is correct
14 Correct 105 ms 30000 KB Output is correct
15 Correct 90 ms 30116 KB Output is correct
16 Correct 123 ms 31052 KB Output is correct
17 Correct 122 ms 31132 KB Output is correct
18 Correct 113 ms 31044 KB Output is correct
19 Correct 120 ms 31576 KB Output is correct
20 Correct 114 ms 31188 KB Output is correct
21 Correct 109 ms 29500 KB Output is correct
22 Correct 118 ms 28992 KB Output is correct
23 Correct 114 ms 28792 KB Output is correct
24 Correct 141 ms 28672 KB Output is correct
25 Correct 123 ms 28536 KB Output is correct
26 Correct 169 ms 29492 KB Output is correct
27 Correct 163 ms 29496 KB Output is correct
28 Correct 134 ms 29492 KB Output is correct
29 Correct 136 ms 29400 KB Output is correct
30 Correct 140 ms 29020 KB Output is correct
31 Correct 134 ms 29532 KB Output is correct
32 Correct 149 ms 28648 KB Output is correct
33 Correct 111 ms 29552 KB Output is correct
34 Correct 87 ms 29644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 54 ms 340 KB Output is correct
4 Execution timed out 5062 ms 20376 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 3 ms 372 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 5 ms 340 KB Output is correct
10 Correct 5 ms 420 KB Output is correct
11 Correct 5 ms 340 KB Output is correct
12 Correct 12 ms 420 KB Output is correct
13 Correct 12 ms 428 KB Output is correct
14 Correct 11 ms 424 KB Output is correct
15 Correct 4 ms 424 KB Output is correct
16 Correct 4 ms 340 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 340 KB Output is correct
19 Correct 4 ms 340 KB Output is correct
20 Correct 4 ms 340 KB Output is correct
21 Correct 4 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 4 ms 340 KB Output is correct
24 Correct 4 ms 340 KB Output is correct
25 Correct 4 ms 340 KB Output is correct
26 Correct 4 ms 424 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 107 ms 788 KB Output is correct
31 Correct 157 ms 780 KB Output is correct
32 Correct 235 ms 940 KB Output is correct
33 Correct 226 ms 952 KB Output is correct
34 Correct 226 ms 952 KB Output is correct
35 Correct 685 ms 948 KB Output is correct
36 Correct 663 ms 948 KB Output is correct
37 Correct 683 ms 940 KB Output is correct
38 Correct 216 ms 988 KB Output is correct
39 Correct 201 ms 992 KB Output is correct
40 Correct 203 ms 988 KB Output is correct
41 Correct 689 ms 984 KB Output is correct
42 Correct 691 ms 1000 KB Output is correct
43 Correct 661 ms 980 KB Output is correct
44 Correct 212 ms 956 KB Output is correct
45 Correct 205 ms 956 KB Output is correct
46 Correct 206 ms 964 KB Output is correct
47 Correct 655 ms 944 KB Output is correct
48 Correct 651 ms 952 KB Output is correct
49 Correct 651 ms 960 KB Output is correct
50 Correct 173 ms 976 KB Output is correct
51 Correct 189 ms 968 KB Output is correct
52 Correct 181 ms 960 KB Output is correct
53 Correct 172 ms 980 KB Output is correct
54 Correct 173 ms 960 KB Output is correct
55 Correct 171 ms 976 KB Output is correct
56 Correct 54 ms 428 KB Output is correct
57 Correct 1 ms 852 KB Output is correct
58 Correct 2 ms 852 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 2 ms 340 KB Output is correct
61 Correct 55 ms 340 KB Output is correct
62 Execution timed out 5067 ms 22052 KB Time limit exceeded
63 Halted 0 ms 0 KB -