답안 #720959

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
720959 2023-04-09T22:54:30 Z thimote75 Birthday gift (IZhO18_treearray) C++14
56 / 100
4000 ms 54724 KB
#include <bits/stdc++.h>

using namespace std;
#define idata vector<int>
#define graph vector<idata>

const int MAX_K = 20;

int nbNodes, seqSize, nbQuery;
graph roads;

idata depth;
idata parents;
graph parents_2k;

void dfs (int node, int parent, int l_depth) {
    depth     [node]    = l_depth;
    parents   [node]    = parent;
    parents_2k[node][0] = parent;

    for (int next : roads[node]) if (next != parent)
        dfs(next, node, l_depth + 1);
}
void propagate () {
    for (int k = 0; k + 1 < MAX_K; k ++) {
        for (int i = 0; i < nbNodes; i ++) {
            if (parents_2k[i][k] == -1) continue ;
        
            parents_2k[i][k + 1]
             = parents_2k[parents_2k[i][k]][k];
        }
    }
}

int jump (int n, int k) {
    for (int i = 0; i < MAX_K; i ++)
        if (((1 << i) & k) != 0)
            n = parents_2k[n][i];

    return n;
}

int lca (int a, int b) {
    if (depth[a] > depth[b]) return lca(b, a);

    b = jump(b, depth[b] - depth[a]);
    if (a == b) return a;

    for (int i = MAX_K - 1; i >= 0; i --) {
        if (parents_2k[a][i] == parents_2k[b][i]) continue ;

        a = parents_2k[a][i];
        b = parents_2k[b][i];
    }

    if (a == b) return a;
    return parents[a];
}

idata sequence;
idata lca_sequence;

