//Bismillahir-Rahmanir-Rahim
#include <bits/stdc++.h>
using namespace std;
#define flash ios_base::sync_with_stdio(0),cin.tie(0)
#define pb push_back
#define ll long long
#define ld long double
#define dbg(x) cerr << #x << " = " << x << "\n";
#define ff first
#define ss second
/*
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma comment (linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/
const long long INF = 1e9 + 7;
const long long MOD = 1e9 + 7;
const int maxn = 2e5 + 10;
const int lg = 22;
int n, m, q, a[maxn], upp[maxn][lg + 1], tin[maxn], tout[maxn], timer;
vector <int> g[maxn];
void dfs(int v, int p) {
upp[v][0] = p;
tin[v] = ++timer;
for (int i = 1; i <= lg; i++) {
upp[v][i] = upp[upp[v][i - 1]][i - 1];
}
for (auto to: g[v]) {
if (to != p) {
dfs(to, v);
}
}
tout[v] = timer;
}
bool check(int a, int b) {
if (tin[a] <= tin[b] && tout[a] >= tout[b]) {
return 1;
}
return 0;
}
void press_F_() {
cin >> n >> m >> q;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
for (int i = 1; i <= m; i++) {
cin >> a[i];
}
dfs(1, 0);
while(q--) {
int tp;
cin >> tp;
if (tp == 1) {
int pos, v;
cin >> pos >> v;
a[pos] = v;
} else {
int l, r, u;
cin >> l >> r >> u;
int x = l, nx = INF, ny = -INF, lca;
lca = a[l];
for (int i = l; i <= r; i++) {
if (a[i] == u) {
if (ny - nx + 1 < i - i + 1) {
ny = i;
nx = i;
}
}
if (check(a[i], lca)) {
lca = a[i];
if (lca == u) {
if (ny - nx + 1 < i - x + 1) {
nx = x;
ny = i;
}
}
if (!check(u, lca)) {
x = i;
lca = a[i];
}
continue;
}
if (check(a[i], lca)) {
if (lca == u) {
if (ny - nx + 1 < i - x + 1) {
nx = x;
ny = i;
}
}
if (!check(u, lca)) {
lca = a[i];
x = i;
}
continue;
}
int cur = a[i];
for (int j = lg; j >= 0; j--) {
if (!check(upp[cur][j], lca) && upp[cur][j] != 0) {
cur = upp[cur][j];
}
}
if (upp[cur][0] == u) {
if (ny - nx + 1 < i - x + 1) {
nx = x;
ny = i;
}
lca = upp[cur][0];
}
if (!check(u, lca)) {
lca = a[i];
x = i;
}
else {
lca = upp[cur][0];
}
}
if (nx == INF && ny == -INF) {
nx = -1;
ny = -1;
}
cout << nx << " " << ny << '\n';
}
}
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
int main() {
flash;
// srand(time(0));
int T = 1;
// cin >> T;
for (int i = 1; i <= T; i++) {
// cout << "Case" << " " << i << ':' << "\n";
press_F_();
}
//Respa gold 2023-2024 InshAllah
return 0;
}
/*
Maybe not today and tomorrow, but InshAllah one day I will reach expert
*/
// g++ -std=c++17 main.cpp
// ./a.out
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
n=5 |
2 |
Correct |
2 ms |
8796 KB |
n=100 |
3 |
Correct |
2 ms |
8796 KB |
n=100 |
4 |
Correct |
2 ms |
8796 KB |
n=100 |
5 |
Correct |
2 ms |
8796 KB |
n=100 |
6 |
Correct |
3 ms |
8796 KB |
n=100 |
7 |
Correct |
2 ms |
8792 KB |
n=100 |
8 |
Correct |
2 ms |
8796 KB |
n=100 |
9 |
Correct |
2 ms |
8796 KB |
n=100 |
10 |
Correct |
2 ms |
8796 KB |
n=100 |
11 |
Correct |
2 ms |
8796 KB |
n=100 |
12 |
Correct |
2 ms |
8792 KB |
n=100 |
13 |
Correct |
2 ms |
8796 KB |
n=100 |
14 |
Correct |
2 ms |
8796 KB |
n=100 |
15 |
Correct |
2 ms |
8796 KB |
n=100 |
16 |
Correct |
2 ms |
8796 KB |
n=100 |
17 |
Correct |
2 ms |
8796 KB |
n=100 |
18 |
Correct |
2 ms |
8796 KB |
n=100 |
19 |
Correct |
2 ms |
8796 KB |
n=100 |
20 |
Correct |
2 ms |
8796 KB |
n=100 |
21 |
Correct |
2 ms |
8796 KB |
n=100 |
22 |
Correct |
2 ms |
8796 KB |
n=100 |
23 |
Correct |
2 ms |
8796 KB |
n=100 |
24 |
Correct |
2 ms |
8796 KB |
n=100 |
25 |
Correct |
2 ms |
8796 KB |
n=100 |
26 |
Correct |
1 ms |
8796 KB |
n=12 |
27 |
Correct |
2 ms |
8796 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
n=5 |
2 |
Correct |
2 ms |
8796 KB |
n=100 |
3 |
Correct |
2 ms |
8796 KB |
n=100 |
4 |
Correct |
2 ms |
8796 KB |
n=100 |
5 |
Correct |
2 ms |
8796 KB |
n=100 |
6 |
Correct |
3 ms |
8796 KB |
n=100 |
7 |
Correct |
2 ms |
8792 KB |
n=100 |
8 |
Correct |
2 ms |
8796 KB |
n=100 |
9 |
Correct |
2 ms |
8796 KB |
n=100 |
10 |
Correct |
2 ms |
8796 KB |
n=100 |
11 |
Correct |
2 ms |
8796 KB |
n=100 |
12 |
Correct |
2 ms |
8792 KB |
n=100 |
13 |
Correct |
2 ms |
8796 KB |
n=100 |
14 |
Correct |
2 ms |
8796 KB |
n=100 |
15 |
Correct |
2 ms |
8796 KB |
n=100 |
16 |
Correct |
2 ms |
8796 KB |
n=100 |
17 |
Correct |
2 ms |
8796 KB |
n=100 |
18 |
Correct |
2 ms |
8796 KB |
n=100 |
19 |
Correct |
2 ms |
8796 KB |
n=100 |
20 |
Correct |
2 ms |
8796 KB |
n=100 |
21 |
Correct |
2 ms |
8796 KB |
n=100 |
22 |
Correct |
2 ms |
8796 KB |
n=100 |
23 |
Correct |
2 ms |
8796 KB |
n=100 |
24 |
Correct |
2 ms |
8796 KB |
n=100 |
25 |
Correct |
2 ms |
8796 KB |
n=100 |
26 |
Correct |
1 ms |
8796 KB |
n=12 |
27 |
Correct |
2 ms |
8796 KB |
n=100 |
28 |
Correct |
6 ms |
8796 KB |
n=500 |
29 |
Correct |
6 ms |
8796 KB |
n=500 |
30 |
Correct |
5 ms |
8888 KB |
n=500 |
31 |
Correct |
5 ms |
8796 KB |
n=500 |
32 |
Correct |
8 ms |
8796 KB |
n=500 |
33 |
Correct |
6 ms |
8792 KB |
n=500 |
34 |
Correct |
6 ms |
8796 KB |
n=500 |
35 |
Correct |
5 ms |
8792 KB |
n=500 |
36 |
Correct |
25 ms |
8796 KB |
n=500 |
37 |
Correct |
25 ms |
8792 KB |
n=500 |
38 |
Correct |
25 ms |
8792 KB |
n=500 |
39 |
Correct |
8 ms |
8796 KB |
n=500 |
40 |
Correct |
7 ms |
8796 KB |
n=500 |
41 |
Correct |
8 ms |
8796 KB |
n=500 |
42 |
Correct |
12 ms |
8796 KB |
n=500 |
43 |
Correct |
13 ms |
8792 KB |
n=500 |
44 |
Correct |
12 ms |
8796 KB |
n=500 |
45 |
Correct |
5 ms |
8796 KB |
n=500 |
46 |
Correct |
6 ms |
8796 KB |
n=500 |
47 |
Correct |
5 ms |
8796 KB |
n=500 |
48 |
Correct |
5 ms |
8796 KB |
n=500 |
49 |
Correct |
5 ms |
8796 KB |
n=500 |
50 |
Correct |
6 ms |
8796 KB |
n=500 |
51 |
Correct |
5 ms |
8796 KB |
n=500 |
52 |
Correct |
6 ms |
8792 KB |
n=500 |
53 |
Correct |
6 ms |
8796 KB |
n=500 |
54 |
Correct |
11 ms |
8796 KB |
n=500 |
55 |
Correct |
3 ms |
9048 KB |
n=278 |
56 |
Correct |
12 ms |
8900 KB |
n=500 |
57 |
Correct |
12 ms |
8792 KB |
n=500 |
58 |
Correct |
12 ms |
8792 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
n=5 |
2 |
Correct |
2 ms |
8796 KB |
n=100 |
3 |
Correct |
2 ms |
8796 KB |
n=100 |
4 |
Correct |
2 ms |
8796 KB |
n=100 |
5 |
Correct |
2 ms |
8796 KB |
n=100 |
6 |
Correct |
3 ms |
8796 KB |
n=100 |
7 |
Correct |
2 ms |
8792 KB |
n=100 |
8 |
Correct |
2 ms |
8796 KB |
n=100 |
9 |
Correct |
2 ms |
8796 KB |
n=100 |
10 |
Correct |
2 ms |
8796 KB |
n=100 |
11 |
Correct |
2 ms |
8796 KB |
n=100 |
12 |
Correct |
2 ms |
8792 KB |
n=100 |
13 |
Correct |
2 ms |
8796 KB |
n=100 |
14 |
Correct |
2 ms |
8796 KB |
n=100 |
15 |
Correct |
2 ms |
8796 KB |
n=100 |
16 |
Correct |
2 ms |
8796 KB |
n=100 |
17 |
Correct |
2 ms |
8796 KB |
n=100 |
18 |
Correct |
2 ms |
8796 KB |
n=100 |
19 |
Correct |
2 ms |
8796 KB |
n=100 |
20 |
Correct |
2 ms |
8796 KB |
n=100 |
21 |
Correct |
2 ms |
8796 KB |
n=100 |
22 |
Correct |
2 ms |
8796 KB |
n=100 |
23 |
Correct |
2 ms |
8796 KB |
n=100 |
24 |
Correct |
2 ms |
8796 KB |
n=100 |
25 |
Correct |
2 ms |
8796 KB |
n=100 |
26 |
Correct |
1 ms |
8796 KB |
n=12 |
27 |
Correct |
2 ms |
8796 KB |
n=100 |
28 |
Correct |
6 ms |
8796 KB |
n=500 |
29 |
Correct |
6 ms |
8796 KB |
n=500 |
30 |
Correct |
5 ms |
8888 KB |
n=500 |
31 |
Correct |
5 ms |
8796 KB |
n=500 |
32 |
Correct |
8 ms |
8796 KB |
n=500 |
33 |
Correct |
6 ms |
8792 KB |
n=500 |
34 |
Correct |
6 ms |
8796 KB |
n=500 |
35 |
Correct |
5 ms |
8792 KB |
n=500 |
36 |
Correct |
25 ms |
8796 KB |
n=500 |
37 |
Correct |
25 ms |
8792 KB |
n=500 |
38 |
Correct |
25 ms |
8792 KB |
n=500 |
39 |
Correct |
8 ms |
8796 KB |
n=500 |
40 |
Correct |
7 ms |
8796 KB |
n=500 |
41 |
Correct |
8 ms |
8796 KB |
n=500 |
42 |
Correct |
12 ms |
8796 KB |
n=500 |
43 |
Correct |
13 ms |
8792 KB |
n=500 |
44 |
Correct |
12 ms |
8796 KB |
n=500 |
45 |
Correct |
5 ms |
8796 KB |
n=500 |
46 |
Correct |
6 ms |
8796 KB |
n=500 |
47 |
Correct |
5 ms |
8796 KB |
n=500 |
48 |
Correct |
5 ms |
8796 KB |
n=500 |
49 |
Correct |
5 ms |
8796 KB |
n=500 |
50 |
Correct |
6 ms |
8796 KB |
n=500 |
51 |
Correct |
5 ms |
8796 KB |
n=500 |
52 |
Correct |
6 ms |
8792 KB |
n=500 |
53 |
Correct |
6 ms |
8796 KB |
n=500 |
54 |
Correct |
11 ms |
8796 KB |
n=500 |
55 |
Correct |
3 ms |
9048 KB |
n=278 |
56 |
Correct |
12 ms |
8900 KB |
n=500 |
57 |
Correct |
12 ms |
8792 KB |
n=500 |
58 |
Correct |
12 ms |
8792 KB |
n=500 |
59 |
Correct |
68 ms |
10844 KB |
n=2000 |
60 |
Correct |
74 ms |
11244 KB |
n=2000 |
61 |
Correct |
78 ms |
10844 KB |
n=2000 |
62 |
Correct |
81 ms |
10840 KB |
n=2000 |
63 |
Correct |
75 ms |
10844 KB |
n=2000 |
64 |
Correct |
82 ms |
10844 KB |
n=2000 |
65 |
Correct |
72 ms |
10840 KB |
n=2000 |
66 |
Correct |
82 ms |
10844 KB |
n=2000 |
67 |
Correct |
82 ms |
10844 KB |
n=2000 |
68 |
Correct |
77 ms |
11048 KB |
n=2000 |
69 |
Correct |
391 ms |
11016 KB |
n=2000 |
70 |
Correct |
393 ms |
10844 KB |
n=2000 |
71 |
Correct |
386 ms |
11016 KB |
n=2000 |
72 |
Correct |
98 ms |
10840 KB |
n=2000 |
73 |
Correct |
101 ms |
11092 KB |
n=2000 |
74 |
Correct |
57 ms |
10840 KB |
n=1844 |
75 |
Correct |
99 ms |
10844 KB |
n=2000 |
76 |
Correct |
194 ms |
11004 KB |
n=2000 |
77 |
Correct |
197 ms |
11008 KB |
n=2000 |
78 |
Correct |
203 ms |
11352 KB |
n=2000 |
79 |
Correct |
59 ms |
10844 KB |
n=2000 |
80 |
Correct |
66 ms |
10844 KB |
n=2000 |
81 |
Correct |
69 ms |
11036 KB |
n=2000 |
82 |
Correct |
58 ms |
10844 KB |
n=2000 |
83 |
Correct |
66 ms |
11064 KB |
n=2000 |
84 |
Correct |
77 ms |
11012 KB |
n=2000 |
85 |
Correct |
79 ms |
11352 KB |
n=2000 |
86 |
Correct |
82 ms |
10840 KB |
n=2000 |
87 |
Correct |
78 ms |
11040 KB |
n=2000 |
88 |
Correct |
285 ms |
11100 KB |
n=2000 |
89 |
Correct |
280 ms |
11096 KB |
n=2000 |
90 |
Correct |
237 ms |
11100 KB |
n=2000 |
91 |
Correct |
200 ms |
11012 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
n=5 |
2 |
Correct |
2 ms |
8796 KB |
n=100 |
3 |
Correct |
2 ms |
8796 KB |
n=100 |
4 |
Correct |
2 ms |
8796 KB |
n=100 |
5 |
Correct |
2 ms |
8796 KB |
n=100 |
6 |
Correct |
3 ms |
8796 KB |
n=100 |
7 |
Correct |
2 ms |
8792 KB |
n=100 |
8 |
Correct |
2 ms |
8796 KB |
n=100 |
9 |
Correct |
2 ms |
8796 KB |
n=100 |
10 |
Correct |
2 ms |
8796 KB |
n=100 |
11 |
Correct |
2 ms |
8796 KB |
n=100 |
12 |
Correct |
2 ms |
8792 KB |
n=100 |
13 |
Correct |
2 ms |
8796 KB |
n=100 |
14 |
Correct |
2 ms |
8796 KB |
n=100 |
15 |
Correct |
2 ms |
8796 KB |
n=100 |
16 |
Correct |
2 ms |
8796 KB |
n=100 |
17 |
Correct |
2 ms |
8796 KB |
n=100 |
18 |
Correct |
2 ms |
8796 KB |
n=100 |
19 |
Correct |
2 ms |
8796 KB |
n=100 |
20 |
Correct |
2 ms |
8796 KB |
n=100 |
21 |
Correct |
2 ms |
8796 KB |
n=100 |
22 |
Correct |
2 ms |
8796 KB |
n=100 |
23 |
Correct |
2 ms |
8796 KB |
n=100 |
24 |
Correct |
2 ms |
8796 KB |
n=100 |
25 |
Correct |
2 ms |
8796 KB |
n=100 |
26 |
Correct |
1 ms |
8796 KB |
n=12 |
27 |
Correct |
2 ms |
8796 KB |
n=100 |
28 |
Correct |
6 ms |
8796 KB |
n=500 |
29 |
Correct |
6 ms |
8796 KB |
n=500 |
30 |
Correct |
5 ms |
8888 KB |
n=500 |
31 |
Correct |
5 ms |
8796 KB |
n=500 |
32 |
Correct |
8 ms |
8796 KB |
n=500 |
33 |
Correct |
6 ms |
8792 KB |
n=500 |
34 |
Correct |
6 ms |
8796 KB |
n=500 |
35 |
Correct |
5 ms |
8792 KB |
n=500 |
36 |
Correct |
25 ms |
8796 KB |
n=500 |
37 |
Correct |
25 ms |
8792 KB |
n=500 |
38 |
Correct |
25 ms |
8792 KB |
n=500 |
39 |
Correct |
8 ms |
8796 KB |
n=500 |
40 |
Correct |
7 ms |
8796 KB |
n=500 |
41 |
Correct |
8 ms |
8796 KB |
n=500 |
42 |
Correct |
12 ms |
8796 KB |
n=500 |
43 |
Correct |
13 ms |
8792 KB |
n=500 |
44 |
Correct |
12 ms |
8796 KB |
n=500 |
45 |
Correct |
5 ms |
8796 KB |
n=500 |
46 |
Correct |
6 ms |
8796 KB |
n=500 |
47 |
Correct |
5 ms |
8796 KB |
n=500 |
48 |
Correct |
5 ms |
8796 KB |
n=500 |
49 |
Correct |
5 ms |
8796 KB |
n=500 |
50 |
Correct |
6 ms |
8796 KB |
n=500 |
51 |
Correct |
5 ms |
8796 KB |
n=500 |
52 |
Correct |
6 ms |
8792 KB |
n=500 |
53 |
Correct |
6 ms |
8796 KB |
n=500 |
54 |
Correct |
11 ms |
8796 KB |
n=500 |
55 |
Correct |
3 ms |
9048 KB |
n=278 |
56 |
Correct |
12 ms |
8900 KB |
n=500 |
57 |
Correct |
12 ms |
8792 KB |
n=500 |
58 |
Correct |
12 ms |
8792 KB |
n=500 |
59 |
Correct |
68 ms |
10844 KB |
n=2000 |
60 |
Correct |
74 ms |
11244 KB |
n=2000 |
61 |
Correct |
78 ms |
10844 KB |
n=2000 |
62 |
Correct |
81 ms |
10840 KB |
n=2000 |
63 |
Correct |
75 ms |
10844 KB |
n=2000 |
64 |
Correct |
82 ms |
10844 KB |
n=2000 |
65 |
Correct |
72 ms |
10840 KB |
n=2000 |
66 |
Correct |
82 ms |
10844 KB |
n=2000 |
67 |
Correct |
82 ms |
10844 KB |
n=2000 |
68 |
Correct |
77 ms |
11048 KB |
n=2000 |
69 |
Correct |
391 ms |
11016 KB |
n=2000 |
70 |
Correct |
393 ms |
10844 KB |
n=2000 |
71 |
Correct |
386 ms |
11016 KB |
n=2000 |
72 |
Correct |
98 ms |
10840 KB |
n=2000 |
73 |
Correct |
101 ms |
11092 KB |
n=2000 |
74 |
Correct |
57 ms |
10840 KB |
n=1844 |
75 |
Correct |
99 ms |
10844 KB |
n=2000 |
76 |
Correct |
194 ms |
11004 KB |
n=2000 |
77 |
Correct |
197 ms |
11008 KB |
n=2000 |
78 |
Correct |
203 ms |
11352 KB |
n=2000 |
79 |
Correct |
59 ms |
10844 KB |
n=2000 |
80 |
Correct |
66 ms |
10844 KB |
n=2000 |
81 |
Correct |
69 ms |
11036 KB |
n=2000 |
82 |
Correct |
58 ms |
10844 KB |
n=2000 |
83 |
Correct |
66 ms |
11064 KB |
n=2000 |
84 |
Correct |
77 ms |
11012 KB |
n=2000 |
85 |
Correct |
79 ms |
11352 KB |
n=2000 |
86 |
Correct |
82 ms |
10840 KB |
n=2000 |
87 |
Correct |
78 ms |
11040 KB |
n=2000 |
88 |
Correct |
285 ms |
11100 KB |
n=2000 |
89 |
Correct |
280 ms |
11096 KB |
n=2000 |
90 |
Correct |
237 ms |
11100 KB |
n=2000 |
91 |
Correct |
200 ms |
11012 KB |
n=2000 |
92 |
Execution timed out |
4021 ms |
36244 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |