#include <iostream>
#include <vector>
#include <set>
using namespace std;
int n,m,q,p[200001][20],t,l,r,k,st[200001],en[200001],id,a[200001],u,v;
vector <int> ke[200001];
set <int> s[200001],s2[200001];
void dfs(int u, int par){
st[u]=++id;
for (int v:ke[u])
if (v!=par){
p[v][0]=u;
dfs(v,u);
}
en[u]=++id;
}
void prep(){
for (int i=1;i<20;i++)
for (int j=1;j<=n;j++)
p[j][i]=p[p[j][i-1]][i-1];
}
bool isancestor(int i, int j){
return (st[i]<=st[j]&&en[i]>=en[j]);
}
int lca(int i, int j){
if (isancestor(i,j))
return i;
if (isancestor(j,i))
return j;
for (int k=19;k>=0;k--)
if (p[i][k]&&!isancestor(p[i][k],j))
i=p[i][k];
return p[i][0];
}
int main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> n >> m >> q;
for (int i=1;i<n;i++){
cin >> u >> v;
ke[u].push_back(v);
ke[v].push_back(u);
}
dfs(1,1);
prep();
for (int i=1;i<=m;i++){
cin >> a[i];
s[a[i]].insert(i);
if (i>1)
s2[lca(a[i-1],a[i])].insert(i-1);
}
while (q--){
cin >> t >> l >> r;
if (t==1){
s[a[l]].erase(l);
if (l<m)
s2[lca(a[l],a[l+1])].erase(l);
if (l>1)
s2[lca(a[l-1],a[l])].erase(l-1);
a[l]=r;
s[a[l]].insert(l);
if (l<m)
s2[lca(a[l],a[l+1])].insert(l);
if (l>1)
s2[lca(a[l-1],a[l])].insert(l-1);
continue;
}
cin >> k;
if (!s[k].empty()&&(*--s[k].end())>=l){
int x=*s[k].lower_bound(l);
if (x<=r){
cout << x << ' ' << x << '\n';
continue;
}
}
if (!s2[k].empty()&&(*--s2[k].end())>=l){
int x=*s2[k].lower_bound(l);
if (x<r){
cout << x << ' ' << x+1 << '\n';
continue;
}
}
cout << "-1 -1\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23744 KB |
n=5 |
2 |
Correct |
15 ms |
23812 KB |
n=100 |
3 |
Correct |
14 ms |
23784 KB |
n=100 |
4 |
Correct |
12 ms |
23840 KB |
n=100 |
5 |
Correct |
12 ms |
23848 KB |
n=100 |
6 |
Correct |
12 ms |
23764 KB |
n=100 |
7 |
Correct |
12 ms |
23764 KB |
n=100 |
8 |
Correct |
12 ms |
23828 KB |
n=100 |
9 |
Correct |
12 ms |
23804 KB |
n=100 |
10 |
Correct |
12 ms |
23736 KB |
n=100 |
11 |
Correct |
15 ms |
23764 KB |
n=100 |
12 |
Correct |
12 ms |
23732 KB |
n=100 |
13 |
Correct |
12 ms |
23764 KB |
n=100 |
14 |
Correct |
11 ms |
23776 KB |
n=100 |
15 |
Correct |
11 ms |
23812 KB |
n=100 |
16 |
Correct |
11 ms |
23840 KB |
n=100 |
17 |
Correct |
12 ms |
23764 KB |
n=100 |
18 |
Correct |
13 ms |
23764 KB |
n=100 |
19 |
Correct |
12 ms |
23764 KB |
n=100 |
20 |
Correct |
16 ms |
23828 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
14 ms |
23764 KB |
n=100 |
23 |
Correct |
13 ms |
23792 KB |
n=100 |
24 |
Correct |
12 ms |
23824 KB |
n=100 |
25 |
Correct |
13 ms |
23732 KB |
n=100 |
26 |
Correct |
14 ms |
23788 KB |
n=12 |
27 |
Correct |
11 ms |
23764 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23744 KB |
n=5 |
2 |
Correct |
15 ms |
23812 KB |
n=100 |
3 |
Correct |
14 ms |
23784 KB |
n=100 |
4 |
Correct |
12 ms |
23840 KB |
n=100 |
5 |
Correct |
12 ms |
23848 KB |
n=100 |
6 |
Correct |
12 ms |
23764 KB |
n=100 |
7 |
Correct |
12 ms |
23764 KB |
n=100 |
8 |
Correct |
12 ms |
23828 KB |
n=100 |
9 |
Correct |
12 ms |
23804 KB |
n=100 |
10 |
Correct |
12 ms |
23736 KB |
n=100 |
11 |
Correct |
15 ms |
23764 KB |
n=100 |
12 |
Correct |
12 ms |
23732 KB |
n=100 |
13 |
Correct |
12 ms |
23764 KB |
n=100 |
14 |
Correct |
11 ms |
23776 KB |
n=100 |
15 |
Correct |
11 ms |
23812 KB |
n=100 |
16 |
Correct |
11 ms |
23840 KB |
n=100 |
17 |
Correct |
12 ms |
23764 KB |
n=100 |
18 |
Correct |
13 ms |
23764 KB |
n=100 |
19 |
Correct |
12 ms |
23764 KB |
n=100 |
20 |
Correct |
16 ms |
23828 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
14 ms |
23764 KB |
n=100 |
23 |
Correct |
13 ms |
23792 KB |
n=100 |
24 |
Correct |
12 ms |
23824 KB |
n=100 |
25 |
Correct |
13 ms |
23732 KB |
n=100 |
26 |
Correct |
14 ms |
23788 KB |
n=12 |
27 |
Correct |
11 ms |
23764 KB |
n=100 |
28 |
Correct |
12 ms |
23892 KB |
n=500 |
29 |
Correct |
13 ms |
23920 KB |
n=500 |
30 |
Correct |
14 ms |
23892 KB |
n=500 |
31 |
Correct |
13 ms |
23892 KB |
n=500 |
32 |
Correct |
13 ms |
23888 KB |
n=500 |
33 |
Correct |
14 ms |
23892 KB |
n=500 |
34 |
Correct |
12 ms |
23832 KB |
n=500 |
35 |
Correct |
15 ms |
23892 KB |
n=500 |
36 |
Correct |
12 ms |
23876 KB |
n=500 |
37 |
Correct |
12 ms |
23932 KB |
n=500 |
38 |
Correct |
12 ms |
23892 KB |
n=500 |
39 |
Correct |
13 ms |
23892 KB |
n=500 |
40 |
Correct |
12 ms |
23892 KB |
n=500 |
41 |
Correct |
12 ms |
23944 KB |
n=500 |
42 |
Correct |
16 ms |
23892 KB |
n=500 |
43 |
Correct |
15 ms |
23888 KB |
n=500 |
44 |
Correct |
13 ms |
23892 KB |
n=500 |
45 |
Correct |
13 ms |
23892 KB |
n=500 |
46 |
Correct |
13 ms |
23952 KB |
n=500 |
47 |
Correct |
13 ms |
23892 KB |
n=500 |
48 |
Correct |
13 ms |
23940 KB |
n=500 |
49 |
Correct |
12 ms |
23892 KB |
n=500 |
50 |
Correct |
15 ms |
23892 KB |
n=500 |
51 |
Correct |
14 ms |
23828 KB |
n=500 |
52 |
Correct |
12 ms |
23892 KB |
n=500 |
53 |
Correct |
12 ms |
23892 KB |
n=500 |
54 |
Correct |
12 ms |
23956 KB |
n=500 |
55 |
Correct |
12 ms |
23904 KB |
n=278 |
56 |
Correct |
13 ms |
23892 KB |
n=500 |
57 |
Correct |
16 ms |
23892 KB |
n=500 |
58 |
Correct |
13 ms |
23892 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23744 KB |
n=5 |
2 |
Correct |
15 ms |
23812 KB |
n=100 |
3 |
Correct |
14 ms |
23784 KB |
n=100 |
4 |
Correct |
12 ms |
23840 KB |
n=100 |
5 |
Correct |
12 ms |
23848 KB |
n=100 |
6 |
Correct |
12 ms |
23764 KB |
n=100 |
7 |
Correct |
12 ms |
23764 KB |
n=100 |
8 |
Correct |
12 ms |
23828 KB |
n=100 |
9 |
Correct |
12 ms |
23804 KB |
n=100 |
10 |
Correct |
12 ms |
23736 KB |
n=100 |
11 |
Correct |
15 ms |
23764 KB |
n=100 |
12 |
Correct |
12 ms |
23732 KB |
n=100 |
13 |
Correct |
12 ms |
23764 KB |
n=100 |
14 |
Correct |
11 ms |
23776 KB |
n=100 |
15 |
Correct |
11 ms |
23812 KB |
n=100 |
16 |
Correct |
11 ms |
23840 KB |
n=100 |
17 |
Correct |
12 ms |
23764 KB |
n=100 |
18 |
Correct |
13 ms |
23764 KB |
n=100 |
19 |
Correct |
12 ms |
23764 KB |
n=100 |
20 |
Correct |
16 ms |
23828 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
14 ms |
23764 KB |
n=100 |
23 |
Correct |
13 ms |
23792 KB |
n=100 |
24 |
Correct |
12 ms |
23824 KB |
n=100 |
25 |
Correct |
13 ms |
23732 KB |
n=100 |
26 |
Correct |
14 ms |
23788 KB |
n=12 |
27 |
Correct |
11 ms |
23764 KB |
n=100 |
28 |
Correct |
12 ms |
23892 KB |
n=500 |
29 |
Correct |
13 ms |
23920 KB |
n=500 |
30 |
Correct |
14 ms |
23892 KB |
n=500 |
31 |
Correct |
13 ms |
23892 KB |
n=500 |
32 |
Correct |
13 ms |
23888 KB |
n=500 |
33 |
Correct |
14 ms |
23892 KB |
n=500 |
34 |
Correct |
12 ms |
23832 KB |
n=500 |
35 |
Correct |
15 ms |
23892 KB |
n=500 |
36 |
Correct |
12 ms |
23876 KB |
n=500 |
37 |
Correct |
12 ms |
23932 KB |
n=500 |
38 |
Correct |
12 ms |
23892 KB |
n=500 |
39 |
Correct |
13 ms |
23892 KB |
n=500 |
40 |
Correct |
12 ms |
23892 KB |
n=500 |
41 |
Correct |
12 ms |
23944 KB |
n=500 |
42 |
Correct |
16 ms |
23892 KB |
n=500 |
43 |
Correct |
15 ms |
23888 KB |
n=500 |
44 |
Correct |
13 ms |
23892 KB |
n=500 |
45 |
Correct |
13 ms |
23892 KB |
n=500 |
46 |
Correct |
13 ms |
23952 KB |
n=500 |
47 |
Correct |
13 ms |
23892 KB |
n=500 |
48 |
Correct |
13 ms |
23940 KB |
n=500 |
49 |
Correct |
12 ms |
23892 KB |
n=500 |
50 |
Correct |
15 ms |
23892 KB |
n=500 |
51 |
Correct |
14 ms |
23828 KB |
n=500 |
52 |
Correct |
12 ms |
23892 KB |
n=500 |
53 |
Correct |
12 ms |
23892 KB |
n=500 |
54 |
Correct |
12 ms |
23956 KB |
n=500 |
55 |
Correct |
12 ms |
23904 KB |
n=278 |
56 |
Correct |
13 ms |
23892 KB |
n=500 |
57 |
Correct |
16 ms |
23892 KB |
n=500 |
58 |
Correct |
13 ms |
23892 KB |
n=500 |
59 |
Correct |
14 ms |
24272 KB |
n=2000 |
60 |
Correct |
13 ms |
24276 KB |
n=2000 |
61 |
Correct |
14 ms |
24332 KB |
n=2000 |
62 |
Correct |
16 ms |
24404 KB |
n=2000 |
63 |
Correct |
17 ms |
24168 KB |
n=2000 |
64 |
Correct |
15 ms |
24276 KB |
n=2000 |
65 |
Correct |
15 ms |
24148 KB |
n=2000 |
66 |
Correct |
14 ms |
24276 KB |
n=2000 |
67 |
Correct |
15 ms |
24216 KB |
n=2000 |
68 |
Correct |
14 ms |
24276 KB |
n=2000 |
69 |
Correct |
17 ms |
24276 KB |
n=2000 |
70 |
Correct |
14 ms |
24276 KB |
n=2000 |
71 |
Correct |
14 ms |
24240 KB |
n=2000 |
72 |
Correct |
13 ms |
24148 KB |
n=2000 |
73 |
Correct |
17 ms |
24256 KB |
n=2000 |
74 |
Correct |
15 ms |
24148 KB |
n=1844 |
75 |
Correct |
14 ms |
24216 KB |
n=2000 |
76 |
Correct |
15 ms |
24220 KB |
n=2000 |
77 |
Correct |
15 ms |
24148 KB |
n=2000 |
78 |
Correct |
15 ms |
24148 KB |
n=2000 |
79 |
Correct |
15 ms |
24276 KB |
n=2000 |
80 |
Correct |
13 ms |
24276 KB |
n=2000 |
81 |
Correct |
14 ms |
24308 KB |
n=2000 |
82 |
Correct |
15 ms |
24204 KB |
n=2000 |
83 |
Correct |
16 ms |
24276 KB |
n=2000 |
84 |
Correct |
16 ms |
24276 KB |
n=2000 |
85 |
Correct |
20 ms |
24280 KB |
n=2000 |
86 |
Correct |
14 ms |
24276 KB |
n=2000 |
87 |
Correct |
14 ms |
24276 KB |
n=2000 |
88 |
Correct |
14 ms |
24276 KB |
n=2000 |
89 |
Correct |
16 ms |
24276 KB |
n=2000 |
90 |
Correct |
14 ms |
24276 KB |
n=2000 |
91 |
Correct |
15 ms |
24220 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23744 KB |
n=5 |
2 |
Correct |
15 ms |
23812 KB |
n=100 |
3 |
Correct |
14 ms |
23784 KB |
n=100 |
4 |
Correct |
12 ms |
23840 KB |
n=100 |
5 |
Correct |
12 ms |
23848 KB |
n=100 |
6 |
Correct |
12 ms |
23764 KB |
n=100 |
7 |
Correct |
12 ms |
23764 KB |
n=100 |
8 |
Correct |
12 ms |
23828 KB |
n=100 |
9 |
Correct |
12 ms |
23804 KB |
n=100 |
10 |
Correct |
12 ms |
23736 KB |
n=100 |
11 |
Correct |
15 ms |
23764 KB |
n=100 |
12 |
Correct |
12 ms |
23732 KB |
n=100 |
13 |
Correct |
12 ms |
23764 KB |
n=100 |
14 |
Correct |
11 ms |
23776 KB |
n=100 |
15 |
Correct |
11 ms |
23812 KB |
n=100 |
16 |
Correct |
11 ms |
23840 KB |
n=100 |
17 |
Correct |
12 ms |
23764 KB |
n=100 |
18 |
Correct |
13 ms |
23764 KB |
n=100 |
19 |
Correct |
12 ms |
23764 KB |
n=100 |
20 |
Correct |
16 ms |
23828 KB |
n=100 |
21 |
Correct |
12 ms |
23764 KB |
n=100 |
22 |
Correct |
14 ms |
23764 KB |
n=100 |
23 |
Correct |
13 ms |
23792 KB |
n=100 |
24 |
Correct |
12 ms |
23824 KB |
n=100 |
25 |
Correct |
13 ms |
23732 KB |
n=100 |
26 |
Correct |
14 ms |
23788 KB |
n=12 |
27 |
Correct |
11 ms |
23764 KB |
n=100 |
28 |
Correct |
12 ms |
23892 KB |
n=500 |
29 |
Correct |
13 ms |
23920 KB |
n=500 |
30 |
Correct |
14 ms |
23892 KB |
n=500 |
31 |
Correct |
13 ms |
23892 KB |
n=500 |
32 |
Correct |
13 ms |
23888 KB |
n=500 |
33 |
Correct |
14 ms |
23892 KB |
n=500 |
34 |
Correct |
12 ms |
23832 KB |
n=500 |
35 |
Correct |
15 ms |
23892 KB |
n=500 |
36 |
Correct |
12 ms |
23876 KB |
n=500 |
37 |
Correct |
12 ms |
23932 KB |
n=500 |
38 |
Correct |
12 ms |
23892 KB |
n=500 |
39 |
Correct |
13 ms |
23892 KB |
n=500 |
40 |
Correct |
12 ms |
23892 KB |
n=500 |
41 |
Correct |
12 ms |
23944 KB |
n=500 |
42 |
Correct |
16 ms |
23892 KB |
n=500 |
43 |
Correct |
15 ms |
23888 KB |
n=500 |
44 |
Correct |
13 ms |
23892 KB |
n=500 |
45 |
Correct |
13 ms |
23892 KB |
n=500 |
46 |
Correct |
13 ms |
23952 KB |
n=500 |
47 |
Correct |
13 ms |
23892 KB |
n=500 |
48 |
Correct |
13 ms |
23940 KB |
n=500 |
49 |
Correct |
12 ms |
23892 KB |
n=500 |
50 |
Correct |
15 ms |
23892 KB |
n=500 |
51 |
Correct |
14 ms |
23828 KB |
n=500 |
52 |
Correct |
12 ms |
23892 KB |
n=500 |
53 |
Correct |
12 ms |
23892 KB |
n=500 |
54 |
Correct |
12 ms |
23956 KB |
n=500 |
55 |
Correct |
12 ms |
23904 KB |
n=278 |
56 |
Correct |
13 ms |
23892 KB |
n=500 |
57 |
Correct |
16 ms |
23892 KB |
n=500 |
58 |
Correct |
13 ms |
23892 KB |
n=500 |
59 |
Correct |
14 ms |
24272 KB |
n=2000 |
60 |
Correct |
13 ms |
24276 KB |
n=2000 |
61 |
Correct |
14 ms |
24332 KB |
n=2000 |
62 |
Correct |
16 ms |
24404 KB |
n=2000 |
63 |
Correct |
17 ms |
24168 KB |
n=2000 |
64 |
Correct |
15 ms |
24276 KB |
n=2000 |
65 |
Correct |
15 ms |
24148 KB |
n=2000 |
66 |
Correct |
14 ms |
24276 KB |
n=2000 |
67 |
Correct |
15 ms |
24216 KB |
n=2000 |
68 |
Correct |
14 ms |
24276 KB |
n=2000 |
69 |
Correct |
17 ms |
24276 KB |
n=2000 |
70 |
Correct |
14 ms |
24276 KB |
n=2000 |
71 |
Correct |
14 ms |
24240 KB |
n=2000 |
72 |
Correct |
13 ms |
24148 KB |
n=2000 |
73 |
Correct |
17 ms |
24256 KB |
n=2000 |
74 |
Correct |
15 ms |
24148 KB |
n=1844 |
75 |
Correct |
14 ms |
24216 KB |
n=2000 |
76 |
Correct |
15 ms |
24220 KB |
n=2000 |
77 |
Correct |
15 ms |
24148 KB |
n=2000 |
78 |
Correct |
15 ms |
24148 KB |
n=2000 |
79 |
Correct |
15 ms |
24276 KB |
n=2000 |
80 |
Correct |
13 ms |
24276 KB |
n=2000 |
81 |
Correct |
14 ms |
24308 KB |
n=2000 |
82 |
Correct |
15 ms |
24204 KB |
n=2000 |
83 |
Correct |
16 ms |
24276 KB |
n=2000 |
84 |
Correct |
16 ms |
24276 KB |
n=2000 |
85 |
Correct |
20 ms |
24280 KB |
n=2000 |
86 |
Correct |
14 ms |
24276 KB |
n=2000 |
87 |
Correct |
14 ms |
24276 KB |
n=2000 |
88 |
Correct |
14 ms |
24276 KB |
n=2000 |
89 |
Correct |
16 ms |
24276 KB |
n=2000 |
90 |
Correct |
14 ms |
24276 KB |
n=2000 |
91 |
Correct |
15 ms |
24220 KB |
n=2000 |
92 |
Correct |
693 ms |
68832 KB |
n=200000 |
93 |
Correct |
772 ms |
74216 KB |
n=200000 |
94 |
Correct |
534 ms |
78552 KB |
n=200000 |
95 |
Correct |
675 ms |
68808 KB |
n=200000 |
96 |
Correct |
607 ms |
68796 KB |
n=200000 |
97 |
Correct |
816 ms |
72992 KB |
n=200000 |
98 |
Correct |
602 ms |
68756 KB |
n=200000 |
99 |
Correct |
761 ms |
68292 KB |
n=200000 |
100 |
Correct |
637 ms |
68704 KB |
n=200000 |
101 |
Correct |
489 ms |
80144 KB |
n=200000 |
102 |
Correct |
390 ms |
69680 KB |
n=200000 |
103 |
Correct |
379 ms |
69660 KB |
n=200000 |
104 |
Correct |
388 ms |
69620 KB |
n=200000 |
105 |
Correct |
395 ms |
69520 KB |
n=200000 |
106 |
Correct |
431 ms |
69384 KB |
n=200000 |
107 |
Correct |
386 ms |
69300 KB |
n=200000 |
108 |
Correct |
706 ms |
68256 KB |
n=200000 |
109 |
Correct |
735 ms |
68496 KB |
n=200000 |
110 |
Correct |
744 ms |
68380 KB |
n=200000 |
111 |
Correct |
632 ms |
68580 KB |
n=200000 |
112 |
Correct |
542 ms |
78864 KB |
n=200000 |
113 |
Correct |
780 ms |
72800 KB |
n=200000 |
114 |
Correct |
609 ms |
68680 KB |
n=200000 |
115 |
Correct |
965 ms |
70220 KB |
n=200000 |
116 |
Correct |
623 ms |
68444 KB |
n=200000 |
117 |
Correct |
525 ms |
79540 KB |
n=200000 |
118 |
Correct |
869 ms |
71272 KB |
n=200000 |
119 |
Correct |
592 ms |
68356 KB |
n=200000 |
120 |
Correct |
445 ms |
80092 KB |
n=200000 |
121 |
Correct |
416 ms |
79948 KB |
n=200000 |
122 |
Correct |
431 ms |
80424 KB |
n=200000 |
123 |
Correct |
404 ms |
69020 KB |
n=200000 |
124 |
Correct |
148 ms |
37324 KB |
n=25264 |