#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << " " <<
#define nl << "\n"
vector<vector<int>> g;
vector<array<int, 19>> p;
vector<int> t0, t1;
int dfsTimer = 0;
void dfs(int u, int parent){
t0[u] = dfsTimer++;
p[u][0] = parent;
for(int i=1; i<19; ++i) p[u][i] = p[p[u][i-1]][i-1];
for(int v : g[u]) if(v != parent) dfs(v, u);
t1[u] = dfsTimer++;
}
bool isAncestor(int u, int v){
return t0[u]<=t0[v] and t1[v]<=t1[u];
}
int lca(int u, int v){
if(isAncestor(u, v)) return u;
if(isAncestor(v, u)) return v;
for(int i=18; i>=0; --i)
while(u and !isAncestor(p[u][i], v)) u = p[u][i];
return p[u][0];
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n, m, q, x, y, type; cin >> n >> m >> q;
g.resize(n), p.resize(n), t0.resize(n), t1.resize(n);
for(int i=1; i<n; ++i){
cin >> x >> y;
--x, --y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(0, 0);
// for(int i=0; i<n; ++i)
// for(int j=i; j<n; ++j)
// cout << i+1 sp j+1 sp lca(i, j)+1 nl;
vector<int> a(m);
vector<set<int>> pairs(n), pos(n);
for(int i=0; i<m; ++i){
cin >> a[i]; --a[i];
pos[a[i]].insert(i);
if(i) pairs[lca(a[i], a[i-1])].insert(i-1);
}
while(q--){
cin >> type >> x >> y;
--x, --y;
if(type==1){
if(x+1<m){
pairs[lca(a[x], a[x+1])].erase(x);
pairs[lca(y, a[x+1])].insert(x);
}
if(x){
pairs[lca(a[x], a[x-1])].erase(x-1);
pairs[lca(y, a[x-1])].insert(x-1);
}
pos[a[x]].erase(x);
pos[y].insert(x);
a[x] = y;
}else{
int v; cin >> v; --v;
auto f = pos[v].lower_bound(x);
if(f!=pos[v].end() and *f<=y){
cout << *f+1 sp *f+1 nl;
continue;
}
f = pairs[v].lower_bound(x);
if(f!=pairs[v].end() and *f<y) cout << *f+1 sp *f+2 nl;
else cout << -1 sp -1 nl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
2 ms |
364 KB |
n=100 |
3 |
Correct |
1 ms |
364 KB |
n=100 |
4 |
Correct |
1 ms |
364 KB |
n=100 |
5 |
Correct |
1 ms |
364 KB |
n=100 |
6 |
Correct |
1 ms |
364 KB |
n=100 |
7 |
Correct |
1 ms |
364 KB |
n=100 |
8 |
Correct |
1 ms |
364 KB |
n=100 |
9 |
Correct |
1 ms |
364 KB |
n=100 |
10 |
Correct |
1 ms |
364 KB |
n=100 |
11 |
Correct |
2 ms |
364 KB |
n=100 |
12 |
Correct |
1 ms |
364 KB |
n=100 |
13 |
Correct |
1 ms |
364 KB |
n=100 |
14 |
Correct |
1 ms |
364 KB |
n=100 |
15 |
Correct |
1 ms |
364 KB |
n=100 |
16 |
Correct |
2 ms |
364 KB |
n=100 |
17 |
Correct |
1 ms |
364 KB |
n=100 |
18 |
Correct |
1 ms |
364 KB |
n=100 |
19 |
Correct |
1 ms |
364 KB |
n=100 |
20 |
Correct |
1 ms |
364 KB |
n=100 |
21 |
Correct |
1 ms |
364 KB |
n=100 |
22 |
Correct |
1 ms |
364 KB |
n=100 |
23 |
Correct |
1 ms |
492 KB |
n=100 |
24 |
Correct |
1 ms |
364 KB |
n=100 |
25 |
Correct |
2 ms |
364 KB |
n=100 |
26 |
Correct |
1 ms |
364 KB |
n=12 |
27 |
Correct |
1 ms |
364 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
2 ms |
364 KB |
n=100 |
3 |
Correct |
1 ms |
364 KB |
n=100 |
4 |
Correct |
1 ms |
364 KB |
n=100 |
5 |
Correct |
1 ms |
364 KB |
n=100 |
6 |
Correct |
1 ms |
364 KB |
n=100 |
7 |
Correct |
1 ms |
364 KB |
n=100 |
8 |
Correct |
1 ms |
364 KB |
n=100 |
9 |
Correct |
1 ms |
364 KB |
n=100 |
10 |
Correct |
1 ms |
364 KB |
n=100 |
11 |
Correct |
2 ms |
364 KB |
n=100 |
12 |
Correct |
1 ms |
364 KB |
n=100 |
13 |
Correct |
1 ms |
364 KB |
n=100 |
14 |
Correct |
1 ms |
364 KB |
n=100 |
15 |
Correct |
1 ms |
364 KB |
n=100 |
16 |
Correct |
2 ms |
364 KB |
n=100 |
17 |
Correct |
1 ms |
364 KB |
n=100 |
18 |
Correct |
1 ms |
364 KB |
n=100 |
19 |
Correct |
1 ms |
364 KB |
n=100 |
20 |
Correct |
1 ms |
364 KB |
n=100 |
21 |
Correct |
1 ms |
364 KB |
n=100 |
22 |
Correct |
1 ms |
364 KB |
n=100 |
23 |
Correct |
1 ms |
492 KB |
n=100 |
24 |
Correct |
1 ms |
364 KB |
n=100 |
25 |
Correct |
2 ms |
364 KB |
n=100 |
26 |
Correct |
1 ms |
364 KB |
n=12 |
27 |
Correct |
1 ms |
364 KB |
n=100 |
28 |
Correct |
2 ms |
620 KB |
n=500 |
29 |
Correct |
2 ms |
620 KB |
n=500 |
30 |
Correct |
2 ms |
620 KB |
n=500 |
31 |
Correct |
2 ms |
620 KB |
n=500 |
32 |
Correct |
2 ms |
620 KB |
n=500 |
33 |
Correct |
2 ms |
620 KB |
n=500 |
34 |
Correct |
2 ms |
620 KB |
n=500 |
35 |
Correct |
2 ms |
620 KB |
n=500 |
36 |
Correct |
2 ms |
620 KB |
n=500 |
37 |
Correct |
2 ms |
640 KB |
n=500 |
38 |
Correct |
2 ms |
620 KB |
n=500 |
39 |
Correct |
2 ms |
620 KB |
n=500 |
40 |
Correct |
2 ms |
640 KB |
n=500 |
41 |
Correct |
2 ms |
620 KB |
n=500 |
42 |
Correct |
2 ms |
620 KB |
n=500 |
43 |
Correct |
2 ms |
620 KB |
n=500 |
44 |
Correct |
2 ms |
620 KB |
n=500 |
45 |
Correct |
2 ms |
620 KB |
n=500 |
46 |
Correct |
2 ms |
620 KB |
n=500 |
47 |
Correct |
2 ms |
620 KB |
n=500 |
48 |
Correct |
2 ms |
620 KB |
n=500 |
49 |
Correct |
2 ms |
620 KB |
n=500 |
50 |
Correct |
2 ms |
620 KB |
n=500 |
51 |
Correct |
2 ms |
620 KB |
n=500 |
52 |
Correct |
2 ms |
620 KB |
n=500 |
53 |
Correct |
2 ms |
620 KB |
n=500 |
54 |
Correct |
2 ms |
620 KB |
n=500 |
55 |
Correct |
2 ms |
512 KB |
n=278 |
56 |
Correct |
3 ms |
620 KB |
n=500 |
57 |
Correct |
2 ms |
620 KB |
n=500 |
58 |
Correct |
2 ms |
620 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
2 ms |
364 KB |
n=100 |
3 |
Correct |
1 ms |
364 KB |
n=100 |
4 |
Correct |
1 ms |
364 KB |
n=100 |
5 |
Correct |
1 ms |
364 KB |
n=100 |
6 |
Correct |
1 ms |
364 KB |
n=100 |
7 |
Correct |
1 ms |
364 KB |
n=100 |
8 |
Correct |
1 ms |
364 KB |
n=100 |
9 |
Correct |
1 ms |
364 KB |
n=100 |
10 |
Correct |
1 ms |
364 KB |
n=100 |
11 |
Correct |
2 ms |
364 KB |
n=100 |
12 |
Correct |
1 ms |
364 KB |
n=100 |
13 |
Correct |
1 ms |
364 KB |
n=100 |
14 |
Correct |
1 ms |
364 KB |
n=100 |
15 |
Correct |
1 ms |
364 KB |
n=100 |
16 |
Correct |
2 ms |
364 KB |
n=100 |
17 |
Correct |
1 ms |
364 KB |
n=100 |
18 |
Correct |
1 ms |
364 KB |
n=100 |
19 |
Correct |
1 ms |
364 KB |
n=100 |
20 |
Correct |
1 ms |
364 KB |
n=100 |
21 |
Correct |
1 ms |
364 KB |
n=100 |
22 |
Correct |
1 ms |
364 KB |
n=100 |
23 |
Correct |
1 ms |
492 KB |
n=100 |
24 |
Correct |
1 ms |
364 KB |
n=100 |
25 |
Correct |
2 ms |
364 KB |
n=100 |
26 |
Correct |
1 ms |
364 KB |
n=12 |
27 |
Correct |
1 ms |
364 KB |
n=100 |
28 |
Correct |
2 ms |
620 KB |
n=500 |
29 |
Correct |
2 ms |
620 KB |
n=500 |
30 |
Correct |
2 ms |
620 KB |
n=500 |
31 |
Correct |
2 ms |
620 KB |
n=500 |
32 |
Correct |
2 ms |
620 KB |
n=500 |
33 |
Correct |
2 ms |
620 KB |
n=500 |
34 |
Correct |
2 ms |
620 KB |
n=500 |
35 |
Correct |
2 ms |
620 KB |
n=500 |
36 |
Correct |
2 ms |
620 KB |
n=500 |
37 |
Correct |
2 ms |
640 KB |
n=500 |
38 |
Correct |
2 ms |
620 KB |
n=500 |
39 |
Correct |
2 ms |
620 KB |
n=500 |
40 |
Correct |
2 ms |
640 KB |
n=500 |
41 |
Correct |
2 ms |
620 KB |
n=500 |
42 |
Correct |
2 ms |
620 KB |
n=500 |
43 |
Correct |
2 ms |
620 KB |
n=500 |
44 |
Correct |
2 ms |
620 KB |
n=500 |
45 |
Correct |
2 ms |
620 KB |
n=500 |
46 |
Correct |
2 ms |
620 KB |
n=500 |
47 |
Correct |
2 ms |
620 KB |
n=500 |
48 |
Correct |
2 ms |
620 KB |
n=500 |
49 |
Correct |
2 ms |
620 KB |
n=500 |
50 |
Correct |
2 ms |
620 KB |
n=500 |
51 |
Correct |
2 ms |
620 KB |
n=500 |
52 |
Correct |
2 ms |
620 KB |
n=500 |
53 |
Correct |
2 ms |
620 KB |
n=500 |
54 |
Correct |
2 ms |
620 KB |
n=500 |
55 |
Correct |
2 ms |
512 KB |
n=278 |
56 |
Correct |
3 ms |
620 KB |
n=500 |
57 |
Correct |
2 ms |
620 KB |
n=500 |
58 |
Correct |
2 ms |
620 KB |
n=500 |
59 |
Correct |
5 ms |
1260 KB |
n=2000 |
60 |
Correct |
4 ms |
1260 KB |
n=2000 |
61 |
Correct |
5 ms |
1260 KB |
n=2000 |
62 |
Correct |
6 ms |
1260 KB |
n=2000 |
63 |
Correct |
6 ms |
1260 KB |
n=2000 |
64 |
Correct |
5 ms |
1260 KB |
n=2000 |
65 |
Correct |
5 ms |
1260 KB |
n=2000 |
66 |
Correct |
4 ms |
1260 KB |
n=2000 |
67 |
Correct |
5 ms |
1260 KB |
n=2000 |
68 |
Correct |
4 ms |
1260 KB |
n=2000 |
69 |
Correct |
4 ms |
1260 KB |
n=2000 |
70 |
Correct |
4 ms |
1260 KB |
n=2000 |
71 |
Correct |
4 ms |
1260 KB |
n=2000 |
72 |
Correct |
4 ms |
1260 KB |
n=2000 |
73 |
Correct |
4 ms |
1260 KB |
n=2000 |
74 |
Correct |
4 ms |
1132 KB |
n=1844 |
75 |
Correct |
4 ms |
1260 KB |
n=2000 |
76 |
Correct |
5 ms |
1260 KB |
n=2000 |
77 |
Correct |
5 ms |
1260 KB |
n=2000 |
78 |
Correct |
5 ms |
1260 KB |
n=2000 |
79 |
Correct |
5 ms |
1260 KB |
n=2000 |
80 |
Correct |
5 ms |
1260 KB |
n=2000 |
81 |
Correct |
5 ms |
1260 KB |
n=2000 |
82 |
Correct |
5 ms |
1260 KB |
n=2000 |
83 |
Correct |
5 ms |
1260 KB |
n=2000 |
84 |
Correct |
5 ms |
1260 KB |
n=2000 |
85 |
Correct |
5 ms |
1260 KB |
n=2000 |
86 |
Correct |
5 ms |
1260 KB |
n=2000 |
87 |
Correct |
5 ms |
1260 KB |
n=2000 |
88 |
Correct |
4 ms |
1260 KB |
n=2000 |
89 |
Correct |
4 ms |
1260 KB |
n=2000 |
90 |
Correct |
4 ms |
1260 KB |
n=2000 |
91 |
Correct |
4 ms |
1260 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=5 |
2 |
Correct |
2 ms |
364 KB |
n=100 |
3 |
Correct |
1 ms |
364 KB |
n=100 |
4 |
Correct |
1 ms |
364 KB |
n=100 |
5 |
Correct |
1 ms |
364 KB |
n=100 |
6 |
Correct |
1 ms |
364 KB |
n=100 |
7 |
Correct |
1 ms |
364 KB |
n=100 |
8 |
Correct |
1 ms |
364 KB |
n=100 |
9 |
Correct |
1 ms |
364 KB |
n=100 |
10 |
Correct |
1 ms |
364 KB |
n=100 |
11 |
Correct |
2 ms |
364 KB |
n=100 |
12 |
Correct |
1 ms |
364 KB |
n=100 |
13 |
Correct |
1 ms |
364 KB |
n=100 |
14 |
Correct |
1 ms |
364 KB |
n=100 |
15 |
Correct |
1 ms |
364 KB |
n=100 |
16 |
Correct |
2 ms |
364 KB |
n=100 |
17 |
Correct |
1 ms |
364 KB |
n=100 |
18 |
Correct |
1 ms |
364 KB |
n=100 |
19 |
Correct |
1 ms |
364 KB |
n=100 |
20 |
Correct |
1 ms |
364 KB |
n=100 |
21 |
Correct |
1 ms |
364 KB |
n=100 |
22 |
Correct |
1 ms |
364 KB |
n=100 |
23 |
Correct |
1 ms |
492 KB |
n=100 |
24 |
Correct |
1 ms |
364 KB |
n=100 |
25 |
Correct |
2 ms |
364 KB |
n=100 |
26 |
Correct |
1 ms |
364 KB |
n=12 |
27 |
Correct |
1 ms |
364 KB |
n=100 |
28 |
Correct |
2 ms |
620 KB |
n=500 |
29 |
Correct |
2 ms |
620 KB |
n=500 |
30 |
Correct |
2 ms |
620 KB |
n=500 |
31 |
Correct |
2 ms |
620 KB |
n=500 |
32 |
Correct |
2 ms |
620 KB |
n=500 |
33 |
Correct |
2 ms |
620 KB |
n=500 |
34 |
Correct |
2 ms |
620 KB |
n=500 |
35 |
Correct |
2 ms |
620 KB |
n=500 |
36 |
Correct |
2 ms |
620 KB |
n=500 |
37 |
Correct |
2 ms |
640 KB |
n=500 |
38 |
Correct |
2 ms |
620 KB |
n=500 |
39 |
Correct |
2 ms |
620 KB |
n=500 |
40 |
Correct |
2 ms |
640 KB |
n=500 |
41 |
Correct |
2 ms |
620 KB |
n=500 |
42 |
Correct |
2 ms |
620 KB |
n=500 |
43 |
Correct |
2 ms |
620 KB |
n=500 |
44 |
Correct |
2 ms |
620 KB |
n=500 |
45 |
Correct |
2 ms |
620 KB |
n=500 |
46 |
Correct |
2 ms |
620 KB |
n=500 |
47 |
Correct |
2 ms |
620 KB |
n=500 |
48 |
Correct |
2 ms |
620 KB |
n=500 |
49 |
Correct |
2 ms |
620 KB |
n=500 |
50 |
Correct |
2 ms |
620 KB |
n=500 |
51 |
Correct |
2 ms |
620 KB |
n=500 |
52 |
Correct |
2 ms |
620 KB |
n=500 |
53 |
Correct |
2 ms |
620 KB |
n=500 |
54 |
Correct |
2 ms |
620 KB |
n=500 |
55 |
Correct |
2 ms |
512 KB |
n=278 |
56 |
Correct |
3 ms |
620 KB |
n=500 |
57 |
Correct |
2 ms |
620 KB |
n=500 |
58 |
Correct |
2 ms |
620 KB |
n=500 |
59 |
Correct |
5 ms |
1260 KB |
n=2000 |
60 |
Correct |
4 ms |
1260 KB |
n=2000 |
61 |
Correct |
5 ms |
1260 KB |
n=2000 |
62 |
Correct |
6 ms |
1260 KB |
n=2000 |
63 |
Correct |
6 ms |
1260 KB |
n=2000 |
64 |
Correct |
5 ms |
1260 KB |
n=2000 |
65 |
Correct |
5 ms |
1260 KB |
n=2000 |
66 |
Correct |
4 ms |
1260 KB |
n=2000 |
67 |
Correct |
5 ms |
1260 KB |
n=2000 |
68 |
Correct |
4 ms |
1260 KB |
n=2000 |
69 |
Correct |
4 ms |
1260 KB |
n=2000 |
70 |
Correct |
4 ms |
1260 KB |
n=2000 |
71 |
Correct |
4 ms |
1260 KB |
n=2000 |
72 |
Correct |
4 ms |
1260 KB |
n=2000 |
73 |
Correct |
4 ms |
1260 KB |
n=2000 |
74 |
Correct |
4 ms |
1132 KB |
n=1844 |
75 |
Correct |
4 ms |
1260 KB |
n=2000 |
76 |
Correct |
5 ms |
1260 KB |
n=2000 |
77 |
Correct |
5 ms |
1260 KB |
n=2000 |
78 |
Correct |
5 ms |
1260 KB |
n=2000 |
79 |
Correct |
5 ms |
1260 KB |
n=2000 |
80 |
Correct |
5 ms |
1260 KB |
n=2000 |
81 |
Correct |
5 ms |
1260 KB |
n=2000 |
82 |
Correct |
5 ms |
1260 KB |
n=2000 |
83 |
Correct |
5 ms |
1260 KB |
n=2000 |
84 |
Correct |
5 ms |
1260 KB |
n=2000 |
85 |
Correct |
5 ms |
1260 KB |
n=2000 |
86 |
Correct |
5 ms |
1260 KB |
n=2000 |
87 |
Correct |
5 ms |
1260 KB |
n=2000 |
88 |
Correct |
4 ms |
1260 KB |
n=2000 |
89 |
Correct |
4 ms |
1260 KB |
n=2000 |
90 |
Correct |
4 ms |
1260 KB |
n=2000 |
91 |
Correct |
4 ms |
1260 KB |
n=2000 |
92 |
Correct |
818 ms |
92648 KB |
n=200000 |
93 |
Correct |
903 ms |
97668 KB |
n=200000 |
94 |
Correct |
714 ms |
100536 KB |
n=200000 |
95 |
Correct |
772 ms |
92508 KB |
n=200000 |
96 |
Correct |
763 ms |
92508 KB |
n=200000 |
97 |
Correct |
997 ms |
96748 KB |
n=200000 |
98 |
Correct |
840 ms |
92764 KB |
n=200000 |
99 |
Correct |
978 ms |
93216 KB |
n=200000 |
100 |
Correct |
842 ms |
92572 KB |
n=200000 |
101 |
Correct |
618 ms |
101484 KB |
n=200000 |
102 |
Correct |
525 ms |
93916 KB |
n=200000 |
103 |
Correct |
507 ms |
93932 KB |
n=200000 |
104 |
Correct |
529 ms |
94060 KB |
n=200000 |
105 |
Correct |
523 ms |
94316 KB |
n=200000 |
106 |
Correct |
523 ms |
94316 KB |
n=200000 |
107 |
Correct |
525 ms |
94212 KB |
n=200000 |
108 |
Correct |
910 ms |
92780 KB |
n=200000 |
109 |
Correct |
943 ms |
92908 KB |
n=200000 |
110 |
Correct |
860 ms |
92844 KB |
n=200000 |
111 |
Correct |
801 ms |
91868 KB |
n=200000 |
112 |
Correct |
688 ms |
100716 KB |
n=200000 |
113 |
Correct |
970 ms |
96876 KB |
n=200000 |
114 |
Correct |
783 ms |
91996 KB |
n=200000 |
115 |
Correct |
1166 ms |
94700 KB |
n=200000 |
116 |
Correct |
746 ms |
92636 KB |
n=200000 |
117 |
Correct |
677 ms |
100844 KB |
n=200000 |
118 |
Correct |
1031 ms |
95596 KB |
n=200000 |
119 |
Correct |
739 ms |
92776 KB |
n=200000 |
120 |
Correct |
583 ms |
100460 KB |
n=200000 |
121 |
Correct |
573 ms |
100460 KB |
n=200000 |
122 |
Correct |
605 ms |
100588 KB |
n=200000 |
123 |
Correct |
539 ms |
94240 KB |
n=200000 |
124 |
Correct |
190 ms |
20716 KB |
n=25264 |