답안 #789106

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
789106 2023-07-21T04:53:05 Z 반딧불(#10041) Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 18772 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, k, q;
vector<int> link[100002];

int inCnt, in[100002], out[100002];
int par[100002], LCADB[100002][20], depth[100002];
int arr[100002];

void dfs(int x, int p=-1){
    in[x] = ++inCnt;
    for(auto y: link[x]){
        if(y==p) continue;
        par[y] = x, depth[y] = depth[x] + 1;
        dfs(y, x);
    }
    out[x] = inCnt;
}

int getLCA(int x, int y){
    if(depth[x] > depth[y]) swap(x, y);
    for(int d=19; d>=0; d--) if((depth[y] - depth[x]) & (1<<d)) y = LCADB[y][d];
    if(x==y) return x;
    for(int d=19; d>=0; d--) if(LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d];
    return par[x];
}

int getDist(int x, int y){
    return depth[x] + depth[y] - depth[getLCA(x, y)] * 2;
}

int main(){
    scanf("%d %d %d", &n, &k, &q);
    for(int i=1; i<n; i++){
        int x, y;
        scanf("%d %d", &x, &y);
        link[x].push_back(y);
        link[y].push_back(x);
    }
    dfs(1);
    for(int i=1; i<=n; i++) LCADB[i][0] = par[i];
    for(int d=1; d<20; d++) for(int i=1; i<=n; i++) LCADB[i][d] = LCADB[LCADB[i][d-1]][d-1];

    for(int i=1; i<=k; i++) scanf("%d", &arr[i]);
    while(q--){
        int l, r;
        scanf("%d %d", &l, &r);
        vector<int> vec;
        for(int i=l; i<=r; i++) vec.push_back(arr[i]);
        sort(vec.begin(), vec.end(), [&](int x, int y){return in[x] < in[y];});

        ll ans = 0;
        for(int i=0; i<(int)vec.size(); i++) ans += getDist(vec[i], vec[(i+1)%(int)vec.size()]);
        printf("%lld\n", ans/2+1);
    }
}

Compilation message

tourism.cpp: In function 'int main()':
tourism.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     scanf("%d %d %d", &n, &k, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
tourism.cpp:48:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |     for(int i=1; i<=k; i++) scanf("%d", &arr[i]);
      |                             ~~~~~^~~~~~~~~~~~~~~
tourism.cpp:51:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         scanf("%d %d", &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 4 ms 2644 KB Output is correct
5 Correct 3 ms 2664 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 3 ms 2660 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 4 ms 2644 KB Output is correct
10 Correct 4 ms 2728 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Correct 9 ms 2644 KB Output is correct
13 Correct 8 ms 2660 KB Output is correct
14 Correct 9 ms 2732 KB Output is correct
15 Correct 4 ms 2644 KB Output is correct
16 Correct 4 ms 2748 KB Output is correct
17 Correct 4 ms 2644 KB Output is correct
18 Correct 4 ms 2644 KB Output is correct
19 Correct 4 ms 2644 KB Output is correct
20 Correct 4 ms 2736 KB Output is correct
21 Correct 4 ms 2664 KB Output is correct
22 Correct 4 ms 2644 KB Output is correct
23 Correct 5 ms 2668 KB Output is correct
24 Correct 4 ms 2644 KB Output is correct
25 Correct 4 ms 2644 KB Output is correct
26 Correct 4 ms 2660 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 4 ms 2644 KB Output is correct
5 Correct 3 ms 2664 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 3 ms 2660 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 4 ms 2644 KB Output is correct
10 Correct 4 ms 2728 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Correct 9 ms 2644 KB Output is correct
13 Correct 8 ms 2660 KB Output is correct
14 Correct 9 ms 2732 KB Output is correct
15 Correct 4 ms 2644 KB Output is correct
16 Correct 4 ms 2748 KB Output is correct
17 Correct 4 ms 2644 KB Output is correct
18 Correct 4 ms 2644 KB Output is correct
19 Correct 4 ms 2644 KB Output is correct
20 Correct 4 ms 2736 KB Output is correct
21 Correct 4 ms 2664 KB Output is correct
22 Correct 4 ms 2644 KB Output is correct
23 Correct 5 ms 2668 KB Output is correct
24 Correct 4 ms 2644 KB Output is correct
25 Correct 4 ms 2644 KB Output is correct
26 Correct 4 ms 2660 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 60 ms 2904 KB Output is correct
31 Correct 85 ms 2904 KB Output is correct
32 Correct 123 ms 2984 KB Output is correct
33 Correct 135 ms 2980 KB Output is correct
34 Correct 120 ms 2900 KB Output is correct
35 Correct 339 ms 2988 KB Output is correct
36 Correct 341 ms 2984 KB Output is correct
37 Correct 352 ms 2976 KB Output is correct
38 Correct 104 ms 3068 KB Output is correct
39 Correct 111 ms 3084 KB Output is correct
40 Correct 101 ms 3080 KB Output is correct
41 Correct 294 ms 3096 KB Output is correct
42 Correct 335 ms 3224 KB Output is correct
43 Correct 297 ms 3060 KB Output is correct
44 Correct 102 ms 3028 KB Output is correct
45 Correct 105 ms 3028 KB Output is correct
46 Correct 106 ms 3028 KB Output is correct
47 Correct 293 ms 3028 KB Output is correct
48 Correct 305 ms 3008 KB Output is correct
49 Correct 294 ms 3028 KB Output is correct
50 Correct 111 ms 2900 KB Output is correct
51 Correct 115 ms 2992 KB Output is correct
52 Correct 119 ms 3116 KB Output is correct
53 Correct 130 ms 2988 KB Output is correct
54 Correct 113 ms 2984 KB Output is correct
55 Correct 111 ms 2992 KB Output is correct
56 Correct 34 ms 2676 KB Output is correct
57 Correct 3 ms 2900 KB Output is correct
58 Correct 3 ms 2900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2660 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 34 ms 2644 KB Output is correct
4 Execution timed out 5028 ms 15748 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 48 ms 10644 KB Output is correct
3 Correct 39 ms 11668 KB Output is correct
4 Correct 38 ms 12536 KB Output is correct
5 Correct 88 ms 17936 KB Output is correct
6 Correct 60 ms 17684 KB Output is correct
7 Correct 65 ms 17496 KB Output is correct
8 Correct 83 ms 17320 KB Output is correct
9 Correct 60 ms 17328 KB Output is correct
10 Correct 62 ms 17372 KB Output is correct
11 Correct 61 ms 17400 KB Output is correct
12 Correct 62 ms 17424 KB Output is correct
13 Correct 62 ms 17524 KB Output is correct
14 Correct 69 ms 17732 KB Output is correct
15 Correct 79 ms 18716 KB Output is correct
16 Correct 86 ms 17868 KB Output is correct
17 Correct 70 ms 17892 KB Output is correct
18 Correct 70 ms 17844 KB Output is correct
19 Correct 70 ms 18028 KB Output is correct
20 Correct 80 ms 17740 KB Output is correct
21 Correct 83 ms 17592 KB Output is correct
22 Correct 74 ms 17392 KB Output is correct
23 Correct 78 ms 17436 KB Output is correct
24 Correct 66 ms 17416 KB Output is correct
25 Correct 75 ms 17352 KB Output is correct
26 Correct 80 ms 17368 KB Output is correct
27 Correct 79 ms 17336 KB Output is correct
28 Correct 91 ms 17264 KB Output is correct
29 Correct 113 ms 17432 KB Output is correct
30 Correct 98 ms 17500 KB Output is correct
31 Correct 91 ms 17528 KB Output is correct
32 Correct 93 ms 17712 KB Output is correct
33 Correct 93 ms 18044 KB Output is correct
34 Correct 89 ms 18772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2656 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 36 ms 2700 KB Output is correct
4 Execution timed out 5023 ms 12248 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 4 ms 2644 KB Output is correct
5 Correct 3 ms 2664 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 3 ms 2660 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 4 ms 2644 KB Output is correct
10 Correct 4 ms 2728 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Correct 9 ms 2644 KB Output is correct
13 Correct 8 ms 2660 KB Output is correct
14 Correct 9 ms 2732 KB Output is correct
15 Correct 4 ms 2644 KB Output is correct
16 Correct 4 ms 2748 KB Output is correct
17 Correct 4 ms 2644 KB Output is correct
18 Correct 4 ms 2644 KB Output is correct
19 Correct 4 ms 2644 KB Output is correct
20 Correct 4 ms 2736 KB Output is correct
21 Correct 4 ms 2664 KB Output is correct
22 Correct 4 ms 2644 KB Output is correct
23 Correct 5 ms 2668 KB Output is correct
24 Correct 4 ms 2644 KB Output is correct
25 Correct 4 ms 2644 KB Output is correct
26 Correct 4 ms 2660 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 60 ms 2904 KB Output is correct
31 Correct 85 ms 2904 KB Output is correct
32 Correct 123 ms 2984 KB Output is correct
33 Correct 135 ms 2980 KB Output is correct
34 Correct 120 ms 2900 KB Output is correct
35 Correct 339 ms 2988 KB Output is correct
36 Correct 341 ms 2984 KB Output is correct
37 Correct 352 ms 2976 KB Output is correct
38 Correct 104 ms 3068 KB Output is correct
39 Correct 111 ms 3084 KB Output is correct
40 Correct 101 ms 3080 KB Output is correct
41 Correct 294 ms 3096 KB Output is correct
42 Correct 335 ms 3224 KB Output is correct
43 Correct 297 ms 3060 KB Output is correct
44 Correct 102 ms 3028 KB Output is correct
45 Correct 105 ms 3028 KB Output is correct
46 Correct 106 ms 3028 KB Output is correct
47 Correct 293 ms 3028 KB Output is correct
48 Correct 305 ms 3008 KB Output is correct
49 Correct 294 ms 3028 KB Output is correct
50 Correct 111 ms 2900 KB Output is correct
51 Correct 115 ms 2992 KB Output is correct
52 Correct 119 ms 3116 KB Output is correct
53 Correct 130 ms 2988 KB Output is correct
54 Correct 113 ms 2984 KB Output is correct
55 Correct 111 ms 2992 KB Output is correct
56 Correct 34 ms 2676 KB Output is correct
57 Correct 3 ms 2900 KB Output is correct
58 Correct 3 ms 2900 KB Output is correct
59 Correct 1 ms 2660 KB Output is correct
60 Correct 3 ms 2644 KB Output is correct
61 Correct 34 ms 2644 KB Output is correct
62 Execution timed out 5028 ms 15748 KB Time limit exceeded
63 Halted 0 ms 0 KB -