#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int L = 20;
int n, m, q;
vector<int> g[N];
int a[N];
int fst[N], et[2 * N], lv[N];
int lg2[2 * N];
pair<int, int> anc[L][2 * N];
int time_dfs = 0;
set<int> sf[N], pr[N];
void pre_dfs(int u, int p) {
time_dfs++;
fst[u] = time_dfs;
et[time_dfs] = u;
for (int v : g[u]) {
if (v != p) {
lv[v] = lv[u] + 1;
pre_dfs(v, u);
time_dfs++;
et[time_dfs] = u;
}
}
}
void build_lca() {
for (int i = 2; i < 2 * N; i++) {
lg2[i] = lg2[i >> 1] + 1;
}
for (int j = 0; j < L; j++) {
for (int i = 1; i + (1 << j) - 1 <= time_dfs; i++) {
anc[j][i] = (j == 0 ? pair<int, int>{lv[et[i]], et[i]} : min(anc[j - 1][i], anc[j - 1][i + (1 << (j - 1))]));
}
}
}
int lca(int u, int v) {
if (fst[u] > fst[v]) {
swap(u, v);
}
int w = lg2[fst[v] - fst[u] + 1];
return min(anc[w][fst[u]], anc[w][fst[v] - (1 << w) + 1]).second;
}
void upd(int pos, int v) {
sf[a[pos]].erase(pos);
if (pos > 1) {
pr[lca(a[pos - 1], a[pos])].erase(pos - 1);
}
if (pos < m) {
pr[lca(a[pos], a[pos + 1])].erase(pos);
}
a[pos] = v;
sf[a[pos]].insert(pos);
if (pos > 1) {
pr[lca(a[pos - 1], a[pos])].insert(pos - 1);
}
if (pos < m) {
pr[lca(a[pos], a[pos + 1])].insert(pos);
}
}
void solve(int l, int r, int v) {
auto it = sf[v].lower_bound(l);
if (it != sf[v].end()) {
int x = *it;
if (x <= r) {
cout << x << ' ' << x << '\n';
return;
}
}
it = pr[v].lower_bound(l);
if (it != pr[v].end()) {
int x = *it;
if (x <= r - 1) {
cout << x << ' ' << x + 1 << '\n';
return;
}
}
cout << -1 << ' ' << -1 << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> q;
for (int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
pre_dfs(1, 1);
build_lca();
for (int i = 1; i <= m; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
sf[a[i]].insert(i);
if (i < m) {
pr[lca(a[i], a[i + 1])].insert(i);
}
}
while (q--) {
int t;
cin >> t;
if (t == 1) {
int pos, v;
cin >> pos >> v;
upd(pos, v);
} else if (t == 2) {
int l, r, v;
cin >> l >> r >> v;
solve(l, r, v);
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
37468 KB |
n=5 |
2 |
Correct |
9 ms |
45660 KB |
n=100 |
3 |
Correct |
9 ms |
45464 KB |
n=100 |
4 |
Correct |
9 ms |
45660 KB |
n=100 |
5 |
Correct |
9 ms |
45660 KB |
n=100 |
6 |
Correct |
9 ms |
45656 KB |
n=100 |
7 |
Correct |
9 ms |
45660 KB |
n=100 |
8 |
Correct |
9 ms |
45660 KB |
n=100 |
9 |
Correct |
9 ms |
45704 KB |
n=100 |
10 |
Correct |
11 ms |
45660 KB |
n=100 |
11 |
Correct |
9 ms |
45660 KB |
n=100 |
12 |
Correct |
9 ms |
45656 KB |
n=100 |
13 |
Correct |
9 ms |
45656 KB |
n=100 |
14 |
Correct |
9 ms |
45660 KB |
n=100 |
15 |
Correct |
9 ms |
45656 KB |
n=100 |
16 |
Correct |
9 ms |
45656 KB |
n=100 |
17 |
Correct |
10 ms |
45660 KB |
n=100 |
18 |
Correct |
9 ms |
45712 KB |
n=100 |
19 |
Correct |
9 ms |
45660 KB |
n=100 |
20 |
Correct |
9 ms |
45660 KB |
n=100 |
21 |
Correct |
11 ms |
45656 KB |
n=100 |
22 |
Correct |
9 ms |
45656 KB |
n=100 |
23 |
Correct |
9 ms |
45660 KB |
n=100 |
24 |
Correct |
9 ms |
45656 KB |
n=100 |
25 |
Correct |
9 ms |
45660 KB |
n=100 |
26 |
Correct |
9 ms |
39516 KB |
n=12 |
27 |
Correct |
9 ms |
45660 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
37468 KB |
n=5 |
2 |
Correct |
9 ms |
45660 KB |
n=100 |
3 |
Correct |
9 ms |
45464 KB |
n=100 |
4 |
Correct |
9 ms |
45660 KB |
n=100 |
5 |
Correct |
9 ms |
45660 KB |
n=100 |
6 |
Correct |
9 ms |
45656 KB |
n=100 |
7 |
Correct |
9 ms |
45660 KB |
n=100 |
8 |
Correct |
9 ms |
45660 KB |
n=100 |
9 |
Correct |
9 ms |
45704 KB |
n=100 |
10 |
Correct |
11 ms |
45660 KB |
n=100 |
11 |
Correct |
9 ms |
45660 KB |
n=100 |
12 |
Correct |
9 ms |
45656 KB |
n=100 |
13 |
Correct |
9 ms |
45656 KB |
n=100 |
14 |
Correct |
9 ms |
45660 KB |
n=100 |
15 |
Correct |
9 ms |
45656 KB |
n=100 |
16 |
Correct |
9 ms |
45656 KB |
n=100 |
17 |
Correct |
10 ms |
45660 KB |
n=100 |
18 |
Correct |
9 ms |
45712 KB |
n=100 |
19 |
Correct |
9 ms |
45660 KB |
n=100 |
20 |
Correct |
9 ms |
45660 KB |
n=100 |
21 |
Correct |
11 ms |
45656 KB |
n=100 |
22 |
Correct |
9 ms |
45656 KB |
n=100 |
23 |
Correct |
9 ms |
45660 KB |
n=100 |
24 |
Correct |
9 ms |
45656 KB |
n=100 |
25 |
Correct |
9 ms |
45660 KB |
n=100 |
26 |
Correct |
9 ms |
39516 KB |
n=12 |
27 |
Correct |
9 ms |
45660 KB |
n=100 |
28 |
Correct |
9 ms |
49756 KB |
n=500 |
29 |
Correct |
10 ms |
49756 KB |
n=500 |
30 |
Correct |
10 ms |
49892 KB |
n=500 |
31 |
Correct |
10 ms |
49756 KB |
n=500 |
32 |
Correct |
10 ms |
49756 KB |
n=500 |
33 |
Correct |
10 ms |
49756 KB |
n=500 |
34 |
Correct |
10 ms |
49752 KB |
n=500 |
35 |
Correct |
10 ms |
49904 KB |
n=500 |
36 |
Correct |
10 ms |
49756 KB |
n=500 |
37 |
Correct |
9 ms |
49752 KB |
n=500 |
38 |
Correct |
10 ms |
49880 KB |
n=500 |
39 |
Correct |
10 ms |
49756 KB |
n=500 |
40 |
Correct |
10 ms |
49756 KB |
n=500 |
41 |
Correct |
9 ms |
49712 KB |
n=500 |
42 |
Correct |
10 ms |
49756 KB |
n=500 |
43 |
Correct |
10 ms |
49756 KB |
n=500 |
44 |
Correct |
10 ms |
49756 KB |
n=500 |
45 |
Correct |
10 ms |
49756 KB |
n=500 |
46 |
Correct |
10 ms |
49752 KB |
n=500 |
47 |
Correct |
10 ms |
49756 KB |
n=500 |
48 |
Correct |
10 ms |
49756 KB |
n=500 |
49 |
Correct |
10 ms |
49756 KB |
n=500 |
50 |
Correct |
10 ms |
49848 KB |
n=500 |
51 |
Correct |
10 ms |
49752 KB |
n=500 |
52 |
Correct |
10 ms |
49752 KB |
n=500 |
53 |
Correct |
10 ms |
49756 KB |
n=500 |
54 |
Correct |
10 ms |
49756 KB |
n=500 |
55 |
Correct |
10 ms |
49756 KB |
n=278 |
56 |
Correct |
10 ms |
49756 KB |
n=500 |
57 |
Correct |
10 ms |
49752 KB |
n=500 |
58 |
Correct |
9 ms |
49752 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
37468 KB |
n=5 |
2 |
Correct |
9 ms |
45660 KB |
n=100 |
3 |
Correct |
9 ms |
45464 KB |
n=100 |
4 |
Correct |
9 ms |
45660 KB |
n=100 |
5 |
Correct |
9 ms |
45660 KB |
n=100 |
6 |
Correct |
9 ms |
45656 KB |
n=100 |
7 |
Correct |
9 ms |
45660 KB |
n=100 |
8 |
Correct |
9 ms |
45660 KB |
n=100 |
9 |
Correct |
9 ms |
45704 KB |
n=100 |
10 |
Correct |
11 ms |
45660 KB |
n=100 |
11 |
Correct |
9 ms |
45660 KB |
n=100 |
12 |
Correct |
9 ms |
45656 KB |
n=100 |
13 |
Correct |
9 ms |
45656 KB |
n=100 |
14 |
Correct |
9 ms |
45660 KB |
n=100 |
15 |
Correct |
9 ms |
45656 KB |
n=100 |
16 |
Correct |
9 ms |
45656 KB |
n=100 |
17 |
Correct |
10 ms |
45660 KB |
n=100 |
18 |
Correct |
9 ms |
45712 KB |
n=100 |
19 |
Correct |
9 ms |
45660 KB |
n=100 |
20 |
Correct |
9 ms |
45660 KB |
n=100 |
21 |
Correct |
11 ms |
45656 KB |
n=100 |
22 |
Correct |
9 ms |
45656 KB |
n=100 |
23 |
Correct |
9 ms |
45660 KB |
n=100 |
24 |
Correct |
9 ms |
45656 KB |
n=100 |
25 |
Correct |
9 ms |
45660 KB |
n=100 |
26 |
Correct |
9 ms |
39516 KB |
n=12 |
27 |
Correct |
9 ms |
45660 KB |
n=100 |
28 |
Correct |
9 ms |
49756 KB |
n=500 |
29 |
Correct |
10 ms |
49756 KB |
n=500 |
30 |
Correct |
10 ms |
49892 KB |
n=500 |
31 |
Correct |
10 ms |
49756 KB |
n=500 |
32 |
Correct |
10 ms |
49756 KB |
n=500 |
33 |
Correct |
10 ms |
49756 KB |
n=500 |
34 |
Correct |
10 ms |
49752 KB |
n=500 |
35 |
Correct |
10 ms |
49904 KB |
n=500 |
36 |
Correct |
10 ms |
49756 KB |
n=500 |
37 |
Correct |
9 ms |
49752 KB |
n=500 |
38 |
Correct |
10 ms |
49880 KB |
n=500 |
39 |
Correct |
10 ms |
49756 KB |
n=500 |
40 |
Correct |
10 ms |
49756 KB |
n=500 |
41 |
Correct |
9 ms |
49712 KB |
n=500 |
42 |
Correct |
10 ms |
49756 KB |
n=500 |
43 |
Correct |
10 ms |
49756 KB |
n=500 |
44 |
Correct |
10 ms |
49756 KB |
n=500 |
45 |
Correct |
10 ms |
49756 KB |
n=500 |
46 |
Correct |
10 ms |
49752 KB |
n=500 |
47 |
Correct |
10 ms |
49756 KB |
n=500 |
48 |
Correct |
10 ms |
49756 KB |
n=500 |
49 |
Correct |
10 ms |
49756 KB |
n=500 |
50 |
Correct |
10 ms |
49848 KB |
n=500 |
51 |
Correct |
10 ms |
49752 KB |
n=500 |
52 |
Correct |
10 ms |
49752 KB |
n=500 |
53 |
Correct |
10 ms |
49756 KB |
n=500 |
54 |
Correct |
10 ms |
49756 KB |
n=500 |
55 |
Correct |
10 ms |
49756 KB |
n=278 |
56 |
Correct |
10 ms |
49756 KB |
n=500 |
57 |
Correct |
10 ms |
49752 KB |
n=500 |
58 |
Correct |
9 ms |
49752 KB |
n=500 |
59 |
Correct |
12 ms |
54036 KB |
n=2000 |
60 |
Correct |
12 ms |
54108 KB |
n=2000 |
61 |
Correct |
12 ms |
54104 KB |
n=2000 |
62 |
Correct |
13 ms |
54132 KB |
n=2000 |
63 |
Correct |
12 ms |
54020 KB |
n=2000 |
64 |
Correct |
12 ms |
54108 KB |
n=2000 |
65 |
Correct |
11 ms |
54108 KB |
n=2000 |
66 |
Correct |
12 ms |
54108 KB |
n=2000 |
67 |
Correct |
13 ms |
54108 KB |
n=2000 |
68 |
Correct |
11 ms |
54108 KB |
n=2000 |
69 |
Correct |
11 ms |
53992 KB |
n=2000 |
70 |
Correct |
11 ms |
54104 KB |
n=2000 |
71 |
Correct |
11 ms |
54104 KB |
n=2000 |
72 |
Correct |
11 ms |
54108 KB |
n=2000 |
73 |
Correct |
12 ms |
54108 KB |
n=2000 |
74 |
Correct |
12 ms |
54108 KB |
n=1844 |
75 |
Correct |
11 ms |
54008 KB |
n=2000 |
76 |
Correct |
12 ms |
54108 KB |
n=2000 |
77 |
Correct |
12 ms |
54104 KB |
n=2000 |
78 |
Correct |
12 ms |
54108 KB |
n=2000 |
79 |
Correct |
12 ms |
54104 KB |
n=2000 |
80 |
Correct |
12 ms |
54108 KB |
n=2000 |
81 |
Correct |
12 ms |
54104 KB |
n=2000 |
82 |
Correct |
12 ms |
54108 KB |
n=2000 |
83 |
Correct |
11 ms |
54104 KB |
n=2000 |
84 |
Correct |
13 ms |
54104 KB |
n=2000 |
85 |
Correct |
12 ms |
54104 KB |
n=2000 |
86 |
Correct |
12 ms |
54056 KB |
n=2000 |
87 |
Correct |
12 ms |
54108 KB |
n=2000 |
88 |
Correct |
12 ms |
54104 KB |
n=2000 |
89 |
Correct |
12 ms |
54108 KB |
n=2000 |
90 |
Correct |
11 ms |
54108 KB |
n=2000 |
91 |
Correct |
13 ms |
54108 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
37468 KB |
n=5 |
2 |
Correct |
9 ms |
45660 KB |
n=100 |
3 |
Correct |
9 ms |
45464 KB |
n=100 |
4 |
Correct |
9 ms |
45660 KB |
n=100 |
5 |
Correct |
9 ms |
45660 KB |
n=100 |
6 |
Correct |
9 ms |
45656 KB |
n=100 |
7 |
Correct |
9 ms |
45660 KB |
n=100 |
8 |
Correct |
9 ms |
45660 KB |
n=100 |
9 |
Correct |
9 ms |
45704 KB |
n=100 |
10 |
Correct |
11 ms |
45660 KB |
n=100 |
11 |
Correct |
9 ms |
45660 KB |
n=100 |
12 |
Correct |
9 ms |
45656 KB |
n=100 |
13 |
Correct |
9 ms |
45656 KB |
n=100 |
14 |
Correct |
9 ms |
45660 KB |
n=100 |
15 |
Correct |
9 ms |
45656 KB |
n=100 |
16 |
Correct |
9 ms |
45656 KB |
n=100 |
17 |
Correct |
10 ms |
45660 KB |
n=100 |
18 |
Correct |
9 ms |
45712 KB |
n=100 |
19 |
Correct |
9 ms |
45660 KB |
n=100 |
20 |
Correct |
9 ms |
45660 KB |
n=100 |
21 |
Correct |
11 ms |
45656 KB |
n=100 |
22 |
Correct |
9 ms |
45656 KB |
n=100 |
23 |
Correct |
9 ms |
45660 KB |
n=100 |
24 |
Correct |
9 ms |
45656 KB |
n=100 |
25 |
Correct |
9 ms |
45660 KB |
n=100 |
26 |
Correct |
9 ms |
39516 KB |
n=12 |
27 |
Correct |
9 ms |
45660 KB |
n=100 |
28 |
Correct |
9 ms |
49756 KB |
n=500 |
29 |
Correct |
10 ms |
49756 KB |
n=500 |
30 |
Correct |
10 ms |
49892 KB |
n=500 |
31 |
Correct |
10 ms |
49756 KB |
n=500 |
32 |
Correct |
10 ms |
49756 KB |
n=500 |
33 |
Correct |
10 ms |
49756 KB |
n=500 |
34 |
Correct |
10 ms |
49752 KB |
n=500 |
35 |
Correct |
10 ms |
49904 KB |
n=500 |
36 |
Correct |
10 ms |
49756 KB |
n=500 |
37 |
Correct |
9 ms |
49752 KB |
n=500 |
38 |
Correct |
10 ms |
49880 KB |
n=500 |
39 |
Correct |
10 ms |
49756 KB |
n=500 |
40 |
Correct |
10 ms |
49756 KB |
n=500 |
41 |
Correct |
9 ms |
49712 KB |
n=500 |
42 |
Correct |
10 ms |
49756 KB |
n=500 |
43 |
Correct |
10 ms |
49756 KB |
n=500 |
44 |
Correct |
10 ms |
49756 KB |
n=500 |
45 |
Correct |
10 ms |
49756 KB |
n=500 |
46 |
Correct |
10 ms |
49752 KB |
n=500 |
47 |
Correct |
10 ms |
49756 KB |
n=500 |
48 |
Correct |
10 ms |
49756 KB |
n=500 |
49 |
Correct |
10 ms |
49756 KB |
n=500 |
50 |
Correct |
10 ms |
49848 KB |
n=500 |
51 |
Correct |
10 ms |
49752 KB |
n=500 |
52 |
Correct |
10 ms |
49752 KB |
n=500 |
53 |
Correct |
10 ms |
49756 KB |
n=500 |
54 |
Correct |
10 ms |
49756 KB |
n=500 |
55 |
Correct |
10 ms |
49756 KB |
n=278 |
56 |
Correct |
10 ms |
49756 KB |
n=500 |
57 |
Correct |
10 ms |
49752 KB |
n=500 |
58 |
Correct |
9 ms |
49752 KB |
n=500 |
59 |
Correct |
12 ms |
54036 KB |
n=2000 |
60 |
Correct |
12 ms |
54108 KB |
n=2000 |
61 |
Correct |
12 ms |
54104 KB |
n=2000 |
62 |
Correct |
13 ms |
54132 KB |
n=2000 |
63 |
Correct |
12 ms |
54020 KB |
n=2000 |
64 |
Correct |
12 ms |
54108 KB |
n=2000 |
65 |
Correct |
11 ms |
54108 KB |
n=2000 |
66 |
Correct |
12 ms |
54108 KB |
n=2000 |
67 |
Correct |
13 ms |
54108 KB |
n=2000 |
68 |
Correct |
11 ms |
54108 KB |
n=2000 |
69 |
Correct |
11 ms |
53992 KB |
n=2000 |
70 |
Correct |
11 ms |
54104 KB |
n=2000 |
71 |
Correct |
11 ms |
54104 KB |
n=2000 |
72 |
Correct |
11 ms |
54108 KB |
n=2000 |
73 |
Correct |
12 ms |
54108 KB |
n=2000 |
74 |
Correct |
12 ms |
54108 KB |
n=1844 |
75 |
Correct |
11 ms |
54008 KB |
n=2000 |
76 |
Correct |
12 ms |
54108 KB |
n=2000 |
77 |
Correct |
12 ms |
54104 KB |
n=2000 |
78 |
Correct |
12 ms |
54108 KB |
n=2000 |
79 |
Correct |
12 ms |
54104 KB |
n=2000 |
80 |
Correct |
12 ms |
54108 KB |
n=2000 |
81 |
Correct |
12 ms |
54104 KB |
n=2000 |
82 |
Correct |
12 ms |
54108 KB |
n=2000 |
83 |
Correct |
11 ms |
54104 KB |
n=2000 |
84 |
Correct |
13 ms |
54104 KB |
n=2000 |
85 |
Correct |
12 ms |
54104 KB |
n=2000 |
86 |
Correct |
12 ms |
54056 KB |
n=2000 |
87 |
Correct |
12 ms |
54108 KB |
n=2000 |
88 |
Correct |
12 ms |
54104 KB |
n=2000 |
89 |
Correct |
12 ms |
54108 KB |
n=2000 |
90 |
Correct |
11 ms |
54108 KB |
n=2000 |
91 |
Correct |
13 ms |
54108 KB |
n=2000 |
92 |
Correct |
560 ms |
122452 KB |
n=200000 |
93 |
Correct |
495 ms |
128656 KB |
n=200000 |
94 |
Correct |
481 ms |
133444 KB |
n=200000 |
95 |
Correct |
554 ms |
122064 KB |
n=200000 |
96 |
Correct |
533 ms |
122500 KB |
n=200000 |
97 |
Correct |
553 ms |
127572 KB |
n=200000 |
98 |
Correct |
514 ms |
122144 KB |
n=200000 |
99 |
Correct |
556 ms |
122196 KB |
n=200000 |
100 |
Correct |
518 ms |
122076 KB |
n=200000 |
101 |
Correct |
449 ms |
134732 KB |
n=200000 |
102 |
Correct |
263 ms |
123296 KB |
n=200000 |
103 |
Correct |
298 ms |
123472 KB |
n=200000 |
104 |
Correct |
278 ms |
123416 KB |
n=200000 |
105 |
Correct |
285 ms |
123556 KB |
n=200000 |
106 |
Correct |
280 ms |
123476 KB |
n=200000 |
107 |
Correct |
293 ms |
123496 KB |
n=200000 |
108 |
Correct |
532 ms |
122164 KB |
n=200000 |
109 |
Correct |
517 ms |
122168 KB |
n=200000 |
110 |
Correct |
508 ms |
122124 KB |
n=200000 |
111 |
Correct |
548 ms |
121416 KB |
n=200000 |
112 |
Correct |
448 ms |
133360 KB |
n=200000 |
113 |
Correct |
497 ms |
127504 KB |
n=200000 |
114 |
Correct |
535 ms |
121556 KB |
n=200000 |
115 |
Correct |
552 ms |
124500 KB |
n=200000 |
116 |
Correct |
506 ms |
122272 KB |
n=200000 |
117 |
Correct |
436 ms |
133908 KB |
n=200000 |
118 |
Correct |
525 ms |
126032 KB |
n=200000 |
119 |
Correct |
507 ms |
122052 KB |
n=200000 |
120 |
Correct |
393 ms |
133712 KB |
n=200000 |
121 |
Correct |
395 ms |
133656 KB |
n=200000 |
122 |
Correct |
402 ms |
134140 KB |
n=200000 |
123 |
Correct |
305 ms |
123392 KB |
n=200000 |
124 |
Correct |
114 ms |
80976 KB |
n=25264 |