#include <bits/stdc++.h>
using namespace std;
const int kN = 2e5;
const int kLog = 17;
int n, m, q, timer, a[1 + kN], tin[1 + kN], tout[1 + kN], depth[1 + kN], lg2[1 + kN], anc[1 + kN][1 + kLog];
vector<int> g[1 + kN];
set<int> one[1 + kN], two[1 + kN];
void precalc() {
for (int i = 2; i <= kN; ++i) {
lg2[i] = lg2[i / 2] + 1;
}
}
void dfs(int u) {
tin[u] = ++timer;
for (int i = 1; i <= kLog; ++i) {
anc[u][i] = anc[anc[u][i - 1]][i - 1];
if (anc[u][i] == 0) {
break;
}
}
for (int v : g[u]) {
if (v != anc[u][0]) {
anc[v][0] = u;
depth[v] = depth[u] + 1;
dfs(v);
}
}
tout[u] = timer;
}
bool isAncestor(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int getLca(int u, int v) {
if (depth[u] < depth[v]) {
swap(u, v);
}
if (isAncestor(v, u)) {
return v;
}
for (int i = lg2[depth[v]]; i >= 0; --i) {
if (anc[v][i] && !isAncestor(anc[v][i], u)) {
v = anc[v][i];
}
}
return anc[v][0];
}
void testCase() {
cin >> n >> m >> q;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1);
for (int i = 1; i <= m; ++i) {
cin >> a[i];
one[a[i]].emplace(i);
if (i > 1) {
two[getLca(a[i - 1], a[i])].emplace(i - 1);
}
}
for (int i = 0; i < q; ++i) {
char op;
cin >> op;
if (op == '1') {
int pos, v;
cin >> pos >> v;
one[a[pos]].erase(pos);
if (pos > 1) {
two[getLca(a[pos - 1], a[pos])].erase(pos - 1);
}
if (pos < n) {
two[getLca(a[pos], a[pos + 1])].erase(pos);
}
a[pos] = v;
one[a[pos]].emplace(pos);
if (pos > 1) {
two[getLca(a[pos - 1], a[pos])].emplace(pos - 1);
}
if (pos < n) {
two[getLca(a[pos], a[pos + 1])].emplace(pos);
}
} else {
int l, r, v;
cin >> l >> r >> v;
auto it = one[v].lower_bound(l);
if (it != one[v].end() && *it <= r) {
cout << *it << ' ' << *it << '\n';
continue;
}
it = two[v].lower_bound(l);
if (it != two[v].end() && *it + 1 <= r) {
cout << *it << ' ' << *it + 1 << '\n';
} else {
cout << "-1 -1\n";
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
precalc();
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
24592 KB |
n=5 |
2 |
Correct |
12 ms |
24596 KB |
n=100 |
3 |
Correct |
12 ms |
24584 KB |
n=100 |
4 |
Correct |
12 ms |
24580 KB |
n=100 |
5 |
Correct |
12 ms |
24512 KB |
n=100 |
6 |
Correct |
11 ms |
24524 KB |
n=100 |
7 |
Correct |
11 ms |
24612 KB |
n=100 |
8 |
Correct |
12 ms |
24580 KB |
n=100 |
9 |
Correct |
12 ms |
24588 KB |
n=100 |
10 |
Correct |
12 ms |
24612 KB |
n=100 |
11 |
Correct |
13 ms |
24524 KB |
n=100 |
12 |
Correct |
12 ms |
24532 KB |
n=100 |
13 |
Correct |
12 ms |
24532 KB |
n=100 |
14 |
Correct |
12 ms |
24620 KB |
n=100 |
15 |
Correct |
17 ms |
24572 KB |
n=100 |
16 |
Correct |
12 ms |
24592 KB |
n=100 |
17 |
Correct |
12 ms |
24584 KB |
n=100 |
18 |
Correct |
12 ms |
24604 KB |
n=100 |
19 |
Correct |
12 ms |
24588 KB |
n=100 |
20 |
Correct |
12 ms |
24524 KB |
n=100 |
21 |
Correct |
15 ms |
24524 KB |
n=100 |
22 |
Correct |
13 ms |
24596 KB |
n=100 |
23 |
Correct |
12 ms |
24552 KB |
n=100 |
24 |
Correct |
14 ms |
24524 KB |
n=100 |
25 |
Correct |
14 ms |
24524 KB |
n=100 |
26 |
Correct |
15 ms |
24524 KB |
n=12 |
27 |
Correct |
12 ms |
24524 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
24592 KB |
n=5 |
2 |
Correct |
12 ms |
24596 KB |
n=100 |
3 |
Correct |
12 ms |
24584 KB |
n=100 |
4 |
Correct |
12 ms |
24580 KB |
n=100 |
5 |
Correct |
12 ms |
24512 KB |
n=100 |
6 |
Correct |
11 ms |
24524 KB |
n=100 |
7 |
Correct |
11 ms |
24612 KB |
n=100 |
8 |
Correct |
12 ms |
24580 KB |
n=100 |
9 |
Correct |
12 ms |
24588 KB |
n=100 |
10 |
Correct |
12 ms |
24612 KB |
n=100 |
11 |
Correct |
13 ms |
24524 KB |
n=100 |
12 |
Correct |
12 ms |
24532 KB |
n=100 |
13 |
Correct |
12 ms |
24532 KB |
n=100 |
14 |
Correct |
12 ms |
24620 KB |
n=100 |
15 |
Correct |
17 ms |
24572 KB |
n=100 |
16 |
Correct |
12 ms |
24592 KB |
n=100 |
17 |
Correct |
12 ms |
24584 KB |
n=100 |
18 |
Correct |
12 ms |
24604 KB |
n=100 |
19 |
Correct |
12 ms |
24588 KB |
n=100 |
20 |
Correct |
12 ms |
24524 KB |
n=100 |
21 |
Correct |
15 ms |
24524 KB |
n=100 |
22 |
Correct |
13 ms |
24596 KB |
n=100 |
23 |
Correct |
12 ms |
24552 KB |
n=100 |
24 |
Correct |
14 ms |
24524 KB |
n=100 |
25 |
Correct |
14 ms |
24524 KB |
n=100 |
26 |
Correct |
15 ms |
24524 KB |
n=12 |
27 |
Correct |
12 ms |
24524 KB |
n=100 |
28 |
Correct |
13 ms |
24672 KB |
n=500 |
29 |
Correct |
15 ms |
24672 KB |
n=500 |
30 |
Correct |
14 ms |
24728 KB |
n=500 |
31 |
Correct |
13 ms |
24644 KB |
n=500 |
32 |
Correct |
13 ms |
24652 KB |
n=500 |
33 |
Correct |
12 ms |
24652 KB |
n=500 |
34 |
Correct |
13 ms |
24728 KB |
n=500 |
35 |
Correct |
12 ms |
24732 KB |
n=500 |
36 |
Correct |
12 ms |
24728 KB |
n=500 |
37 |
Correct |
14 ms |
24724 KB |
n=500 |
38 |
Correct |
15 ms |
24628 KB |
n=500 |
39 |
Correct |
13 ms |
24716 KB |
n=500 |
40 |
Correct |
13 ms |
24652 KB |
n=500 |
41 |
Correct |
13 ms |
24652 KB |
n=500 |
42 |
Correct |
13 ms |
24652 KB |
n=500 |
43 |
Correct |
13 ms |
24652 KB |
n=500 |
44 |
Correct |
13 ms |
24652 KB |
n=500 |
45 |
Correct |
13 ms |
24728 KB |
n=500 |
46 |
Correct |
13 ms |
24736 KB |
n=500 |
47 |
Correct |
14 ms |
24780 KB |
n=500 |
48 |
Correct |
13 ms |
24648 KB |
n=500 |
49 |
Correct |
15 ms |
24652 KB |
n=500 |
50 |
Correct |
15 ms |
24732 KB |
n=500 |
51 |
Correct |
14 ms |
24716 KB |
n=500 |
52 |
Correct |
13 ms |
24724 KB |
n=500 |
53 |
Correct |
12 ms |
24648 KB |
n=500 |
54 |
Correct |
14 ms |
24692 KB |
n=500 |
55 |
Correct |
15 ms |
24652 KB |
n=278 |
56 |
Correct |
13 ms |
24652 KB |
n=500 |
57 |
Correct |
13 ms |
24652 KB |
n=500 |
58 |
Correct |
13 ms |
24732 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
24592 KB |
n=5 |
2 |
Correct |
12 ms |
24596 KB |
n=100 |
3 |
Correct |
12 ms |
24584 KB |
n=100 |
4 |
Correct |
12 ms |
24580 KB |
n=100 |
5 |
Correct |
12 ms |
24512 KB |
n=100 |
6 |
Correct |
11 ms |
24524 KB |
n=100 |
7 |
Correct |
11 ms |
24612 KB |
n=100 |
8 |
Correct |
12 ms |
24580 KB |
n=100 |
9 |
Correct |
12 ms |
24588 KB |
n=100 |
10 |
Correct |
12 ms |
24612 KB |
n=100 |
11 |
Correct |
13 ms |
24524 KB |
n=100 |
12 |
Correct |
12 ms |
24532 KB |
n=100 |
13 |
Correct |
12 ms |
24532 KB |
n=100 |
14 |
Correct |
12 ms |
24620 KB |
n=100 |
15 |
Correct |
17 ms |
24572 KB |
n=100 |
16 |
Correct |
12 ms |
24592 KB |
n=100 |
17 |
Correct |
12 ms |
24584 KB |
n=100 |
18 |
Correct |
12 ms |
24604 KB |
n=100 |
19 |
Correct |
12 ms |
24588 KB |
n=100 |
20 |
Correct |
12 ms |
24524 KB |
n=100 |
21 |
Correct |
15 ms |
24524 KB |
n=100 |
22 |
Correct |
13 ms |
24596 KB |
n=100 |
23 |
Correct |
12 ms |
24552 KB |
n=100 |
24 |
Correct |
14 ms |
24524 KB |
n=100 |
25 |
Correct |
14 ms |
24524 KB |
n=100 |
26 |
Correct |
15 ms |
24524 KB |
n=12 |
27 |
Correct |
12 ms |
24524 KB |
n=100 |
28 |
Correct |
13 ms |
24672 KB |
n=500 |
29 |
Correct |
15 ms |
24672 KB |
n=500 |
30 |
Correct |
14 ms |
24728 KB |
n=500 |
31 |
Correct |
13 ms |
24644 KB |
n=500 |
32 |
Correct |
13 ms |
24652 KB |
n=500 |
33 |
Correct |
12 ms |
24652 KB |
n=500 |
34 |
Correct |
13 ms |
24728 KB |
n=500 |
35 |
Correct |
12 ms |
24732 KB |
n=500 |
36 |
Correct |
12 ms |
24728 KB |
n=500 |
37 |
Correct |
14 ms |
24724 KB |
n=500 |
38 |
Correct |
15 ms |
24628 KB |
n=500 |
39 |
Correct |
13 ms |
24716 KB |
n=500 |
40 |
Correct |
13 ms |
24652 KB |
n=500 |
41 |
Correct |
13 ms |
24652 KB |
n=500 |
42 |
Correct |
13 ms |
24652 KB |
n=500 |
43 |
Correct |
13 ms |
24652 KB |
n=500 |
44 |
Correct |
13 ms |
24652 KB |
n=500 |
45 |
Correct |
13 ms |
24728 KB |
n=500 |
46 |
Correct |
13 ms |
24736 KB |
n=500 |
47 |
Correct |
14 ms |
24780 KB |
n=500 |
48 |
Correct |
13 ms |
24648 KB |
n=500 |
49 |
Correct |
15 ms |
24652 KB |
n=500 |
50 |
Correct |
15 ms |
24732 KB |
n=500 |
51 |
Correct |
14 ms |
24716 KB |
n=500 |
52 |
Correct |
13 ms |
24724 KB |
n=500 |
53 |
Correct |
12 ms |
24648 KB |
n=500 |
54 |
Correct |
14 ms |
24692 KB |
n=500 |
55 |
Correct |
15 ms |
24652 KB |
n=278 |
56 |
Correct |
13 ms |
24652 KB |
n=500 |
57 |
Correct |
13 ms |
24652 KB |
n=500 |
58 |
Correct |
13 ms |
24732 KB |
n=500 |
59 |
Correct |
15 ms |
25048 KB |
n=2000 |
60 |
Correct |
15 ms |
25116 KB |
n=2000 |
61 |
Correct |
15 ms |
25164 KB |
n=2000 |
62 |
Correct |
15 ms |
25044 KB |
n=2000 |
63 |
Correct |
17 ms |
25060 KB |
n=2000 |
64 |
Correct |
15 ms |
25036 KB |
n=2000 |
65 |
Correct |
14 ms |
25036 KB |
n=2000 |
66 |
Correct |
14 ms |
25116 KB |
n=2000 |
67 |
Correct |
15 ms |
24988 KB |
n=2000 |
68 |
Correct |
15 ms |
25244 KB |
n=2000 |
69 |
Correct |
14 ms |
24980 KB |
n=2000 |
70 |
Correct |
16 ms |
25036 KB |
n=2000 |
71 |
Correct |
17 ms |
25112 KB |
n=2000 |
72 |
Correct |
14 ms |
25036 KB |
n=2000 |
73 |
Correct |
14 ms |
25036 KB |
n=2000 |
74 |
Correct |
14 ms |
25044 KB |
n=1844 |
75 |
Correct |
14 ms |
25036 KB |
n=2000 |
76 |
Correct |
15 ms |
24976 KB |
n=2000 |
77 |
Correct |
15 ms |
24992 KB |
n=2000 |
78 |
Correct |
15 ms |
24992 KB |
n=2000 |
79 |
Correct |
17 ms |
24984 KB |
n=2000 |
80 |
Correct |
18 ms |
25164 KB |
n=2000 |
81 |
Correct |
15 ms |
25120 KB |
n=2000 |
82 |
Correct |
17 ms |
25052 KB |
n=2000 |
83 |
Correct |
16 ms |
25112 KB |
n=2000 |
84 |
Correct |
14 ms |
25036 KB |
n=2000 |
85 |
Correct |
15 ms |
25068 KB |
n=2000 |
86 |
Correct |
14 ms |
25116 KB |
n=2000 |
87 |
Correct |
14 ms |
25036 KB |
n=2000 |
88 |
Correct |
15 ms |
25112 KB |
n=2000 |
89 |
Correct |
14 ms |
25164 KB |
n=2000 |
90 |
Correct |
14 ms |
25128 KB |
n=2000 |
91 |
Correct |
14 ms |
25036 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
24592 KB |
n=5 |
2 |
Correct |
12 ms |
24596 KB |
n=100 |
3 |
Correct |
12 ms |
24584 KB |
n=100 |
4 |
Correct |
12 ms |
24580 KB |
n=100 |
5 |
Correct |
12 ms |
24512 KB |
n=100 |
6 |
Correct |
11 ms |
24524 KB |
n=100 |
7 |
Correct |
11 ms |
24612 KB |
n=100 |
8 |
Correct |
12 ms |
24580 KB |
n=100 |
9 |
Correct |
12 ms |
24588 KB |
n=100 |
10 |
Correct |
12 ms |
24612 KB |
n=100 |
11 |
Correct |
13 ms |
24524 KB |
n=100 |
12 |
Correct |
12 ms |
24532 KB |
n=100 |
13 |
Correct |
12 ms |
24532 KB |
n=100 |
14 |
Correct |
12 ms |
24620 KB |
n=100 |
15 |
Correct |
17 ms |
24572 KB |
n=100 |
16 |
Correct |
12 ms |
24592 KB |
n=100 |
17 |
Correct |
12 ms |
24584 KB |
n=100 |
18 |
Correct |
12 ms |
24604 KB |
n=100 |
19 |
Correct |
12 ms |
24588 KB |
n=100 |
20 |
Correct |
12 ms |
24524 KB |
n=100 |
21 |
Correct |
15 ms |
24524 KB |
n=100 |
22 |
Correct |
13 ms |
24596 KB |
n=100 |
23 |
Correct |
12 ms |
24552 KB |
n=100 |
24 |
Correct |
14 ms |
24524 KB |
n=100 |
25 |
Correct |
14 ms |
24524 KB |
n=100 |
26 |
Correct |
15 ms |
24524 KB |
n=12 |
27 |
Correct |
12 ms |
24524 KB |
n=100 |
28 |
Correct |
13 ms |
24672 KB |
n=500 |
29 |
Correct |
15 ms |
24672 KB |
n=500 |
30 |
Correct |
14 ms |
24728 KB |
n=500 |
31 |
Correct |
13 ms |
24644 KB |
n=500 |
32 |
Correct |
13 ms |
24652 KB |
n=500 |
33 |
Correct |
12 ms |
24652 KB |
n=500 |
34 |
Correct |
13 ms |
24728 KB |
n=500 |
35 |
Correct |
12 ms |
24732 KB |
n=500 |
36 |
Correct |
12 ms |
24728 KB |
n=500 |
37 |
Correct |
14 ms |
24724 KB |
n=500 |
38 |
Correct |
15 ms |
24628 KB |
n=500 |
39 |
Correct |
13 ms |
24716 KB |
n=500 |
40 |
Correct |
13 ms |
24652 KB |
n=500 |
41 |
Correct |
13 ms |
24652 KB |
n=500 |
42 |
Correct |
13 ms |
24652 KB |
n=500 |
43 |
Correct |
13 ms |
24652 KB |
n=500 |
44 |
Correct |
13 ms |
24652 KB |
n=500 |
45 |
Correct |
13 ms |
24728 KB |
n=500 |
46 |
Correct |
13 ms |
24736 KB |
n=500 |
47 |
Correct |
14 ms |
24780 KB |
n=500 |
48 |
Correct |
13 ms |
24648 KB |
n=500 |
49 |
Correct |
15 ms |
24652 KB |
n=500 |
50 |
Correct |
15 ms |
24732 KB |
n=500 |
51 |
Correct |
14 ms |
24716 KB |
n=500 |
52 |
Correct |
13 ms |
24724 KB |
n=500 |
53 |
Correct |
12 ms |
24648 KB |
n=500 |
54 |
Correct |
14 ms |
24692 KB |
n=500 |
55 |
Correct |
15 ms |
24652 KB |
n=278 |
56 |
Correct |
13 ms |
24652 KB |
n=500 |
57 |
Correct |
13 ms |
24652 KB |
n=500 |
58 |
Correct |
13 ms |
24732 KB |
n=500 |
59 |
Correct |
15 ms |
25048 KB |
n=2000 |
60 |
Correct |
15 ms |
25116 KB |
n=2000 |
61 |
Correct |
15 ms |
25164 KB |
n=2000 |
62 |
Correct |
15 ms |
25044 KB |
n=2000 |
63 |
Correct |
17 ms |
25060 KB |
n=2000 |
64 |
Correct |
15 ms |
25036 KB |
n=2000 |
65 |
Correct |
14 ms |
25036 KB |
n=2000 |
66 |
Correct |
14 ms |
25116 KB |
n=2000 |
67 |
Correct |
15 ms |
24988 KB |
n=2000 |
68 |
Correct |
15 ms |
25244 KB |
n=2000 |
69 |
Correct |
14 ms |
24980 KB |
n=2000 |
70 |
Correct |
16 ms |
25036 KB |
n=2000 |
71 |
Correct |
17 ms |
25112 KB |
n=2000 |
72 |
Correct |
14 ms |
25036 KB |
n=2000 |
73 |
Correct |
14 ms |
25036 KB |
n=2000 |
74 |
Correct |
14 ms |
25044 KB |
n=1844 |
75 |
Correct |
14 ms |
25036 KB |
n=2000 |
76 |
Correct |
15 ms |
24976 KB |
n=2000 |
77 |
Correct |
15 ms |
24992 KB |
n=2000 |
78 |
Correct |
15 ms |
24992 KB |
n=2000 |
79 |
Correct |
17 ms |
24984 KB |
n=2000 |
80 |
Correct |
18 ms |
25164 KB |
n=2000 |
81 |
Correct |
15 ms |
25120 KB |
n=2000 |
82 |
Correct |
17 ms |
25052 KB |
n=2000 |
83 |
Correct |
16 ms |
25112 KB |
n=2000 |
84 |
Correct |
14 ms |
25036 KB |
n=2000 |
85 |
Correct |
15 ms |
25068 KB |
n=2000 |
86 |
Correct |
14 ms |
25116 KB |
n=2000 |
87 |
Correct |
14 ms |
25036 KB |
n=2000 |
88 |
Correct |
15 ms |
25112 KB |
n=2000 |
89 |
Correct |
14 ms |
25164 KB |
n=2000 |
90 |
Correct |
14 ms |
25128 KB |
n=2000 |
91 |
Correct |
14 ms |
25036 KB |
n=2000 |
92 |
Correct |
587 ms |
75444 KB |
n=200000 |
93 |
Correct |
695 ms |
81724 KB |
n=200000 |
94 |
Correct |
523 ms |
86184 KB |
n=200000 |
95 |
Correct |
593 ms |
75088 KB |
n=200000 |
96 |
Correct |
521 ms |
75060 KB |
n=200000 |
97 |
Correct |
698 ms |
80612 KB |
n=200000 |
98 |
Correct |
582 ms |
75292 KB |
n=200000 |
99 |
Correct |
670 ms |
75568 KB |
n=200000 |
100 |
Correct |
576 ms |
75304 KB |
n=200000 |
101 |
Correct |
514 ms |
87784 KB |
n=200000 |
102 |
Correct |
376 ms |
76312 KB |
n=200000 |
103 |
Correct |
352 ms |
76280 KB |
n=200000 |
104 |
Correct |
330 ms |
76308 KB |
n=200000 |
105 |
Correct |
339 ms |
76656 KB |
n=200000 |
106 |
Correct |
388 ms |
76780 KB |
n=200000 |
107 |
Correct |
382 ms |
76744 KB |
n=200000 |
108 |
Correct |
604 ms |
75144 KB |
n=200000 |
109 |
Correct |
627 ms |
75140 KB |
n=200000 |
110 |
Correct |
638 ms |
75224 KB |
n=200000 |
111 |
Correct |
596 ms |
74644 KB |
n=200000 |
112 |
Correct |
494 ms |
86508 KB |
n=200000 |
113 |
Correct |
691 ms |
80516 KB |
n=200000 |
114 |
Correct |
564 ms |
74804 KB |
n=200000 |
115 |
Correct |
820 ms |
77764 KB |
n=200000 |
116 |
Correct |
536 ms |
75196 KB |
n=200000 |
117 |
Correct |
505 ms |
87000 KB |
n=200000 |
118 |
Correct |
773 ms |
78980 KB |
n=200000 |
119 |
Correct |
523 ms |
75204 KB |
n=200000 |
120 |
Correct |
530 ms |
86836 KB |
n=200000 |
121 |
Correct |
429 ms |
86864 KB |
n=200000 |
122 |
Correct |
431 ms |
87224 KB |
n=200000 |
123 |
Correct |
340 ms |
76520 KB |
n=200000 |
124 |
Incorrect |
138 ms |
39924 KB |
Wrong range. |
125 |
Halted |
0 ms |
0 KB |
- |