Submission #894465

# Submission time Handle Problem Language Result Execution time Memory
894465 2023-12-28T10:18:03 Z nahco314 Birthday gift (IZhO18_treearray) C++14
100 / 100
1734 ms 96304 KB
#include <bits/stdc++.h>
using namespace std;

void dfs(int v, int p, int d, vector<vector<int>>& g, vector<int>& prev, vector<int>& dist) {
    prev[v] = p;
    dist[v] = d;
    for (int nxt : g[v]) {
        if (nxt == p) continue;
        dfs(nxt, v, d + 1, g, prev, dist);
    }
}

int lca(int a, int b, vector<vector<int>>& prevs, vector<int>& dist) {
    if (dist[a] < dist[b]) {
        swap(a, b);
    }
    for (int i = 0; i < 32; i++) {
        if ((dist[a] - dist[b]) >> i & 1) {
            a = prevs[i][a];
        }
    }

    if (a == b) {
        return a;
    }

    for (int i = 31; i >= 0; i--) {
        if (prevs[i][a] != prevs[i][b]) {
            a = prevs[i][a];
            b = prevs[i][b];
        }
    }

    return prevs[0][a];
}

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int> a(m);
    for (int i = 0; i < m; i++) {
        cin >> a[i];
        a[i]--;
    }

    vector<vector<int>> prevs(32, vector<int>(n));
    vector<int> dist(n);
    dfs(0, -1, 0, g, prevs[0], dist);
    for (int i = 1; i < 32; i++) {
        for (int j = 0; j < n; j++) {
            if (prevs[i - 1][j] == -1) {
                prevs[i][j] = -1;
            }
            else {
                prevs[i][j] = prevs[i - 1][prevs[i - 1][j]];
            }
        }
    }

    vector<set<int>> rev_anses(n);
    vector<set<int>> self_anses(n);
    for (int i = 0; i < m - 1; i++) {
        int l = lca(a[i], a[i + 1], prevs, dist);
        rev_anses[l].insert(i);
    }
    for (int i = 0; i < m; i++) {
        self_anses[a[i]].insert(i);
    }

    for (int i = 0; i < q; i++) {
        int query_type;
        cin >> query_type;

        if (query_type == 1) {
            int pos, v;
            cin >> pos >> v;
            pos--;
            v--;
            if (pos != 0) {
                int l = lca(a[pos - 1], a[pos], prevs, dist);
                rev_anses[l].erase(pos - 1);
            }
            if (pos != m - 1) {
                int l = lca(a[pos], a[pos + 1], prevs, dist);
                rev_anses[l].erase(pos);
            }
            self_anses[a[pos]].erase(pos);
            a[pos] = v;
            if (pos != 0) {
                int l = lca(a[pos - 1], a[pos], prevs, dist);
                rev_anses[l].insert(pos - 1);
            }
            if (pos != m - 1) {
                int l = lca(a[pos], a[pos + 1], prevs, dist);
                rev_anses[l].insert(pos);
            }
            self_anses[a[pos]].insert(pos);
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            l--;
            r--;
            v--;
            auto e = rev_anses[v].lower_bound(l);
            if (e == rev_anses[v].end() || *e >= r) {
                e = self_anses[v].lower_bound(l);
                if (e == self_anses[v].end() || *e > r) {
                    cout << -1 << " " << -1 << endl;
                } else {
                    int num = *e + 1;
                    cout << num << " " << num << endl;
                }
            } else {
                int num = *e + 1;
                cout << num << " " << (num + 1) << endl;
            }
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 348 KB n=100
6 Correct 1 ms 348 KB n=100
7 Correct 1 ms 348 KB n=100
8 Correct 1 ms 344 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 348 KB n=100
13 Correct 1 ms 348 KB n=100
14 Correct 1 ms 348 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 1 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 600 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 1 ms 436 KB n=100
22 Correct 1 ms 344 KB n=100
23 Correct 1 ms 600 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 1 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 436 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 348 KB n=100
6 Correct 1 ms 348 KB n=100
7 Correct 1 ms 348 KB n=100
8 Correct 1 ms 344 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 348 KB n=100
13 Correct 1 ms 348 KB n=100
14 Correct 1 ms 348 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 1 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 600 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 1 ms 436 KB n=100
22 Correct 1 ms 344 KB n=100
23 Correct 1 ms 600 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 1 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 436 KB n=100
28 Correct 2 ms 604 KB n=500
29 Correct 2 ms 604 KB n=500
30 Correct 1 ms 604 KB n=500
31 Correct 2 ms 604 KB n=500
32 Correct 2 ms 604 KB n=500
33 Correct 2 ms 604 KB n=500
34 Correct 2 ms 600 KB n=500
35 Correct 2 ms 604 KB n=500
36 Correct 2 ms 604 KB n=500
37 Correct 2 ms 600 KB n=500
38 Correct 2 ms 604 KB n=500
39 Correct 2 ms 600 KB n=500
40 Correct 2 ms 600 KB n=500
41 Correct 2 ms 600 KB n=500
42 Correct 2 ms 604 KB n=500
43 Correct 2 ms 604 KB n=500
44 Correct 2 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 2 ms 600 KB n=500
47 Correct 2 ms 604 KB n=500
48 Correct 2 ms 600 KB n=500
49 Correct 2 ms 600 KB n=500
50 Correct 2 ms 604 KB n=500
51 Correct 2 ms 604 KB n=500
52 Correct 1 ms 604 KB n=500
53 Correct 2 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 344 KB n=278
56 Correct 2 ms 604 KB n=500
57 Correct 2 ms 604 KB n=500
58 Correct 2 ms 604 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 348 KB n=100
6 Correct 1 ms 348 KB n=100
7 Correct 1 ms 348 KB n=100
8 Correct 1 ms 344 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 348 KB n=100
13 Correct 1 ms 348 KB n=100
14 Correct 1 ms 348 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 1 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 600 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 1 ms 436 KB n=100
22 Correct 1 ms 344 KB n=100
23 Correct 1 ms 600 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 1 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 436 KB n=100
28 Correct 2 ms 604 KB n=500
29 Correct 2 ms 604 KB n=500
30 Correct 1 ms 604 KB n=500
31 Correct 2 ms 604 KB n=500
32 Correct 2 ms 604 KB n=500
33 Correct 2 ms 604 KB n=500
34 Correct 2 ms 600 KB n=500
35 Correct 2 ms 604 KB n=500
36 Correct 2 ms 604 KB n=500
37 Correct 2 ms 600 KB n=500
38 Correct 2 ms 604 KB n=500
39 Correct 2 ms 600 KB n=500
40 Correct 2 ms 600 KB n=500
41 Correct 2 ms 600 KB n=500
42 Correct 2 ms 604 KB n=500
43 Correct 2 ms 604 KB n=500
44 Correct 2 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 2 ms 600 KB n=500
47 Correct 2 ms 604 KB n=500
48 Correct 2 ms 600 KB n=500
49 Correct 2 ms 600 KB n=500
50 Correct 2 ms 604 KB n=500
51 Correct 2 ms 604 KB n=500
52 Correct 1 ms 604 KB n=500
53 Correct 2 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 344 KB n=278
56 Correct 2 ms 604 KB n=500
57 Correct 2 ms 604 KB n=500
58 Correct 2 ms 604 KB n=500
59 Correct 6 ms 1116 KB n=2000
60 Correct 6 ms 1624 KB n=2000
61 Correct 6 ms 1116 KB n=2000
62 Correct 6 ms 1116 KB n=2000
63 Correct 7 ms 1372 KB n=2000
64 Correct 6 ms 1116 KB n=2000
65 Correct 5 ms 1116 KB n=2000
66 Correct 8 ms 1112 KB n=2000
67 Correct 6 ms 1116 KB n=2000
68 Correct 6 ms 1112 KB n=2000
69 Correct 6 ms 1116 KB n=2000
70 Correct 6 ms 1112 KB n=2000
71 Correct 6 ms 1112 KB n=2000
72 Correct 6 ms 1112 KB n=2000
73 Correct 6 ms 1116 KB n=2000
74 Correct 7 ms 1116 KB n=1844
75 Correct 8 ms 1116 KB n=2000
76 Correct 6 ms 1116 KB n=2000
77 Correct 6 ms 1112 KB n=2000
78 Correct 6 ms 1116 KB n=2000
79 Correct 6 ms 1112 KB n=2000
80 Correct 6 ms 1116 KB n=2000
81 Correct 6 ms 1116 KB n=2000
82 Correct 5 ms 1112 KB n=2000
83 Correct 6 ms 1116 KB n=2000
84 Correct 5 ms 1116 KB n=2000
85 Correct 6 ms 1116 KB n=2000
86 Correct 6 ms 1116 KB n=2000
87 Correct 7 ms 1116 KB n=2000
88 Correct 5 ms 1360 KB n=2000
89 Correct 5 ms 1368 KB n=2000
90 Correct 6 ms 1368 KB n=2000
91 Correct 6 ms 1116 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 1 ms 348 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 348 KB n=100
6 Correct 1 ms 348 KB n=100
7 Correct 1 ms 348 KB n=100
8 Correct 1 ms 344 KB n=100
9 Correct 1 ms 348 KB n=100
10 Correct 1 ms 348 KB n=100
11 Correct 1 ms 348 KB n=100
12 Correct 1 ms 348 KB n=100
13 Correct 1 ms 348 KB n=100
14 Correct 1 ms 348 KB n=100
15 Correct 1 ms 344 KB n=100
16 Correct 1 ms 348 KB n=100
17 Correct 1 ms 348 KB n=100
18 Correct 1 ms 348 KB n=100
19 Correct 1 ms 600 KB n=100
20 Correct 1 ms 348 KB n=100
21 Correct 1 ms 436 KB n=100
22 Correct 1 ms 344 KB n=100
23 Correct 1 ms 600 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 1 ms 348 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 1 ms 436 KB n=100
28 Correct 2 ms 604 KB n=500
29 Correct 2 ms 604 KB n=500
30 Correct 1 ms 604 KB n=500
31 Correct 2 ms 604 KB n=500
32 Correct 2 ms 604 KB n=500
33 Correct 2 ms 604 KB n=500
34 Correct 2 ms 600 KB n=500
35 Correct 2 ms 604 KB n=500
36 Correct 2 ms 604 KB n=500
37 Correct 2 ms 600 KB n=500
38 Correct 2 ms 604 KB n=500
39 Correct 2 ms 600 KB n=500
40 Correct 2 ms 600 KB n=500
41 Correct 2 ms 600 KB n=500
42 Correct 2 ms 604 KB n=500
43 Correct 2 ms 604 KB n=500
44 Correct 2 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 2 ms 600 KB n=500
47 Correct 2 ms 604 KB n=500
48 Correct 2 ms 600 KB n=500
49 Correct 2 ms 600 KB n=500
50 Correct 2 ms 604 KB n=500
51 Correct 2 ms 604 KB n=500
52 Correct 1 ms 604 KB n=500
53 Correct 2 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 344 KB n=278
56 Correct 2 ms 604 KB n=500
57 Correct 2 ms 604 KB n=500
58 Correct 2 ms 604 KB n=500
59 Correct 6 ms 1116 KB n=2000
60 Correct 6 ms 1624 KB n=2000
61 Correct 6 ms 1116 KB n=2000
62 Correct 6 ms 1116 KB n=2000
63 Correct 7 ms 1372 KB n=2000
64 Correct 6 ms 1116 KB n=2000
65 Correct 5 ms 1116 KB n=2000
66 Correct 8 ms 1112 KB n=2000
67 Correct 6 ms 1116 KB n=2000
68 Correct 6 ms 1112 KB n=2000
69 Correct 6 ms 1116 KB n=2000
70 Correct 6 ms 1112 KB n=2000
71 Correct 6 ms 1112 KB n=2000
72 Correct 6 ms 1112 KB n=2000
73 Correct 6 ms 1116 KB n=2000
74 Correct 7 ms 1116 KB n=1844
75 Correct 8 ms 1116 KB n=2000
76 Correct 6 ms 1116 KB n=2000
77 Correct 6 ms 1112 KB n=2000
78 Correct 6 ms 1116 KB n=2000
79 Correct 6 ms 1112 KB n=2000
80 Correct 6 ms 1116 KB n=2000
81 Correct 6 ms 1116 KB n=2000
82 Correct 5 ms 1112 KB n=2000
83 Correct 6 ms 1116 KB n=2000
84 Correct 5 ms 1116 KB n=2000
85 Correct 6 ms 1116 KB n=2000
86 Correct 6 ms 1116 KB n=2000
87 Correct 7 ms 1116 KB n=2000
88 Correct 5 ms 1360 KB n=2000
89 Correct 5 ms 1368 KB n=2000
90 Correct 6 ms 1368 KB n=2000
91 Correct 6 ms 1116 KB n=2000
92 Correct 1089 ms 81264 KB n=200000
93 Correct 1252 ms 88896 KB n=200000
94 Correct 1162 ms 94272 KB n=200000
95 Correct 1022 ms 80900 KB n=200000
96 Correct 1055 ms 80924 KB n=200000
97 Correct 1334 ms 87036 KB n=200000
98 Correct 1097 ms 81156 KB n=200000
99 Correct 1278 ms 80796 KB n=200000
100 Correct 1055 ms 81136 KB n=200000
101 Correct 1129 ms 96304 KB n=200000
102 Correct 790 ms 82148 KB n=200000
103 Correct 779 ms 82236 KB n=200000
104 Correct 780 ms 82028 KB n=200000
105 Correct 795 ms 82308 KB n=200000
106 Correct 800 ms 82008 KB n=200000
107 Correct 836 ms 82060 KB n=200000
108 Correct 1201 ms 80952 KB n=200000
109 Correct 1242 ms 80752 KB n=200000
110 Correct 1209 ms 80856 KB n=200000
111 Correct 1168 ms 81016 KB n=200000
112 Correct 1423 ms 94612 KB n=200000
113 Correct 1595 ms 87196 KB n=200000
114 Correct 1179 ms 80724 KB n=200000
115 Correct 1734 ms 83332 KB n=200000
116 Correct 1126 ms 80840 KB n=200000
117 Correct 1342 ms 95136 KB n=200000
118 Correct 1671 ms 85188 KB n=200000
119 Correct 1154 ms 80932 KB n=200000
120 Correct 1276 ms 95732 KB n=200000
121 Correct 1253 ms 95720 KB n=200000
122 Correct 1276 ms 95904 KB n=200000
123 Correct 879 ms 81848 KB n=200000
124 Correct 273 ms 19536 KB n=25264