답안 #639592

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
639592 2022-09-10T17:10:45 Z classic Birthday gift (IZhO18_treearray) C++14
100 / 100
1058 ms 93340 KB
#include <bits/stdc++.h>
using namespace std;

const int LOG = 20;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> adj(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }
    vector<int> a(m);
    for (int i = 0; i < m; i++) {
        cin >> a[i];
        a[i]--;
    }
    vector<int> dep(n);
    vector<vector<int>> anc(LOG, vector<int>(n, -1));
    function<void(int, int)> dfs = [&](int u, int p) {
        anc[0][u] = p;
        for (int i = 1; i < LOG; i++) {
            if (anc[i - 1][u] != -1) {
                anc[i][u] = anc[i - 1][anc[i - 1][u]];
            }
        }
        for (int v : adj[u]) {
            if (v != p) {
                dep[v] = dep[u] + 1;
                dfs(v, u);
            }
        }
    };
    dfs(0, -1);
    function<int(int, int)> lca = [&](int u, int v) {
        if (dep[u] < dep[v]) {
            swap(u, v);
        }
        int x = dep[u] - dep[v];
        for (int i = LOG - 1; i >= 0; i--) {
            if (x >> i & 1) {
                u = anc[i][u];
            }
        }
        if (u == v) {
            return u;
        }
        for (int i = LOG - 1; i >= 0; i--) {
            if (anc[i][u] != anc[i][v]) {
                u = anc[i][u];
                v = anc[i][v];
            }
        }
        return anc[0][u];
    };
    vector<set<int>> s(n), s1(n);
    for (int i = 0; i < m; i++) {
        if (i < m - 1) {
            s[lca(a[i], a[i + 1])].insert(i);
        }
        s1[a[i]].insert(i);
    }
    while (q--) {
        int op;
        cin >> op;
        if (op == 1) {
            int pos, v;
            cin >> pos >> v;
            pos--;
            v--;
            s1[a[pos]].erase(pos);
            s1[v].insert(pos);
            if (pos < m - 1) {
                s[lca(a[pos], a[pos + 1])].erase(pos);
                s[lca(v, a[pos + 1])].insert(pos);
            }
            if (pos > 0) {
                s[lca(a[pos - 1], a[pos])].erase(pos - 1);
                s[lca(a[pos - 1], v)].insert(pos - 1);
            }
            a[pos] = v;
        } else {
            int l, r, k;
            cin >> l >> r >> k;
            l--;
            r--;
            k--;
            set<int>::iterator it = s[k].lower_bound(l);
            if (it != s[k].end() && *it < r) {
                cout << *it + 1 << ' ' << *it + 2 << '\n';
                continue;
            }
            it = s1[k].lower_bound(l);
            if (it != s1[k].end() && *it <= r) {
                cout << *it + 1 << ' ' << *it + 1 << '\n';
                continue;
            }
            cout << -1 << ' ' << -1 << '\n';
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 0 ms 340 KB n=100
12 Correct 1 ms 320 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 324 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 0 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 0 ms 316 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 320 KB n=100
26 Correct 0 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 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 0 ms 340 KB n=100
12 Correct 1 ms 320 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 324 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 0 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 0 ms 316 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 320 KB n=100
26 Correct 0 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 456 KB n=500
31 Correct 1 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 2 ms 468 KB n=500
34 Correct 1 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 468 KB n=500
37 Correct 1 ms 468 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 468 KB n=500
40 Correct 1 ms 468 KB n=500
41 Correct 1 ms 456 KB n=500
42 Correct 1 ms 468 KB n=500
43 Correct 2 ms 452 KB n=500
44 Correct 1 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 468 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 2 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 1 ms 456 KB n=500
52 Correct 1 ms 596 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 452 KB n=500
57 Correct 1 ms 456 KB n=500
58 Correct 1 ms 468 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 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 0 ms 340 KB n=100
12 Correct 1 ms 320 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 324 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 0 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 0 ms 316 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 320 KB n=100
26 Correct 0 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 456 KB n=500
31 Correct 1 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 2 ms 468 KB n=500
34 Correct 1 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 468 KB n=500
37 Correct 1 ms 468 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 468 KB n=500
40 Correct 1 ms 468 KB n=500
41 Correct 1 ms 456 KB n=500
42 Correct 1 ms 468 KB n=500
43 Correct 2 ms 452 KB n=500
44 Correct 1 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 468 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 2 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 1 ms 456 KB n=500
52 Correct 1 ms 596 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 452 KB n=500
57 Correct 1 ms 456 KB n=500
58 Correct 1 ms 468 KB n=500
59 Correct 3 ms 980 KB n=2000
60 Correct 3 ms 1108 KB n=2000
61 Correct 3 ms 1108 KB n=2000
62 Correct 3 ms 980 KB n=2000
63 Correct 4 ms 980 KB n=2000
64 Correct 4 ms 1108 KB n=2000
65 Correct 4 ms 980 KB n=2000
66 Correct 3 ms 1108 KB n=2000
67 Correct 4 ms 980 KB n=2000
68 Correct 3 ms 1108 KB n=2000
69 Correct 3 ms 980 KB n=2000
70 Correct 3 ms 980 KB n=2000
71 Correct 3 ms 980 KB n=2000
72 Correct 2 ms 980 KB n=2000
73 Correct 3 ms 980 KB n=2000
74 Correct 3 ms 972 KB n=1844
75 Correct 2 ms 980 KB n=2000
76 Correct 3 ms 980 KB n=2000
77 Correct 4 ms 980 KB n=2000
78 Correct 3 ms 980 KB n=2000
79 Correct 3 ms 968 KB n=2000
80 Correct 3 ms 1112 KB n=2000
81 Correct 4 ms 1108 KB n=2000
82 Correct 3 ms 1044 KB n=2000
83 Correct 3 ms 1108 KB n=2000
84 Correct 3 ms 980 KB n=2000
85 Correct 4 ms 1100 KB n=2000
86 Correct 3 ms 980 KB n=2000
87 Correct 3 ms 968 KB n=2000
88 Correct 3 ms 1232 KB n=2000
89 Correct 3 ms 1108 KB n=2000
90 Correct 3 ms 1104 KB n=2000
91 Correct 3 ms 980 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 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 1 ms 340 KB n=100
11 Correct 0 ms 340 KB n=100
12 Correct 1 ms 320 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 324 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 0 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 0 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 1 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 0 ms 316 KB n=100
24 Correct 1 ms 340 KB n=100
25 Correct 1 ms 320 KB n=100
26 Correct 0 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 456 KB n=500
31 Correct 1 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 2 ms 468 KB n=500
34 Correct 1 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 468 KB n=500
37 Correct 1 ms 468 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 468 KB n=500
40 Correct 1 ms 468 KB n=500
41 Correct 1 ms 456 KB n=500
42 Correct 1 ms 468 KB n=500
43 Correct 2 ms 452 KB n=500
44 Correct 1 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 468 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 2 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 1 ms 456 KB n=500
52 Correct 1 ms 596 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 452 KB n=500
57 Correct 1 ms 456 KB n=500
58 Correct 1 ms 468 KB n=500
59 Correct 3 ms 980 KB n=2000
60 Correct 3 ms 1108 KB n=2000
61 Correct 3 ms 1108 KB n=2000
62 Correct 3 ms 980 KB n=2000
63 Correct 4 ms 980 KB n=2000
64 Correct 4 ms 1108 KB n=2000
65 Correct 4 ms 980 KB n=2000
66 Correct 3 ms 1108 KB n=2000
67 Correct 4 ms 980 KB n=2000
68 Correct 3 ms 1108 KB n=2000
69 Correct 3 ms 980 KB n=2000
70 Correct 3 ms 980 KB n=2000
71 Correct 3 ms 980 KB n=2000
72 Correct 2 ms 980 KB n=2000
73 Correct 3 ms 980 KB n=2000
74 Correct 3 ms 972 KB n=1844
75 Correct 2 ms 980 KB n=2000
76 Correct 3 ms 980 KB n=2000
77 Correct 4 ms 980 KB n=2000
78 Correct 3 ms 980 KB n=2000
79 Correct 3 ms 968 KB n=2000
80 Correct 3 ms 1112 KB n=2000
81 Correct 4 ms 1108 KB n=2000
82 Correct 3 ms 1044 KB n=2000
83 Correct 3 ms 1108 KB n=2000
84 Correct 3 ms 980 KB n=2000
85 Correct 4 ms 1100 KB n=2000
86 Correct 3 ms 980 KB n=2000
87 Correct 3 ms 968 KB n=2000
88 Correct 3 ms 1232 KB n=2000
89 Correct 3 ms 1108 KB n=2000
90 Correct 3 ms 1104 KB n=2000
91 Correct 3 ms 980 KB n=2000
92 Correct 788 ms 74652 KB n=200000
93 Correct 1002 ms 84224 KB n=200000
94 Correct 952 ms 91008 KB n=200000
95 Correct 861 ms 74316 KB n=200000
96 Correct 772 ms 74320 KB n=200000
97 Correct 987 ms 82288 KB n=200000
98 Correct 780 ms 74508 KB n=200000
99 Correct 963 ms 74588 KB n=200000
100 Correct 803 ms 74380 KB n=200000
101 Correct 892 ms 93340 KB n=200000
102 Correct 457 ms 75604 KB n=200000
103 Correct 454 ms 75556 KB n=200000
104 Correct 456 ms 75760 KB n=200000
105 Correct 447 ms 76048 KB n=200000
106 Correct 470 ms 76024 KB n=200000
107 Correct 481 ms 76008 KB n=200000
108 Correct 862 ms 74624 KB n=200000
109 Correct 845 ms 74476 KB n=200000
110 Correct 861 ms 74452 KB n=200000
111 Correct 760 ms 73808 KB n=200000
112 Correct 881 ms 91572 KB n=200000
113 Correct 973 ms 82272 KB n=200000
114 Correct 759 ms 73848 KB n=200000
115 Correct 1058 ms 78036 KB n=200000
116 Correct 715 ms 74548 KB n=200000
117 Correct 866 ms 92264 KB n=200000
118 Correct 982 ms 80116 KB n=200000
119 Correct 711 ms 74672 KB n=200000
120 Correct 846 ms 92496 KB n=200000
121 Correct 837 ms 92416 KB n=200000
122 Correct 844 ms 92924 KB n=200000
123 Correct 462 ms 75836 KB n=200000
124 Correct 177 ms 19148 KB n=25264