// I stand with PALESTINE
//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int lg = 18;
vector<vector<int>> g, jump;
vector<int> tin, tout;
int z;
void dfs(int u) {
if (u != 1) {
for (int i = 1; i < lg; i++) {
jump[i][u] = jump[i - 1][jump[i - 1][u]];
if (!jump[i][u]) break;
}
}
tin[u] = z++;
for (int v: g[u]) {
if (jump[0][u] == v) continue;
jump[0][v] = u;
dfs(v);
}
tout[u] = z++;
}
bool isPar(int u, int v) {
return tin[u] <= tin[v] and tout[v] <= tout[u];
}
int lca(int u, int v) {
if (v == 0 or isPar(u, v)) return u;
if (u == 0 or isPar(v, u)) return v;
for (int i = lg - 1; i >= 0; i--) {
if (!jump[i][u] or isPar(jump[i][u], v)) continue;
u = jump[i][u];
}
return jump[0][u];
}
void solve() {
int n, m, q;
cin >> n >> m >> q;
g.assign(n + 1, {});
jump.assign(lg, vector<int>(n + 1));
tin.assign(n + 1, 0); tout.assign(n + 1, 0);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
vector<int> a(m + 1);
for (int i = 1; i <= m; i++) cin >> a[i];
dfs(1);
while (q--) {
int op;
cin >> op;
if (op == 1) {
int i;
cin >> i >> a[i];
} else {
int l, r, root;
cin >> l >> r >> root;
bool found = false;
for (int i = l; i <= r and !found; i++) {
if (a[i] == root) {
cout << i << ' ' << i << '\n';
found = true;
}
}
if (found) continue;
for (int i = l; i < r and !found; i++) {
if (lca(a[i], a[i + 1]) == root) {
cout << i << ' ' << i + 1 << '\n';
found = true;
}
}
if (!found) cout << "-1 -1\n";
}
}
}
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int q = 1;
// cin >> q;
while (q--) {
solve();
cout << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
348 KB |
n=100 |
3 |
Correct |
0 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
0 ms |
348 KB |
n=100 |
6 |
Correct |
1 ms |
348 KB |
n=100 |
7 |
Correct |
0 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
348 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
0 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
0 ms |
344 KB |
n=100 |
13 |
Correct |
0 ms |
348 KB |
n=100 |
14 |
Correct |
0 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
0 ms |
348 KB |
n=100 |
17 |
Correct |
0 ms |
348 KB |
n=100 |
18 |
Correct |
0 ms |
348 KB |
n=100 |
19 |
Correct |
0 ms |
348 KB |
n=100 |
20 |
Correct |
0 ms |
348 KB |
n=100 |
21 |
Correct |
0 ms |
344 KB |
n=100 |
22 |
Correct |
0 ms |
348 KB |
n=100 |
23 |
Correct |
0 ms |
348 KB |
n=100 |
24 |
Correct |
0 ms |
348 KB |
n=100 |
25 |
Correct |
0 ms |
348 KB |
n=100 |
26 |
Correct |
0 ms |
344 KB |
n=12 |
27 |
Correct |
1 ms |
348 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
348 KB |
n=100 |
3 |
Correct |
0 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
0 ms |
348 KB |
n=100 |
6 |
Correct |
1 ms |
348 KB |
n=100 |
7 |
Correct |
0 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
348 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
0 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
0 ms |
344 KB |
n=100 |
13 |
Correct |
0 ms |
348 KB |
n=100 |
14 |
Correct |
0 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
0 ms |
348 KB |
n=100 |
17 |
Correct |
0 ms |
348 KB |
n=100 |
18 |
Correct |
0 ms |
348 KB |
n=100 |
19 |
Correct |
0 ms |
348 KB |
n=100 |
20 |
Correct |
0 ms |
348 KB |
n=100 |
21 |
Correct |
0 ms |
344 KB |
n=100 |
22 |
Correct |
0 ms |
348 KB |
n=100 |
23 |
Correct |
0 ms |
348 KB |
n=100 |
24 |
Correct |
0 ms |
348 KB |
n=100 |
25 |
Correct |
0 ms |
348 KB |
n=100 |
26 |
Correct |
0 ms |
344 KB |
n=12 |
27 |
Correct |
1 ms |
348 KB |
n=100 |
28 |
Correct |
1 ms |
348 KB |
n=500 |
29 |
Correct |
1 ms |
344 KB |
n=500 |
30 |
Correct |
1 ms |
344 KB |
n=500 |
31 |
Correct |
1 ms |
348 KB |
n=500 |
32 |
Correct |
1 ms |
348 KB |
n=500 |
33 |
Correct |
1 ms |
348 KB |
n=500 |
34 |
Correct |
0 ms |
348 KB |
n=500 |
35 |
Correct |
1 ms |
348 KB |
n=500 |
36 |
Correct |
4 ms |
348 KB |
n=500 |
37 |
Correct |
3 ms |
348 KB |
n=500 |
38 |
Correct |
4 ms |
344 KB |
n=500 |
39 |
Correct |
2 ms |
348 KB |
n=500 |
40 |
Correct |
3 ms |
348 KB |
n=500 |
41 |
Correct |
2 ms |
348 KB |
n=500 |
42 |
Correct |
2 ms |
348 KB |
n=500 |
43 |
Correct |
2 ms |
520 KB |
n=500 |
44 |
Correct |
2 ms |
348 KB |
n=500 |
45 |
Correct |
1 ms |
348 KB |
n=500 |
46 |
Correct |
1 ms |
348 KB |
n=500 |
47 |
Correct |
1 ms |
348 KB |
n=500 |
48 |
Correct |
0 ms |
344 KB |
n=500 |
49 |
Correct |
1 ms |
348 KB |
n=500 |
50 |
Correct |
1 ms |
348 KB |
n=500 |
51 |
Correct |
1 ms |
348 KB |
n=500 |
52 |
Correct |
1 ms |
348 KB |
n=500 |
53 |
Correct |
1 ms |
348 KB |
n=500 |
54 |
Correct |
1 ms |
344 KB |
n=500 |
55 |
Correct |
1 ms |
344 KB |
n=278 |
56 |
Correct |
1 ms |
356 KB |
n=500 |
57 |
Correct |
1 ms |
344 KB |
n=500 |
58 |
Correct |
3 ms |
348 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
348 KB |
n=100 |
3 |
Correct |
0 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
0 ms |
348 KB |
n=100 |
6 |
Correct |
1 ms |
348 KB |
n=100 |
7 |
Correct |
0 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
348 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
0 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
0 ms |
344 KB |
n=100 |
13 |
Correct |
0 ms |
348 KB |
n=100 |
14 |
Correct |
0 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
0 ms |
348 KB |
n=100 |
17 |
Correct |
0 ms |
348 KB |
n=100 |
18 |
Correct |
0 ms |
348 KB |
n=100 |
19 |
Correct |
0 ms |
348 KB |
n=100 |
20 |
Correct |
0 ms |
348 KB |
n=100 |
21 |
Correct |
0 ms |
344 KB |
n=100 |
22 |
Correct |
0 ms |
348 KB |
n=100 |
23 |
Correct |
0 ms |
348 KB |
n=100 |
24 |
Correct |
0 ms |
348 KB |
n=100 |
25 |
Correct |
0 ms |
348 KB |
n=100 |
26 |
Correct |
0 ms |
344 KB |
n=12 |
27 |
Correct |
1 ms |
348 KB |
n=100 |
28 |
Correct |
1 ms |
348 KB |
n=500 |
29 |
Correct |
1 ms |
344 KB |
n=500 |
30 |
Correct |
1 ms |
344 KB |
n=500 |
31 |
Correct |
1 ms |
348 KB |
n=500 |
32 |
Correct |
1 ms |
348 KB |
n=500 |
33 |
Correct |
1 ms |
348 KB |
n=500 |
34 |
Correct |
0 ms |
348 KB |
n=500 |
35 |
Correct |
1 ms |
348 KB |
n=500 |
36 |
Correct |
4 ms |
348 KB |
n=500 |
37 |
Correct |
3 ms |
348 KB |
n=500 |
38 |
Correct |
4 ms |
344 KB |
n=500 |
39 |
Correct |
2 ms |
348 KB |
n=500 |
40 |
Correct |
3 ms |
348 KB |
n=500 |
41 |
Correct |
2 ms |
348 KB |
n=500 |
42 |
Correct |
2 ms |
348 KB |
n=500 |
43 |
Correct |
2 ms |
520 KB |
n=500 |
44 |
Correct |
2 ms |
348 KB |
n=500 |
45 |
Correct |
1 ms |
348 KB |
n=500 |
46 |
Correct |
1 ms |
348 KB |
n=500 |
47 |
Correct |
1 ms |
348 KB |
n=500 |
48 |
Correct |
0 ms |
344 KB |
n=500 |
49 |
Correct |
1 ms |
348 KB |
n=500 |
50 |
Correct |
1 ms |
348 KB |
n=500 |
51 |
Correct |
1 ms |
348 KB |
n=500 |
52 |
Correct |
1 ms |
348 KB |
n=500 |
53 |
Correct |
1 ms |
348 KB |
n=500 |
54 |
Correct |
1 ms |
344 KB |
n=500 |
55 |
Correct |
1 ms |
344 KB |
n=278 |
56 |
Correct |
1 ms |
356 KB |
n=500 |
57 |
Correct |
1 ms |
344 KB |
n=500 |
58 |
Correct |
3 ms |
348 KB |
n=500 |
59 |
Correct |
2 ms |
792 KB |
n=2000 |
60 |
Correct |
2 ms |
1112 KB |
n=2000 |
61 |
Correct |
4 ms |
604 KB |
n=2000 |
62 |
Correct |
6 ms |
604 KB |
n=2000 |
63 |
Correct |
2 ms |
604 KB |
n=2000 |
64 |
Correct |
5 ms |
604 KB |
n=2000 |
65 |
Correct |
2 ms |
604 KB |
n=2000 |
66 |
Correct |
4 ms |
604 KB |
n=2000 |
67 |
Correct |
2 ms |
604 KB |
n=2000 |
68 |
Correct |
4 ms |
604 KB |
n=2000 |
69 |
Correct |
60 ms |
724 KB |
n=2000 |
70 |
Correct |
60 ms |
604 KB |
n=2000 |
71 |
Correct |
67 ms |
728 KB |
n=2000 |
72 |
Correct |
32 ms |
600 KB |
n=2000 |
73 |
Correct |
30 ms |
600 KB |
n=2000 |
74 |
Correct |
1 ms |
604 KB |
n=1844 |
75 |
Correct |
30 ms |
604 KB |
n=2000 |
76 |
Correct |
30 ms |
600 KB |
n=2000 |
77 |
Correct |
36 ms |
604 KB |
n=2000 |
78 |
Correct |
31 ms |
604 KB |
n=2000 |
79 |
Correct |
1 ms |
604 KB |
n=2000 |
80 |
Correct |
2 ms |
604 KB |
n=2000 |
81 |
Correct |
4 ms |
604 KB |
n=2000 |
82 |
Correct |
1 ms |
604 KB |
n=2000 |
83 |
Correct |
3 ms |
604 KB |
n=2000 |
84 |
Correct |
11 ms |
600 KB |
n=2000 |
85 |
Correct |
19 ms |
860 KB |
n=2000 |
86 |
Correct |
19 ms |
604 KB |
n=2000 |
87 |
Correct |
15 ms |
600 KB |
n=2000 |
88 |
Correct |
12 ms |
856 KB |
n=2000 |
89 |
Correct |
10 ms |
856 KB |
n=2000 |
90 |
Correct |
7 ms |
860 KB |
n=2000 |
91 |
Correct |
45 ms |
728 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=5 |
2 |
Correct |
1 ms |
348 KB |
n=100 |
3 |
Correct |
0 ms |
348 KB |
n=100 |
4 |
Correct |
1 ms |
348 KB |
n=100 |
5 |
Correct |
0 ms |
348 KB |
n=100 |
6 |
Correct |
1 ms |
348 KB |
n=100 |
7 |
Correct |
0 ms |
348 KB |
n=100 |
8 |
Correct |
1 ms |
348 KB |
n=100 |
9 |
Correct |
1 ms |
348 KB |
n=100 |
10 |
Correct |
0 ms |
348 KB |
n=100 |
11 |
Correct |
1 ms |
348 KB |
n=100 |
12 |
Correct |
0 ms |
344 KB |
n=100 |
13 |
Correct |
0 ms |
348 KB |
n=100 |
14 |
Correct |
0 ms |
348 KB |
n=100 |
15 |
Correct |
0 ms |
348 KB |
n=100 |
16 |
Correct |
0 ms |
348 KB |
n=100 |
17 |
Correct |
0 ms |
348 KB |
n=100 |
18 |
Correct |
0 ms |
348 KB |
n=100 |
19 |
Correct |
0 ms |
348 KB |
n=100 |
20 |
Correct |
0 ms |
348 KB |
n=100 |
21 |
Correct |
0 ms |
344 KB |
n=100 |
22 |
Correct |
0 ms |
348 KB |
n=100 |
23 |
Correct |
0 ms |
348 KB |
n=100 |
24 |
Correct |
0 ms |
348 KB |
n=100 |
25 |
Correct |
0 ms |
348 KB |
n=100 |
26 |
Correct |
0 ms |
344 KB |
n=12 |
27 |
Correct |
1 ms |
348 KB |
n=100 |
28 |
Correct |
1 ms |
348 KB |
n=500 |
29 |
Correct |
1 ms |
344 KB |
n=500 |
30 |
Correct |
1 ms |
344 KB |
n=500 |
31 |
Correct |
1 ms |
348 KB |
n=500 |
32 |
Correct |
1 ms |
348 KB |
n=500 |
33 |
Correct |
1 ms |
348 KB |
n=500 |
34 |
Correct |
0 ms |
348 KB |
n=500 |
35 |
Correct |
1 ms |
348 KB |
n=500 |
36 |
Correct |
4 ms |
348 KB |
n=500 |
37 |
Correct |
3 ms |
348 KB |
n=500 |
38 |
Correct |
4 ms |
344 KB |
n=500 |
39 |
Correct |
2 ms |
348 KB |
n=500 |
40 |
Correct |
3 ms |
348 KB |
n=500 |
41 |
Correct |
2 ms |
348 KB |
n=500 |
42 |
Correct |
2 ms |
348 KB |
n=500 |
43 |
Correct |
2 ms |
520 KB |
n=500 |
44 |
Correct |
2 ms |
348 KB |
n=500 |
45 |
Correct |
1 ms |
348 KB |
n=500 |
46 |
Correct |
1 ms |
348 KB |
n=500 |
47 |
Correct |
1 ms |
348 KB |
n=500 |
48 |
Correct |
0 ms |
344 KB |
n=500 |
49 |
Correct |
1 ms |
348 KB |
n=500 |
50 |
Correct |
1 ms |
348 KB |
n=500 |
51 |
Correct |
1 ms |
348 KB |
n=500 |
52 |
Correct |
1 ms |
348 KB |
n=500 |
53 |
Correct |
1 ms |
348 KB |
n=500 |
54 |
Correct |
1 ms |
344 KB |
n=500 |
55 |
Correct |
1 ms |
344 KB |
n=278 |
56 |
Correct |
1 ms |
356 KB |
n=500 |
57 |
Correct |
1 ms |
344 KB |
n=500 |
58 |
Correct |
3 ms |
348 KB |
n=500 |
59 |
Correct |
2 ms |
792 KB |
n=2000 |
60 |
Correct |
2 ms |
1112 KB |
n=2000 |
61 |
Correct |
4 ms |
604 KB |
n=2000 |
62 |
Correct |
6 ms |
604 KB |
n=2000 |
63 |
Correct |
2 ms |
604 KB |
n=2000 |
64 |
Correct |
5 ms |
604 KB |
n=2000 |
65 |
Correct |
2 ms |
604 KB |
n=2000 |
66 |
Correct |
4 ms |
604 KB |
n=2000 |
67 |
Correct |
2 ms |
604 KB |
n=2000 |
68 |
Correct |
4 ms |
604 KB |
n=2000 |
69 |
Correct |
60 ms |
724 KB |
n=2000 |
70 |
Correct |
60 ms |
604 KB |
n=2000 |
71 |
Correct |
67 ms |
728 KB |
n=2000 |
72 |
Correct |
32 ms |
600 KB |
n=2000 |
73 |
Correct |
30 ms |
600 KB |
n=2000 |
74 |
Correct |
1 ms |
604 KB |
n=1844 |
75 |
Correct |
30 ms |
604 KB |
n=2000 |
76 |
Correct |
30 ms |
600 KB |
n=2000 |
77 |
Correct |
36 ms |
604 KB |
n=2000 |
78 |
Correct |
31 ms |
604 KB |
n=2000 |
79 |
Correct |
1 ms |
604 KB |
n=2000 |
80 |
Correct |
2 ms |
604 KB |
n=2000 |
81 |
Correct |
4 ms |
604 KB |
n=2000 |
82 |
Correct |
1 ms |
604 KB |
n=2000 |
83 |
Correct |
3 ms |
604 KB |
n=2000 |
84 |
Correct |
11 ms |
600 KB |
n=2000 |
85 |
Correct |
19 ms |
860 KB |
n=2000 |
86 |
Correct |
19 ms |
604 KB |
n=2000 |
87 |
Correct |
15 ms |
600 KB |
n=2000 |
88 |
Correct |
12 ms |
856 KB |
n=2000 |
89 |
Correct |
10 ms |
856 KB |
n=2000 |
90 |
Correct |
7 ms |
860 KB |
n=2000 |
91 |
Correct |
45 ms |
728 KB |
n=2000 |
92 |
Correct |
2648 ms |
29752 KB |
n=200000 |
93 |
Execution timed out |
4024 ms |
34084 KB |
Time limit exceeded |
94 |
Halted |
0 ms |
0 KB |
- |