#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define LOG 20
vector <int> g[MAXN];
int a[MAXN];
int in[MAXN];
int out[MAXN];
int rmq[MAXN][LOG];
int T=0;
void dfs(int v,int p)
{
in[v]=++T;
rmq[v][0]=p;
for(auto x:g[v])
{
if(x==p)continue;
dfs(x,v);
}
out[v]=T;
}
int insubtree(int u,int v)
{
return (in[u]<=in[v]&&out[u]>=in[v]);
}
int lca(int u,int v)
{
if(insubtree(u,v))return u;
if(insubtree(v,u))return v;
for(int j = LOG-1; j >= 0; j--)
{
if(!insubtree(rmq[u][j],v))u=rmq[u][j];
}
return rmq[u][0];
}
set <int> st[MAXN];
set <int> stz[MAXN];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m,q;
cin >> n >> m >> q;
for(int i = 0; i < n-1; i++)
{
int u,v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1,0);
rmq[1][0]=1;
for(int d = 1; d < LOG; d++)
{
for(int i = 1; i <= n; i++)rmq[i][d]=rmq[rmq[i][d-1]][d-1];
}
for(int i = 1; i <= m; i++)cin >> a[i];
for(int i = 1; i <= m-1; i++)
{
st[lca(a[i],a[i+1])].insert(i);
}
for(int i = 1; i <= m; i++)
{
stz[a[i]].insert(i);
}
while(q--)
{
int t;
cin >> t;
if(t==1)
{
int idx,val;
cin >> idx >> val;
if(idx!=m)
{
st[lca(a[idx],a[idx+1])].erase(idx);
st[lca(val,a[idx+1])].insert(idx);
}
if(idx != 1)
{
st[lca(a[idx-1],a[idx])].erase(idx-1);
st[lca(a[idx-1],val)].insert(idx-1);
}
stz[a[idx]].erase(idx);
stz[val].insert(idx);
a[idx]=val;
}
else
{
int l,r,v;
cin >> l >> r >> v;
auto it = st[v].lower_bound(l);
auto itt = stz[v].lower_bound(l);
if(itt!=stz[v].end() && *itt >= l && *itt <= r)
{
cout << *itt << " " << *itt << "\n";
continue;
}
if(it==st[v].end())
{
cout << "-1 -1\n";
continue;
}
if(*it <= r && *it >= l && (*it+1)<=r)
{
cout << *it << " " << *it+1 << "\n";
}
else
{
cout << "-1 -1\n";
}
}
}
return 0;
}
/*
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23928 KB |
n=5 |
2 |
Correct |
20 ms |
24044 KB |
n=100 |
3 |
Correct |
24 ms |
24156 KB |
n=100 |
4 |
Correct |
21 ms |
24268 KB |
n=100 |
5 |
Correct |
21 ms |
24308 KB |
n=100 |
6 |
Correct |
21 ms |
24360 KB |
n=100 |
7 |
Correct |
21 ms |
24404 KB |
n=100 |
8 |
Correct |
21 ms |
24404 KB |
n=100 |
9 |
Correct |
21 ms |
24404 KB |
n=100 |
10 |
Correct |
20 ms |
24404 KB |
n=100 |
11 |
Correct |
20 ms |
24404 KB |
n=100 |
12 |
Correct |
20 ms |
24404 KB |
n=100 |
13 |
Correct |
20 ms |
24404 KB |
n=100 |
14 |
Correct |
20 ms |
24404 KB |
n=100 |
15 |
Correct |
19 ms |
24404 KB |
n=100 |
16 |
Correct |
24 ms |
24404 KB |
n=100 |
17 |
Correct |
20 ms |
24404 KB |
n=100 |
18 |
Correct |
21 ms |
24404 KB |
n=100 |
19 |
Correct |
20 ms |
24404 KB |
n=100 |
20 |
Correct |
21 ms |
24476 KB |
n=100 |
21 |
Correct |
20 ms |
24476 KB |
n=100 |
22 |
Correct |
20 ms |
24476 KB |
n=100 |
23 |
Correct |
22 ms |
24476 KB |
n=100 |
24 |
Correct |
20 ms |
24476 KB |
n=100 |
25 |
Correct |
20 ms |
24476 KB |
n=100 |
26 |
Correct |
20 ms |
24476 KB |
n=12 |
27 |
Correct |
21 ms |
24476 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23928 KB |
n=5 |
2 |
Correct |
20 ms |
24044 KB |
n=100 |
3 |
Correct |
24 ms |
24156 KB |
n=100 |
4 |
Correct |
21 ms |
24268 KB |
n=100 |
5 |
Correct |
21 ms |
24308 KB |
n=100 |
6 |
Correct |
21 ms |
24360 KB |
n=100 |
7 |
Correct |
21 ms |
24404 KB |
n=100 |
8 |
Correct |
21 ms |
24404 KB |
n=100 |
9 |
Correct |
21 ms |
24404 KB |
n=100 |
10 |
Correct |
20 ms |
24404 KB |
n=100 |
11 |
Correct |
20 ms |
24404 KB |
n=100 |
12 |
Correct |
20 ms |
24404 KB |
n=100 |
13 |
Correct |
20 ms |
24404 KB |
n=100 |
14 |
Correct |
20 ms |
24404 KB |
n=100 |
15 |
Correct |
19 ms |
24404 KB |
n=100 |
16 |
Correct |
24 ms |
24404 KB |
n=100 |
17 |
Correct |
20 ms |
24404 KB |
n=100 |
18 |
Correct |
21 ms |
24404 KB |
n=100 |
19 |
Correct |
20 ms |
24404 KB |
n=100 |
20 |
Correct |
21 ms |
24476 KB |
n=100 |
21 |
Correct |
20 ms |
24476 KB |
n=100 |
22 |
Correct |
20 ms |
24476 KB |
n=100 |
23 |
Correct |
22 ms |
24476 KB |
n=100 |
24 |
Correct |
20 ms |
24476 KB |
n=100 |
25 |
Correct |
20 ms |
24476 KB |
n=100 |
26 |
Correct |
20 ms |
24476 KB |
n=12 |
27 |
Correct |
21 ms |
24476 KB |
n=100 |
28 |
Correct |
22 ms |
24568 KB |
n=500 |
29 |
Correct |
21 ms |
24692 KB |
n=500 |
30 |
Correct |
21 ms |
24692 KB |
n=500 |
31 |
Correct |
21 ms |
24692 KB |
n=500 |
32 |
Correct |
21 ms |
24692 KB |
n=500 |
33 |
Correct |
21 ms |
24692 KB |
n=500 |
34 |
Correct |
22 ms |
24692 KB |
n=500 |
35 |
Correct |
21 ms |
24692 KB |
n=500 |
36 |
Correct |
20 ms |
24692 KB |
n=500 |
37 |
Correct |
21 ms |
24692 KB |
n=500 |
38 |
Correct |
21 ms |
24696 KB |
n=500 |
39 |
Correct |
22 ms |
24728 KB |
n=500 |
40 |
Correct |
20 ms |
24728 KB |
n=500 |
41 |
Correct |
21 ms |
24756 KB |
n=500 |
42 |
Correct |
22 ms |
24772 KB |
n=500 |
43 |
Correct |
21 ms |
24784 KB |
n=500 |
44 |
Correct |
21 ms |
24796 KB |
n=500 |
45 |
Correct |
22 ms |
24864 KB |
n=500 |
46 |
Correct |
21 ms |
24864 KB |
n=500 |
47 |
Correct |
21 ms |
24864 KB |
n=500 |
48 |
Correct |
21 ms |
24864 KB |
n=500 |
49 |
Correct |
20 ms |
24864 KB |
n=500 |
50 |
Correct |
21 ms |
24880 KB |
n=500 |
51 |
Correct |
21 ms |
24976 KB |
n=500 |
52 |
Correct |
21 ms |
24976 KB |
n=500 |
53 |
Correct |
21 ms |
24976 KB |
n=500 |
54 |
Correct |
21 ms |
24976 KB |
n=500 |
55 |
Correct |
20 ms |
24976 KB |
n=278 |
56 |
Correct |
23 ms |
24976 KB |
n=500 |
57 |
Correct |
22 ms |
24976 KB |
n=500 |
58 |
Correct |
21 ms |
24984 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23928 KB |
n=5 |
2 |
Correct |
20 ms |
24044 KB |
n=100 |
3 |
Correct |
24 ms |
24156 KB |
n=100 |
4 |
Correct |
21 ms |
24268 KB |
n=100 |
5 |
Correct |
21 ms |
24308 KB |
n=100 |
6 |
Correct |
21 ms |
24360 KB |
n=100 |
7 |
Correct |
21 ms |
24404 KB |
n=100 |
8 |
Correct |
21 ms |
24404 KB |
n=100 |
9 |
Correct |
21 ms |
24404 KB |
n=100 |
10 |
Correct |
20 ms |
24404 KB |
n=100 |
11 |
Correct |
20 ms |
24404 KB |
n=100 |
12 |
Correct |
20 ms |
24404 KB |
n=100 |
13 |
Correct |
20 ms |
24404 KB |
n=100 |
14 |
Correct |
20 ms |
24404 KB |
n=100 |
15 |
Correct |
19 ms |
24404 KB |
n=100 |
16 |
Correct |
24 ms |
24404 KB |
n=100 |
17 |
Correct |
20 ms |
24404 KB |
n=100 |
18 |
Correct |
21 ms |
24404 KB |
n=100 |
19 |
Correct |
20 ms |
24404 KB |
n=100 |
20 |
Correct |
21 ms |
24476 KB |
n=100 |
21 |
Correct |
20 ms |
24476 KB |
n=100 |
22 |
Correct |
20 ms |
24476 KB |
n=100 |
23 |
Correct |
22 ms |
24476 KB |
n=100 |
24 |
Correct |
20 ms |
24476 KB |
n=100 |
25 |
Correct |
20 ms |
24476 KB |
n=100 |
26 |
Correct |
20 ms |
24476 KB |
n=12 |
27 |
Correct |
21 ms |
24476 KB |
n=100 |
28 |
Correct |
22 ms |
24568 KB |
n=500 |
29 |
Correct |
21 ms |
24692 KB |
n=500 |
30 |
Correct |
21 ms |
24692 KB |
n=500 |
31 |
Correct |
21 ms |
24692 KB |
n=500 |
32 |
Correct |
21 ms |
24692 KB |
n=500 |
33 |
Correct |
21 ms |
24692 KB |
n=500 |
34 |
Correct |
22 ms |
24692 KB |
n=500 |
35 |
Correct |
21 ms |
24692 KB |
n=500 |
36 |
Correct |
20 ms |
24692 KB |
n=500 |
37 |
Correct |
21 ms |
24692 KB |
n=500 |
38 |
Correct |
21 ms |
24696 KB |
n=500 |
39 |
Correct |
22 ms |
24728 KB |
n=500 |
40 |
Correct |
20 ms |
24728 KB |
n=500 |
41 |
Correct |
21 ms |
24756 KB |
n=500 |
42 |
Correct |
22 ms |
24772 KB |
n=500 |
43 |
Correct |
21 ms |
24784 KB |
n=500 |
44 |
Correct |
21 ms |
24796 KB |
n=500 |
45 |
Correct |
22 ms |
24864 KB |
n=500 |
46 |
Correct |
21 ms |
24864 KB |
n=500 |
47 |
Correct |
21 ms |
24864 KB |
n=500 |
48 |
Correct |
21 ms |
24864 KB |
n=500 |
49 |
Correct |
20 ms |
24864 KB |
n=500 |
50 |
Correct |
21 ms |
24880 KB |
n=500 |
51 |
Correct |
21 ms |
24976 KB |
n=500 |
52 |
Correct |
21 ms |
24976 KB |
n=500 |
53 |
Correct |
21 ms |
24976 KB |
n=500 |
54 |
Correct |
21 ms |
24976 KB |
n=500 |
55 |
Correct |
20 ms |
24976 KB |
n=278 |
56 |
Correct |
23 ms |
24976 KB |
n=500 |
57 |
Correct |
22 ms |
24976 KB |
n=500 |
58 |
Correct |
21 ms |
24984 KB |
n=500 |
59 |
Correct |
25 ms |
25384 KB |
n=2000 |
60 |
Correct |
23 ms |
25580 KB |
n=2000 |
61 |
Correct |
24 ms |
25620 KB |
n=2000 |
62 |
Correct |
25 ms |
25620 KB |
n=2000 |
63 |
Correct |
25 ms |
25620 KB |
n=2000 |
64 |
Correct |
24 ms |
25660 KB |
n=2000 |
65 |
Correct |
25 ms |
25720 KB |
n=2000 |
66 |
Correct |
25 ms |
25896 KB |
n=2000 |
67 |
Correct |
26 ms |
25896 KB |
n=2000 |
68 |
Correct |
25 ms |
25896 KB |
n=2000 |
69 |
Correct |
25 ms |
25944 KB |
n=2000 |
70 |
Correct |
25 ms |
26032 KB |
n=2000 |
71 |
Correct |
23 ms |
26044 KB |
n=2000 |
72 |
Correct |
23 ms |
26096 KB |
n=2000 |
73 |
Correct |
24 ms |
26140 KB |
n=2000 |
74 |
Correct |
25 ms |
26208 KB |
n=1844 |
75 |
Correct |
24 ms |
26328 KB |
n=2000 |
76 |
Correct |
24 ms |
26328 KB |
n=2000 |
77 |
Correct |
32 ms |
26360 KB |
n=2000 |
78 |
Correct |
25 ms |
26412 KB |
n=2000 |
79 |
Correct |
26 ms |
26504 KB |
n=2000 |
80 |
Correct |
24 ms |
26724 KB |
n=2000 |
81 |
Correct |
24 ms |
26724 KB |
n=2000 |
82 |
Correct |
26 ms |
26728 KB |
n=2000 |
83 |
Correct |
25 ms |
26824 KB |
n=2000 |
84 |
Correct |
25 ms |
26824 KB |
n=2000 |
85 |
Correct |
24 ms |
26824 KB |
n=2000 |
86 |
Correct |
25 ms |
26868 KB |
n=2000 |
87 |
Correct |
25 ms |
27004 KB |
n=2000 |
88 |
Correct |
24 ms |
27096 KB |
n=2000 |
89 |
Correct |
24 ms |
27160 KB |
n=2000 |
90 |
Correct |
24 ms |
27216 KB |
n=2000 |
91 |
Correct |
24 ms |
27216 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
23928 KB |
n=5 |
2 |
Correct |
20 ms |
24044 KB |
n=100 |
3 |
Correct |
24 ms |
24156 KB |
n=100 |
4 |
Correct |
21 ms |
24268 KB |
n=100 |
5 |
Correct |
21 ms |
24308 KB |
n=100 |
6 |
Correct |
21 ms |
24360 KB |
n=100 |
7 |
Correct |
21 ms |
24404 KB |
n=100 |
8 |
Correct |
21 ms |
24404 KB |
n=100 |
9 |
Correct |
21 ms |
24404 KB |
n=100 |
10 |
Correct |
20 ms |
24404 KB |
n=100 |
11 |
Correct |
20 ms |
24404 KB |
n=100 |
12 |
Correct |
20 ms |
24404 KB |
n=100 |
13 |
Correct |
20 ms |
24404 KB |
n=100 |
14 |
Correct |
20 ms |
24404 KB |
n=100 |
15 |
Correct |
19 ms |
24404 KB |
n=100 |
16 |
Correct |
24 ms |
24404 KB |
n=100 |
17 |
Correct |
20 ms |
24404 KB |
n=100 |
18 |
Correct |
21 ms |
24404 KB |
n=100 |
19 |
Correct |
20 ms |
24404 KB |
n=100 |
20 |
Correct |
21 ms |
24476 KB |
n=100 |
21 |
Correct |
20 ms |
24476 KB |
n=100 |
22 |
Correct |
20 ms |
24476 KB |
n=100 |
23 |
Correct |
22 ms |
24476 KB |
n=100 |
24 |
Correct |
20 ms |
24476 KB |
n=100 |
25 |
Correct |
20 ms |
24476 KB |
n=100 |
26 |
Correct |
20 ms |
24476 KB |
n=12 |
27 |
Correct |
21 ms |
24476 KB |
n=100 |
28 |
Correct |
22 ms |
24568 KB |
n=500 |
29 |
Correct |
21 ms |
24692 KB |
n=500 |
30 |
Correct |
21 ms |
24692 KB |
n=500 |
31 |
Correct |
21 ms |
24692 KB |
n=500 |
32 |
Correct |
21 ms |
24692 KB |
n=500 |
33 |
Correct |
21 ms |
24692 KB |
n=500 |
34 |
Correct |
22 ms |
24692 KB |
n=500 |
35 |
Correct |
21 ms |
24692 KB |
n=500 |
36 |
Correct |
20 ms |
24692 KB |
n=500 |
37 |
Correct |
21 ms |
24692 KB |
n=500 |
38 |
Correct |
21 ms |
24696 KB |
n=500 |
39 |
Correct |
22 ms |
24728 KB |
n=500 |
40 |
Correct |
20 ms |
24728 KB |
n=500 |
41 |
Correct |
21 ms |
24756 KB |
n=500 |
42 |
Correct |
22 ms |
24772 KB |
n=500 |
43 |
Correct |
21 ms |
24784 KB |
n=500 |
44 |
Correct |
21 ms |
24796 KB |
n=500 |
45 |
Correct |
22 ms |
24864 KB |
n=500 |
46 |
Correct |
21 ms |
24864 KB |
n=500 |
47 |
Correct |
21 ms |
24864 KB |
n=500 |
48 |
Correct |
21 ms |
24864 KB |
n=500 |
49 |
Correct |
20 ms |
24864 KB |
n=500 |
50 |
Correct |
21 ms |
24880 KB |
n=500 |
51 |
Correct |
21 ms |
24976 KB |
n=500 |
52 |
Correct |
21 ms |
24976 KB |
n=500 |
53 |
Correct |
21 ms |
24976 KB |
n=500 |
54 |
Correct |
21 ms |
24976 KB |
n=500 |
55 |
Correct |
20 ms |
24976 KB |
n=278 |
56 |
Correct |
23 ms |
24976 KB |
n=500 |
57 |
Correct |
22 ms |
24976 KB |
n=500 |
58 |
Correct |
21 ms |
24984 KB |
n=500 |
59 |
Correct |
25 ms |
25384 KB |
n=2000 |
60 |
Correct |
23 ms |
25580 KB |
n=2000 |
61 |
Correct |
24 ms |
25620 KB |
n=2000 |
62 |
Correct |
25 ms |
25620 KB |
n=2000 |
63 |
Correct |
25 ms |
25620 KB |
n=2000 |
64 |
Correct |
24 ms |
25660 KB |
n=2000 |
65 |
Correct |
25 ms |
25720 KB |
n=2000 |
66 |
Correct |
25 ms |
25896 KB |
n=2000 |
67 |
Correct |
26 ms |
25896 KB |
n=2000 |
68 |
Correct |
25 ms |
25896 KB |
n=2000 |
69 |
Correct |
25 ms |
25944 KB |
n=2000 |
70 |
Correct |
25 ms |
26032 KB |
n=2000 |
71 |
Correct |
23 ms |
26044 KB |
n=2000 |
72 |
Correct |
23 ms |
26096 KB |
n=2000 |
73 |
Correct |
24 ms |
26140 KB |
n=2000 |
74 |
Correct |
25 ms |
26208 KB |
n=1844 |
75 |
Correct |
24 ms |
26328 KB |
n=2000 |
76 |
Correct |
24 ms |
26328 KB |
n=2000 |
77 |
Correct |
32 ms |
26360 KB |
n=2000 |
78 |
Correct |
25 ms |
26412 KB |
n=2000 |
79 |
Correct |
26 ms |
26504 KB |
n=2000 |
80 |
Correct |
24 ms |
26724 KB |
n=2000 |
81 |
Correct |
24 ms |
26724 KB |
n=2000 |
82 |
Correct |
26 ms |
26728 KB |
n=2000 |
83 |
Correct |
25 ms |
26824 KB |
n=2000 |
84 |
Correct |
25 ms |
26824 KB |
n=2000 |
85 |
Correct |
24 ms |
26824 KB |
n=2000 |
86 |
Correct |
25 ms |
26868 KB |
n=2000 |
87 |
Correct |
25 ms |
27004 KB |
n=2000 |
88 |
Correct |
24 ms |
27096 KB |
n=2000 |
89 |
Correct |
24 ms |
27160 KB |
n=2000 |
90 |
Correct |
24 ms |
27216 KB |
n=2000 |
91 |
Correct |
24 ms |
27216 KB |
n=2000 |
92 |
Correct |
934 ms |
78264 KB |
n=200000 |
93 |
Correct |
1094 ms |
89792 KB |
n=200000 |
94 |
Correct |
746 ms |
100596 KB |
n=200000 |
95 |
Correct |
902 ms |
100596 KB |
n=200000 |
96 |
Correct |
893 ms |
106588 KB |
n=200000 |
97 |
Correct |
1029 ms |
117192 KB |
n=200000 |
98 |
Correct |
934 ms |
120632 KB |
n=200000 |
99 |
Correct |
1014 ms |
127340 KB |
n=200000 |
100 |
Correct |
883 ms |
134412 KB |
n=200000 |
101 |
Correct |
692 ms |
150484 KB |
n=200000 |
102 |
Correct |
526 ms |
150484 KB |
n=200000 |
103 |
Correct |
547 ms |
156280 KB |
n=200000 |
104 |
Correct |
522 ms |
163028 KB |
n=200000 |
105 |
Correct |
532 ms |
170240 KB |
n=200000 |
106 |
Correct |
544 ms |
177296 KB |
n=200000 |
107 |
Correct |
559 ms |
184776 KB |
n=200000 |
108 |
Correct |
1108 ms |
190804 KB |
n=200000 |
109 |
Correct |
1078 ms |
197616 KB |
n=200000 |
110 |
Correct |
1016 ms |
204468 KB |
n=200000 |
111 |
Correct |
1002 ms |
210792 KB |
n=200000 |
112 |
Correct |
806 ms |
225896 KB |
n=200000 |
113 |
Correct |
1094 ms |
228992 KB |
n=200000 |
114 |
Correct |
1008 ms |
232240 KB |
n=200000 |
115 |
Correct |
1326 ms |
240940 KB |
n=200000 |
116 |
Correct |
954 ms |
246512 KB |
n=200000 |
117 |
Correct |
753 ms |
262128 KB |
n=200000 |
118 |
Correct |
1236 ms |
262144 KB |
n=200000 |
119 |
Correct |
905 ms |
262144 KB |
n=200000 |
120 |
Correct |
634 ms |
262144 KB |
n=200000 |
121 |
Correct |
672 ms |
262144 KB |
n=200000 |
122 |
Correct |
620 ms |
262144 KB |
n=200000 |
123 |
Correct |
534 ms |
262144 KB |
n=200000 |
124 |
Correct |
231 ms |
262144 KB |
n=25264 |