void compute (int i) {
    lca_sequence[i] = lca(sequence[i], sequence[i + 1]);
}

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

    cin >> nbNodes >> seqSize >> nbQuery;

    roads.resize(nbNodes);
    for (int iR = 1; iR < nbNodes; iR ++) {
        int a, b;
        cin >> a >> b;
        a --; b --;

        roads[a].push_back(b);
        roads[b].push_back(a);
    }

    depth     .resize(nbNodes);
    parents   .resize(nbNodes);
    parents_2k.resize(nbNodes, idata(MAX_K, -1));
    dfs(0, -1, 0);
    propagate();

    sequence.resize(seqSize);
    for (int i = 0; i < seqSize; i ++) {
        cin >> sequence[i];
        sequence[i] --;
    }
    lca_sequence.resize(seqSize - 1);
    for (int i = 0; i + 1 < seqSize; i ++)
        compute(i);
    
    for (int iQ = 0; iQ < nbQuery; iQ ++) {
        int type, a, b, c;
        cin >> type >> a >> b;
            a --; b --;

        if (type == 2) {
            cin >> c; c --;

            int left  = -1;
            int right = -1;
            for (int j = a; j <= b; j ++) {
                if (j != b && lca_sequence[j] == c) {
                    left  = j;
                    right = j + 1;
                    break ;
                }
                if (sequence[j] == c) {
                    left  = j;
                    right = j;
                    break;
                }
            }

            if (left == -1) cout << "-1 -1\n";
            else 
                cout << left + 1 << " " << right + 1 << endl;
        } else {
            sequence[a] = b;

            if (a != 0) compute(a - 1);
            if (a != seqSize) compute(a);
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 324 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 212 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 320 KB n=100
9 Correct 1 ms 320 KB n=100
10 Correct 1 ms 320 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 212 KB n=100
14 Correct 1 ms 324 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 324 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 316 KB n=100
22 Correct 1 ms 212 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 212 KB n=100
25 Correct 1 ms 212 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 324 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 212 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 320 KB n=100
9 Correct 1 ms 320 KB n=100
10 Correct 1 ms 320 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 212 KB n=100
14 Correct 1 ms 324 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 324 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 316 KB n=100
22 Correct 1 ms 212 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 212 KB n=100
25 Correct 1 ms 212 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 2 ms 340 KB n=500
29 Correct 1 ms 340 KB n=500
30 Correct 1 ms 340 KB n=500
31 Correct 1 ms 340 KB n=500
32 Correct 1 ms 328 KB n=500
33 Correct 2 ms 340 KB n=500
34 Correct 1 ms 340 KB n=500
35 Correct 1 ms 340 KB n=500
36 Correct 1 ms 340 KB n=500
37 Correct 2 ms 328 KB n=500
38 Correct 2 ms 340 KB n=500
39 Correct 1 ms 340 KB n=500
40 Correct 1 ms 340 KB n=500
41 Correct 1 ms 340 KB n=500
42 Correct 1 ms 340 KB n=500
43 Correct 2 ms 328 KB n=500
44 Correct 1 ms 340 KB n=500
45 Correct 1 ms 328 KB n=500
46 Correct 1 ms 340 KB n=500
47 Correct 2 ms 340 KB n=500
48 Correct 1 ms 340 KB n=500
49 Correct 1 ms 340 KB n=500
50 Correct 1 ms 340 KB n=500
51 Correct 1 ms 332 KB n=500
52 Correct 1 ms 340 KB n=500
53 Correct 1 ms 340 KB n=500
54 Correct 1 ms 340 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 324 KB n=500
57 Correct 1 ms 340 KB n=500
58 Correct 1 ms 332 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 324 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 212 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 320 KB n=100
9 Correct 1 ms 320 KB n=100
10 Correct 1 ms 320 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 212 KB n=100
14 Correct 1 ms 324 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 324 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 316 KB n=100
22 Correct 1 ms 212 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 212 KB n=100
25 Correct 1 ms 212 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 2 ms 340 KB n=500
29 Correct 1 ms 340 KB n=500
30 Correct 1 ms 340 KB n=500
31 Correct 1 ms 340 KB n=500
32 Correct 1 ms 328 KB n=500
33 Correct 2 ms 340 KB n=500
34 Correct 1 ms 340 KB n=500
35 Correct 1 ms 340 KB n=500
36 Correct 1 ms 340 KB n=500
37 Correct 2 ms 328 KB n=500
38 Correct 2 ms 340 KB n=500
39 Correct 1 ms 340 KB n=500
40 Correct 1 ms 340 KB n=500
41 Correct 1 ms 340 KB n=500
42 Correct 1 ms 340 KB n=500
43 Correct 2 ms 328 KB n=500
44 Correct 1 ms 340 KB n=500
45 Correct 1 ms 328 KB n=500
46 Correct 1 ms 340 KB n=500
47 Correct 2 ms 340 KB n=500
48 Correct 1 ms 340 KB n=500
49 Correct 1 ms 340 KB n=500
50 Correct 1 ms 340 KB n=500
51 Correct 1 ms 332 KB n=500
52 Correct 1 ms 340 KB n=500
53 Correct 1 ms 340 KB n=500
54 Correct 1 ms 340 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 324 KB n=500
57 Correct 1 ms 340 KB n=500
58 Correct 1 ms 332 KB n=500
59 Correct 3 ms 764 KB n=2000
60 Correct 3 ms 724 KB n=2000
61 Correct 4 ms 812 KB n=2000
62 Correct 4 ms 724 KB n=2000
63 Correct 3 ms 724 KB n=2000
64 Correct 5 ms 720 KB n=2000
65 Correct 4 ms 724 KB n=2000
66 Correct 4 ms 728 KB n=2000
67 Correct 4 ms 724 KB n=2000
68 Correct 5 ms 724 KB n=2000
69 Correct 6 ms 764 KB n=2000
70 Correct 6 ms 724 KB n=2000
71 Correct 6 ms 724 KB n=2000
72 Correct 4 ms 724 KB n=2000
73 Correct 3 ms 724 KB n=2000
74 Correct 3 ms 596 KB n=1844
75 Correct 4 ms 724 KB n=2000
76 Correct 5 ms 724 KB n=2000
77 Correct 5 ms 724 KB n=2000
78 Correct 5 ms 720 KB n=2000
79 Correct 3 ms 724 KB n=2000
80 Correct 5 ms 724 KB n=2000
81 Correct 6 ms 724 KB n=2000
82 Correct 3 ms 724 KB n=2000
83 Correct 4 ms 796 KB n=2000
84 Correct 6 ms 724 KB n=2000
85 Correct 4 ms 724 KB n=2000
86 Correct 4 ms 724 KB n=2000
87 Correct 4 ms 712 KB n=2000
88 Correct 5 ms 844 KB n=2000
89 Correct 6 ms 724 KB n=2000
90 Correct 5 ms 724 KB n=2000
91 Correct 5 ms 712 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 324 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 1 ms 212 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 320 KB n=100
9 Correct 1 ms 320 KB n=100
10 Correct 1 ms 320 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 212 KB n=100
14 Correct 1 ms 324 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 324 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 316 KB n=100
22 Correct 1 ms 212 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 212 KB n=100
25 Correct 1 ms 212 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 2 ms 340 KB n=500
29 Correct 1 ms 340 KB n=500
30 Correct 1 ms 340 KB n=500
31 Correct 1 ms 340 KB n=500
32 Correct 1 ms 328 KB n=500
33 Correct 2 ms 340 KB n=500
34 Correct 1 ms 340 KB n=500
35 Correct 1 ms 340 KB n=500
36 Correct 1 ms 340 KB n=500
37 Correct 2 ms 328 KB n=500
38 Correct 2 ms 340 KB n=500
39 Correct 1 ms 340 KB n=500
40 Correct 1 ms 340 KB n=500
41 Correct 1 ms 340 KB n=500
42 Correct 1 ms 340 KB n=500
43 Correct 2 ms 328 KB n=500
44 Correct 1 ms 340 KB n=500
45 Correct 1 ms 328 KB n=500
46 Correct 1 ms 340 KB n=500
47 Correct 2 ms 340 KB n=500
48 Correct 1 ms 340 KB n=500
49 Correct 1 ms 340 KB n=500
50 Correct 1 ms 340 KB n=500
51 Correct 1 ms 332 KB n=500
52 Correct 1 ms 340 KB n=500
53 Correct 1 ms 340 KB n=500
54 Correct 1 ms 340 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 324 KB n=500
57 Correct 1 ms 340 KB n=500
58 Correct 1 ms 332 KB n=500
59 Correct 3 ms 764 KB n=2000
60 Correct 3 ms 724 KB n=2000
61 Correct 4 ms 812 KB n=2000
62 Correct 4 ms 724 KB n=2000
63 Correct 3 ms 724 KB n=2000
64 Correct 5 ms 720 KB n=2000
65 Correct 4 ms 724 KB n=2000
66 Correct 4 ms 728 KB n=2000
67 Correct 4 ms 724 KB n=2000
68 Correct 5 ms 724 KB n=2000
69 Correct 6 ms 764 KB n=2000
70 Correct 6 ms 724 KB n=2000
71 Correct 6 ms 724 KB n=2000
72 Correct 4 ms 724 KB n=2000
73 Correct 3 ms 724 KB n=2000
74 Correct 3 ms 596 KB n=1844
75 Correct 4 ms 724 KB n=2000
76 Correct 5 ms 724 KB n=2000
77 Correct 5 ms 724 KB n=2000
78 Correct 5 ms 720 KB n=2000
79 Correct 3 ms 724 KB n=2000
80 Correct 5 ms 724 KB n=2000
81 Correct 6 ms 724 KB n=2000
82 Correct 3 ms 724 KB n=2000
83 Correct 4 ms 796 KB n=2000
84 Correct 6 ms 724 KB n=2000
85 Correct 4 ms 724 KB n=2000
86 Correct 4 ms 724 KB n=2000
87 Correct 4 ms 712 KB n=2000
88 Correct 5 ms 844 KB n=2000
89 Correct 6 ms 724 KB n=2000
90 Correct 5 ms 724 KB n=2000
91 Correct 5 ms 712 KB n=2000
92 Correct 502 ms 46300 KB n=200000
93 Correct 3697 ms 51504 KB n=200000
94 Execution timed out 4043 ms 54724 KB Time limit exceeded
95 Halted 0 ms 0 KB -