#include <bits/stdc++.h>
using namespace std;
#define FOR(i, x, y) for (int i = x; i < y; i++)
const int mx = 2e5 + 5, ml = 18;
int n, m, q, up[mx][ml], dep[mx], A[mx], L[mx];
vector<int> adj[mx]; set<int> posA[mx], posL[mx];
void dfs(int cur){
FOR(i, 1, ml) up[cur][i] = up[up[cur][i - 1]][i - 1];
for (int to : adj[cur]) if (to != up[cur][0]){
up[to][0] = cur; dep[to] = dep[cur] + 1;
dfs(to);
}
}
int lca(int a, int b){
if (dep[a] < dep[b]) swap(a, b);
int dif = dep[a] - dep[b];
FOR(i, 0, 18) if ((dif >> i) % 2) a = up[a][i];
if (a == b) return a;
for (int i = 17; ~i; i--) if (up[a][i] != up[b][i]) a = up[a][i], b = up[b][i];
return up[a][0];
}
void upd(int i, int v){
posA[A[i]].erase(i); A[i] = v; posA[A[i]].insert(i);
posL[L[i - 1]].erase(i - 1); posL[L[i]].erase(i);
L[i - 1] = lca(A[i - 1], A[i]); L[i] = lca(A[i], A[i + 1]);
posL[L[i - 1]].insert(i - 1); posL[L[i]].insert(i);
}
void solve(int l, int r, int v){
auto itA = posA[v].lower_bound(l);
auto itL = posL[v].lower_bound(l);
if (itA != posA[v].end() and *itA <= r) cout<<*itA<<" "<<*itA<<"\n";
else if (itL != posL[v].end() and *itL < r) cout<<*itL<<" "<<*itL + 1<<"\n";
else cout<<"-1 -1"<<"\n";
}
int main(){
cin >> n >> m >> q;
FOR(i, 1, n){
int a, b; cin >> a >> b;
adj[a].push_back(b); adj[b].push_back(a);
}
dfs(1);
FOR(i, 1, m + 1){ int v; cin >> v; upd(i, v); }
while (q--){
int t; cin >> t;
if (t == 1){ int a, v; cin >> a >> v; upd(a, v); }
else{ int l, r, v; cin >> l >> r >> v; solve(l, r, v); }
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=5 |
2 |
Correct |
12 ms |
23756 KB |
n=100 |
3 |
Correct |
11 ms |
23760 KB |
n=100 |
4 |
Correct |
12 ms |
23796 KB |
n=100 |
5 |
Correct |
12 ms |
23784 KB |
n=100 |
6 |
Correct |
11 ms |
23756 KB |
n=100 |
7 |
Correct |
11 ms |
23756 KB |
n=100 |
8 |
Correct |
11 ms |
23704 KB |
n=100 |
9 |
Correct |
12 ms |
23756 KB |
n=100 |
10 |
Correct |
11 ms |
23784 KB |
n=100 |
11 |
Correct |
11 ms |
23756 KB |
n=100 |
12 |
Correct |
14 ms |
23756 KB |
n=100 |
13 |
Correct |
14 ms |
23784 KB |
n=100 |
14 |
Correct |
11 ms |
23784 KB |
n=100 |
15 |
Correct |
11 ms |
23780 KB |
n=100 |
16 |
Correct |
12 ms |
23756 KB |
n=100 |
17 |
Correct |
11 ms |
23756 KB |
n=100 |
18 |
Correct |
12 ms |
23756 KB |
n=100 |
19 |
Correct |
11 ms |
23776 KB |
n=100 |
20 |
Correct |
12 ms |
23756 KB |
n=100 |
21 |
Correct |
11 ms |
23780 KB |
n=100 |
22 |
Correct |
12 ms |
23756 KB |
n=100 |
23 |
Correct |
12 ms |
23756 KB |
n=100 |
24 |
Correct |
13 ms |
23756 KB |
n=100 |
25 |
Correct |
12 ms |
23780 KB |
n=100 |
26 |
Correct |
11 ms |
23756 KB |
n=12 |
27 |
Correct |
11 ms |
23756 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=5 |
2 |
Correct |
12 ms |
23756 KB |
n=100 |
3 |
Correct |
11 ms |
23760 KB |
n=100 |
4 |
Correct |
12 ms |
23796 KB |
n=100 |
5 |
Correct |
12 ms |
23784 KB |
n=100 |
6 |
Correct |
11 ms |
23756 KB |
n=100 |
7 |
Correct |
11 ms |
23756 KB |
n=100 |
8 |
Correct |
11 ms |
23704 KB |
n=100 |
9 |
Correct |
12 ms |
23756 KB |
n=100 |
10 |
Correct |
11 ms |
23784 KB |
n=100 |
11 |
Correct |
11 ms |
23756 KB |
n=100 |
12 |
Correct |
14 ms |
23756 KB |
n=100 |
13 |
Correct |
14 ms |
23784 KB |
n=100 |
14 |
Correct |
11 ms |
23784 KB |
n=100 |
15 |
Correct |
11 ms |
23780 KB |
n=100 |
16 |
Correct |
12 ms |
23756 KB |
n=100 |
17 |
Correct |
11 ms |
23756 KB |
n=100 |
18 |
Correct |
12 ms |
23756 KB |
n=100 |
19 |
Correct |
11 ms |
23776 KB |
n=100 |
20 |
Correct |
12 ms |
23756 KB |
n=100 |
21 |
Correct |
11 ms |
23780 KB |
n=100 |
22 |
Correct |
12 ms |
23756 KB |
n=100 |
23 |
Correct |
12 ms |
23756 KB |
n=100 |
24 |
Correct |
13 ms |
23756 KB |
n=100 |
25 |
Correct |
12 ms |
23780 KB |
n=100 |
26 |
Correct |
11 ms |
23756 KB |
n=12 |
27 |
Correct |
11 ms |
23756 KB |
n=100 |
28 |
Correct |
12 ms |
23856 KB |
n=500 |
29 |
Correct |
12 ms |
23952 KB |
n=500 |
30 |
Correct |
13 ms |
23884 KB |
n=500 |
31 |
Correct |
13 ms |
23884 KB |
n=500 |
32 |
Correct |
12 ms |
23884 KB |
n=500 |
33 |
Correct |
13 ms |
23916 KB |
n=500 |
34 |
Correct |
13 ms |
23884 KB |
n=500 |
35 |
Correct |
12 ms |
23884 KB |
n=500 |
36 |
Correct |
13 ms |
23884 KB |
n=500 |
37 |
Correct |
16 ms |
23884 KB |
n=500 |
38 |
Correct |
13 ms |
23920 KB |
n=500 |
39 |
Correct |
13 ms |
23924 KB |
n=500 |
40 |
Correct |
13 ms |
23884 KB |
n=500 |
41 |
Correct |
13 ms |
23924 KB |
n=500 |
42 |
Correct |
12 ms |
23884 KB |
n=500 |
43 |
Correct |
13 ms |
23904 KB |
n=500 |
44 |
Correct |
12 ms |
23888 KB |
n=500 |
45 |
Correct |
12 ms |
23916 KB |
n=500 |
46 |
Correct |
13 ms |
23916 KB |
n=500 |
47 |
Correct |
13 ms |
23916 KB |
n=500 |
48 |
Correct |
13 ms |
23840 KB |
n=500 |
49 |
Correct |
12 ms |
23884 KB |
n=500 |
50 |
Correct |
13 ms |
23920 KB |
n=500 |
51 |
Correct |
13 ms |
23884 KB |
n=500 |
52 |
Correct |
13 ms |
23884 KB |
n=500 |
53 |
Correct |
13 ms |
23920 KB |
n=500 |
54 |
Correct |
13 ms |
23884 KB |
n=500 |
55 |
Correct |
13 ms |
23884 KB |
n=278 |
56 |
Correct |
13 ms |
23884 KB |
n=500 |
57 |
Correct |
13 ms |
23884 KB |
n=500 |
58 |
Correct |
13 ms |
23924 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=5 |
2 |
Correct |
12 ms |
23756 KB |
n=100 |
3 |
Correct |
11 ms |
23760 KB |
n=100 |
4 |
Correct |
12 ms |
23796 KB |
n=100 |
5 |
Correct |
12 ms |
23784 KB |
n=100 |
6 |
Correct |
11 ms |
23756 KB |
n=100 |
7 |
Correct |
11 ms |
23756 KB |
n=100 |
8 |
Correct |
11 ms |
23704 KB |
n=100 |
9 |
Correct |
12 ms |
23756 KB |
n=100 |
10 |
Correct |
11 ms |
23784 KB |
n=100 |
11 |
Correct |
11 ms |
23756 KB |
n=100 |
12 |
Correct |
14 ms |
23756 KB |
n=100 |
13 |
Correct |
14 ms |
23784 KB |
n=100 |
14 |
Correct |
11 ms |
23784 KB |
n=100 |
15 |
Correct |
11 ms |
23780 KB |
n=100 |
16 |
Correct |
12 ms |
23756 KB |
n=100 |
17 |
Correct |
11 ms |
23756 KB |
n=100 |
18 |
Correct |
12 ms |
23756 KB |
n=100 |
19 |
Correct |
11 ms |
23776 KB |
n=100 |
20 |
Correct |
12 ms |
23756 KB |
n=100 |
21 |
Correct |
11 ms |
23780 KB |
n=100 |
22 |
Correct |
12 ms |
23756 KB |
n=100 |
23 |
Correct |
12 ms |
23756 KB |
n=100 |
24 |
Correct |
13 ms |
23756 KB |
n=100 |
25 |
Correct |
12 ms |
23780 KB |
n=100 |
26 |
Correct |
11 ms |
23756 KB |
n=12 |
27 |
Correct |
11 ms |
23756 KB |
n=100 |
28 |
Correct |
12 ms |
23856 KB |
n=500 |
29 |
Correct |
12 ms |
23952 KB |
n=500 |
30 |
Correct |
13 ms |
23884 KB |
n=500 |
31 |
Correct |
13 ms |
23884 KB |
n=500 |
32 |
Correct |
12 ms |
23884 KB |
n=500 |
33 |
Correct |
13 ms |
23916 KB |
n=500 |
34 |
Correct |
13 ms |
23884 KB |
n=500 |
35 |
Correct |
12 ms |
23884 KB |
n=500 |
36 |
Correct |
13 ms |
23884 KB |
n=500 |
37 |
Correct |
16 ms |
23884 KB |
n=500 |
38 |
Correct |
13 ms |
23920 KB |
n=500 |
39 |
Correct |
13 ms |
23924 KB |
n=500 |
40 |
Correct |
13 ms |
23884 KB |
n=500 |
41 |
Correct |
13 ms |
23924 KB |
n=500 |
42 |
Correct |
12 ms |
23884 KB |
n=500 |
43 |
Correct |
13 ms |
23904 KB |
n=500 |
44 |
Correct |
12 ms |
23888 KB |
n=500 |
45 |
Correct |
12 ms |
23916 KB |
n=500 |
46 |
Correct |
13 ms |
23916 KB |
n=500 |
47 |
Correct |
13 ms |
23916 KB |
n=500 |
48 |
Correct |
13 ms |
23840 KB |
n=500 |
49 |
Correct |
12 ms |
23884 KB |
n=500 |
50 |
Correct |
13 ms |
23920 KB |
n=500 |
51 |
Correct |
13 ms |
23884 KB |
n=500 |
52 |
Correct |
13 ms |
23884 KB |
n=500 |
53 |
Correct |
13 ms |
23920 KB |
n=500 |
54 |
Correct |
13 ms |
23884 KB |
n=500 |
55 |
Correct |
13 ms |
23884 KB |
n=278 |
56 |
Correct |
13 ms |
23884 KB |
n=500 |
57 |
Correct |
13 ms |
23884 KB |
n=500 |
58 |
Correct |
13 ms |
23924 KB |
n=500 |
59 |
Correct |
17 ms |
24268 KB |
n=2000 |
60 |
Correct |
17 ms |
24304 KB |
n=2000 |
61 |
Correct |
20 ms |
24396 KB |
n=2000 |
62 |
Correct |
17 ms |
24220 KB |
n=2000 |
63 |
Correct |
17 ms |
24268 KB |
n=2000 |
64 |
Correct |
17 ms |
24268 KB |
n=2000 |
65 |
Correct |
17 ms |
24180 KB |
n=2000 |
66 |
Correct |
18 ms |
24308 KB |
n=2000 |
67 |
Correct |
17 ms |
24268 KB |
n=2000 |
68 |
Correct |
17 ms |
24268 KB |
n=2000 |
69 |
Correct |
17 ms |
24288 KB |
n=2000 |
70 |
Correct |
18 ms |
24180 KB |
n=2000 |
71 |
Correct |
18 ms |
24268 KB |
n=2000 |
72 |
Correct |
18 ms |
24180 KB |
n=2000 |
73 |
Correct |
19 ms |
24280 KB |
n=2000 |
74 |
Correct |
16 ms |
24268 KB |
n=1844 |
75 |
Correct |
17 ms |
24180 KB |
n=2000 |
76 |
Correct |
17 ms |
24268 KB |
n=2000 |
77 |
Correct |
17 ms |
24268 KB |
n=2000 |
78 |
Correct |
17 ms |
24176 KB |
n=2000 |
79 |
Correct |
19 ms |
24240 KB |
n=2000 |
80 |
Correct |
17 ms |
24268 KB |
n=2000 |
81 |
Correct |
17 ms |
24268 KB |
n=2000 |
82 |
Correct |
17 ms |
24176 KB |
n=2000 |
83 |
Correct |
17 ms |
24356 KB |
n=2000 |
84 |
Correct |
17 ms |
24180 KB |
n=2000 |
85 |
Correct |
17 ms |
24312 KB |
n=2000 |
86 |
Correct |
18 ms |
24212 KB |
n=2000 |
87 |
Correct |
17 ms |
24180 KB |
n=2000 |
88 |
Correct |
16 ms |
24396 KB |
n=2000 |
89 |
Correct |
16 ms |
24304 KB |
n=2000 |
90 |
Correct |
17 ms |
24304 KB |
n=2000 |
91 |
Correct |
18 ms |
24268 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=5 |
2 |
Correct |
12 ms |
23756 KB |
n=100 |
3 |
Correct |
11 ms |
23760 KB |
n=100 |
4 |
Correct |
12 ms |
23796 KB |
n=100 |
5 |
Correct |
12 ms |
23784 KB |
n=100 |
6 |
Correct |
11 ms |
23756 KB |
n=100 |
7 |
Correct |
11 ms |
23756 KB |
n=100 |
8 |
Correct |
11 ms |
23704 KB |
n=100 |
9 |
Correct |
12 ms |
23756 KB |
n=100 |
10 |
Correct |
11 ms |
23784 KB |
n=100 |
11 |
Correct |
11 ms |
23756 KB |
n=100 |
12 |
Correct |
14 ms |
23756 KB |
n=100 |
13 |
Correct |
14 ms |
23784 KB |
n=100 |
14 |
Correct |
11 ms |
23784 KB |
n=100 |
15 |
Correct |
11 ms |
23780 KB |
n=100 |
16 |
Correct |
12 ms |
23756 KB |
n=100 |
17 |
Correct |
11 ms |
23756 KB |
n=100 |
18 |
Correct |
12 ms |
23756 KB |
n=100 |
19 |
Correct |
11 ms |
23776 KB |
n=100 |
20 |
Correct |
12 ms |
23756 KB |
n=100 |
21 |
Correct |
11 ms |
23780 KB |
n=100 |
22 |
Correct |
12 ms |
23756 KB |
n=100 |
23 |
Correct |
12 ms |
23756 KB |
n=100 |
24 |
Correct |
13 ms |
23756 KB |
n=100 |
25 |
Correct |
12 ms |
23780 KB |
n=100 |
26 |
Correct |
11 ms |
23756 KB |
n=12 |
27 |
Correct |
11 ms |
23756 KB |
n=100 |
28 |
Correct |
12 ms |
23856 KB |
n=500 |
29 |
Correct |
12 ms |
23952 KB |
n=500 |
30 |
Correct |
13 ms |
23884 KB |
n=500 |
31 |
Correct |
13 ms |
23884 KB |
n=500 |
32 |
Correct |
12 ms |
23884 KB |
n=500 |
33 |
Correct |
13 ms |
23916 KB |
n=500 |
34 |
Correct |
13 ms |
23884 KB |
n=500 |
35 |
Correct |
12 ms |
23884 KB |
n=500 |
36 |
Correct |
13 ms |
23884 KB |
n=500 |
37 |
Correct |
16 ms |
23884 KB |
n=500 |
38 |
Correct |
13 ms |
23920 KB |
n=500 |
39 |
Correct |
13 ms |
23924 KB |
n=500 |
40 |
Correct |
13 ms |
23884 KB |
n=500 |
41 |
Correct |
13 ms |
23924 KB |
n=500 |
42 |
Correct |
12 ms |
23884 KB |
n=500 |
43 |
Correct |
13 ms |
23904 KB |
n=500 |
44 |
Correct |
12 ms |
23888 KB |
n=500 |
45 |
Correct |
12 ms |
23916 KB |
n=500 |
46 |
Correct |
13 ms |
23916 KB |
n=500 |
47 |
Correct |
13 ms |
23916 KB |
n=500 |
48 |
Correct |
13 ms |
23840 KB |
n=500 |
49 |
Correct |
12 ms |
23884 KB |
n=500 |
50 |
Correct |
13 ms |
23920 KB |
n=500 |
51 |
Correct |
13 ms |
23884 KB |
n=500 |
52 |
Correct |
13 ms |
23884 KB |
n=500 |
53 |
Correct |
13 ms |
23920 KB |
n=500 |
54 |
Correct |
13 ms |
23884 KB |
n=500 |
55 |
Correct |
13 ms |
23884 KB |
n=278 |
56 |
Correct |
13 ms |
23884 KB |
n=500 |
57 |
Correct |
13 ms |
23884 KB |
n=500 |
58 |
Correct |
13 ms |
23924 KB |
n=500 |
59 |
Correct |
17 ms |
24268 KB |
n=2000 |
60 |
Correct |
17 ms |
24304 KB |
n=2000 |
61 |
Correct |
20 ms |
24396 KB |
n=2000 |
62 |
Correct |
17 ms |
24220 KB |
n=2000 |
63 |
Correct |
17 ms |
24268 KB |
n=2000 |
64 |
Correct |
17 ms |
24268 KB |
n=2000 |
65 |
Correct |
17 ms |
24180 KB |
n=2000 |
66 |
Correct |
18 ms |
24308 KB |
n=2000 |
67 |
Correct |
17 ms |
24268 KB |
n=2000 |
68 |
Correct |
17 ms |
24268 KB |
n=2000 |
69 |
Correct |
17 ms |
24288 KB |
n=2000 |
70 |
Correct |
18 ms |
24180 KB |
n=2000 |
71 |
Correct |
18 ms |
24268 KB |
n=2000 |
72 |
Correct |
18 ms |
24180 KB |
n=2000 |
73 |
Correct |
19 ms |
24280 KB |
n=2000 |
74 |
Correct |
16 ms |
24268 KB |
n=1844 |
75 |
Correct |
17 ms |
24180 KB |
n=2000 |
76 |
Correct |
17 ms |
24268 KB |
n=2000 |
77 |
Correct |
17 ms |
24268 KB |
n=2000 |
78 |
Correct |
17 ms |
24176 KB |
n=2000 |
79 |
Correct |
19 ms |
24240 KB |
n=2000 |
80 |
Correct |
17 ms |
24268 KB |
n=2000 |
81 |
Correct |
17 ms |
24268 KB |
n=2000 |
82 |
Correct |
17 ms |
24176 KB |
n=2000 |
83 |
Correct |
17 ms |
24356 KB |
n=2000 |
84 |
Correct |
17 ms |
24180 KB |
n=2000 |
85 |
Correct |
17 ms |
24312 KB |
n=2000 |
86 |
Correct |
18 ms |
24212 KB |
n=2000 |
87 |
Correct |
17 ms |
24180 KB |
n=2000 |
88 |
Correct |
16 ms |
24396 KB |
n=2000 |
89 |
Correct |
16 ms |
24304 KB |
n=2000 |
90 |
Correct |
17 ms |
24304 KB |
n=2000 |
91 |
Correct |
18 ms |
24268 KB |
n=2000 |
92 |
Correct |
980 ms |
73648 KB |
n=200000 |
93 |
Correct |
1141 ms |
80244 KB |
n=200000 |
94 |
Correct |
1091 ms |
84660 KB |
n=200000 |
95 |
Correct |
937 ms |
73400 KB |
n=200000 |
96 |
Correct |
959 ms |
73756 KB |
n=200000 |
97 |
Correct |
1240 ms |
78940 KB |
n=200000 |
98 |
Correct |
1008 ms |
73652 KB |
n=200000 |
99 |
Correct |
1177 ms |
73828 KB |
n=200000 |
100 |
Correct |
970 ms |
73516 KB |
n=200000 |
101 |
Correct |
1053 ms |
86280 KB |
n=200000 |
102 |
Correct |
828 ms |
74692 KB |
n=200000 |
103 |
Correct |
846 ms |
74676 KB |
n=200000 |
104 |
Correct |
846 ms |
74736 KB |
n=200000 |
105 |
Correct |
874 ms |
75072 KB |
n=200000 |
106 |
Correct |
859 ms |
75128 KB |
n=200000 |
107 |
Correct |
856 ms |
75124 KB |
n=200000 |
108 |
Correct |
1014 ms |
73540 KB |
n=200000 |
109 |
Correct |
1018 ms |
73668 KB |
n=200000 |
110 |
Correct |
1018 ms |
73544 KB |
n=200000 |
111 |
Correct |
961 ms |
73016 KB |
n=200000 |
112 |
Correct |
1093 ms |
84880 KB |
n=200000 |
113 |
Correct |
1120 ms |
78916 KB |
n=200000 |
114 |
Correct |
1037 ms |
73144 KB |
n=200000 |
115 |
Correct |
1242 ms |
76168 KB |
n=200000 |
116 |
Correct |
916 ms |
73780 KB |
n=200000 |
117 |
Correct |
1036 ms |
85572 KB |
n=200000 |
118 |
Correct |
1149 ms |
77456 KB |
n=200000 |
119 |
Correct |
912 ms |
73740 KB |
n=200000 |
120 |
Correct |
1008 ms |
85436 KB |
n=200000 |
121 |
Correct |
1056 ms |
85256 KB |
n=200000 |
122 |
Correct |
1000 ms |
85572 KB |
n=200000 |
123 |
Correct |
886 ms |
74932 KB |
n=200000 |
124 |
Correct |
279 ms |
39168 KB |
n=25264 |