#include<bits/stdc++.h>
using namespace std;
const int N = 2*1e5 + 5;
vector<int> gr[N];
int par[18][N];
int in[N], out[N];
void dfs(int nod, int dad, int &cnt){
par[0][nod] = dad;
in[nod]= ++cnt;
for(auto x :gr[nod]){
if(x!=dad)
dfs(x, nod, cnt);
}
out[nod] = cnt;
}
bool is_dad(int dad, int nod){
if(in[dad]<= in[nod] && out[nod]<= out[dad])
return true;
return false;
}
int get_lca(int a,int b){
for(int log = 17; log>=0; log--){
if(par[log][a]!=0 && is_dad(par[log][a], b)== false)
a = par[log][a];
}
if(is_dad(a, b) == false)
a = par[0][a];
return a;
}
int v[N];
int lc[N];
set<int> pzv[N];
set<int> pzl[N];
int query(set<int> &pz, int l,int r){
auto it = pz.lower_bound(l);
if(it==pz.end())
return -1;
if((*it) <=r)
return (*it);
return -1;
}
int main()
{
//freopen(".in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n, m, q;
cin>>n>>m>>q;
for(int i=1;i<n;i++){
int a, b;
cin>>a>>b;
gr[a].push_back(b);
gr[b].push_back(a);
}
int cnt =0;
dfs(1, 0, cnt);
for(int log =1 ; log < 18; log ++){
for(int nod = 1; nod<=n;nod++){
par[log][nod] = par[log-1][par[log-1][nod]];
}
}
for(int i=1;i<=m;i++){
cin>>v[i];
pzv[v[i]].insert(i);
}
for(int i=1;i<m;i++){
lc[i] = get_lca(v[i], v[i+1]);
pzl[lc[i]].insert(i);
}
for(int i=1;i<=q;i++){
int tip;
cin>>tip;
if(tip == 1){
int poz, val;
cin>>poz>>val;
pzv[v[poz]].erase(poz);
if(poz < m)
pzl[lc[poz]].erase(poz);
if(poz > 1)
pzl[lc[poz-1]].erase(poz - 1);
v[poz] = val;
pzv[v[poz]].insert(poz);
if(poz < m){
lc[poz] = get_lca(v[poz], v[poz + 1]);
pzl[lc[poz]].insert(poz);
}
if(poz > 1){
lc[poz-1] = get_lca(v[poz-1], v[poz]);
pzl[lc[poz-1]].insert(poz -1);
}
}
else {
int val, l , r;
cin>>l>>r>>val;
int ans = query(pzv[val], l, r);
if(ans != -1){
cout<<ans<<" "<<ans<<"\n";
continue;
}
ans = query(pzl[val], l, r-1);
if(ans!= -1){
cout<<ans<<" "<<ans + 1<<"\n";
continue;
}
cout<<"-1"<<" "<<"-1"<<"\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23916 KB |
n=5 |
2 |
Correct |
17 ms |
23916 KB |
n=100 |
3 |
Correct |
16 ms |
23916 KB |
n=100 |
4 |
Correct |
16 ms |
23916 KB |
n=100 |
5 |
Correct |
19 ms |
23916 KB |
n=100 |
6 |
Correct |
16 ms |
24044 KB |
n=100 |
7 |
Correct |
16 ms |
23916 KB |
n=100 |
8 |
Correct |
19 ms |
23916 KB |
n=100 |
9 |
Correct |
19 ms |
23916 KB |
n=100 |
10 |
Correct |
17 ms |
23916 KB |
n=100 |
11 |
Correct |
18 ms |
23916 KB |
n=100 |
12 |
Correct |
16 ms |
23916 KB |
n=100 |
13 |
Correct |
16 ms |
23916 KB |
n=100 |
14 |
Correct |
17 ms |
24044 KB |
n=100 |
15 |
Correct |
16 ms |
23916 KB |
n=100 |
16 |
Correct |
19 ms |
23936 KB |
n=100 |
17 |
Correct |
17 ms |
23916 KB |
n=100 |
18 |
Correct |
17 ms |
23916 KB |
n=100 |
19 |
Correct |
17 ms |
23916 KB |
n=100 |
20 |
Correct |
17 ms |
24044 KB |
n=100 |
21 |
Correct |
16 ms |
23916 KB |
n=100 |
22 |
Correct |
20 ms |
23916 KB |
n=100 |
23 |
Correct |
17 ms |
23936 KB |
n=100 |
24 |
Correct |
17 ms |
23916 KB |
n=100 |
25 |
Correct |
17 ms |
23916 KB |
n=100 |
26 |
Correct |
16 ms |
23916 KB |
n=12 |
27 |
Correct |
16 ms |
23916 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23916 KB |
n=5 |
2 |
Correct |
17 ms |
23916 KB |
n=100 |
3 |
Correct |
16 ms |
23916 KB |
n=100 |
4 |
Correct |
16 ms |
23916 KB |
n=100 |
5 |
Correct |
19 ms |
23916 KB |
n=100 |
6 |
Correct |
16 ms |
24044 KB |
n=100 |
7 |
Correct |
16 ms |
23916 KB |
n=100 |
8 |
Correct |
19 ms |
23916 KB |
n=100 |
9 |
Correct |
19 ms |
23916 KB |
n=100 |
10 |
Correct |
17 ms |
23916 KB |
n=100 |
11 |
Correct |
18 ms |
23916 KB |
n=100 |
12 |
Correct |
16 ms |
23916 KB |
n=100 |
13 |
Correct |
16 ms |
23916 KB |
n=100 |
14 |
Correct |
17 ms |
24044 KB |
n=100 |
15 |
Correct |
16 ms |
23916 KB |
n=100 |
16 |
Correct |
19 ms |
23936 KB |
n=100 |
17 |
Correct |
17 ms |
23916 KB |
n=100 |
18 |
Correct |
17 ms |
23916 KB |
n=100 |
19 |
Correct |
17 ms |
23916 KB |
n=100 |
20 |
Correct |
17 ms |
24044 KB |
n=100 |
21 |
Correct |
16 ms |
23916 KB |
n=100 |
22 |
Correct |
20 ms |
23916 KB |
n=100 |
23 |
Correct |
17 ms |
23936 KB |
n=100 |
24 |
Correct |
17 ms |
23916 KB |
n=100 |
25 |
Correct |
17 ms |
23916 KB |
n=100 |
26 |
Correct |
16 ms |
23916 KB |
n=12 |
27 |
Correct |
16 ms |
23916 KB |
n=100 |
28 |
Correct |
17 ms |
24044 KB |
n=500 |
29 |
Correct |
17 ms |
24044 KB |
n=500 |
30 |
Correct |
18 ms |
24044 KB |
n=500 |
31 |
Correct |
17 ms |
24044 KB |
n=500 |
32 |
Correct |
17 ms |
24044 KB |
n=500 |
33 |
Correct |
17 ms |
24044 KB |
n=500 |
34 |
Correct |
17 ms |
24044 KB |
n=500 |
35 |
Correct |
17 ms |
24172 KB |
n=500 |
36 |
Correct |
17 ms |
24044 KB |
n=500 |
37 |
Correct |
17 ms |
24044 KB |
n=500 |
38 |
Correct |
17 ms |
24044 KB |
n=500 |
39 |
Correct |
17 ms |
24172 KB |
n=500 |
40 |
Correct |
17 ms |
24044 KB |
n=500 |
41 |
Correct |
17 ms |
24044 KB |
n=500 |
42 |
Correct |
17 ms |
24044 KB |
n=500 |
43 |
Correct |
17 ms |
24044 KB |
n=500 |
44 |
Correct |
18 ms |
24044 KB |
n=500 |
45 |
Correct |
17 ms |
24044 KB |
n=500 |
46 |
Correct |
17 ms |
24044 KB |
n=500 |
47 |
Correct |
18 ms |
24044 KB |
n=500 |
48 |
Correct |
17 ms |
24044 KB |
n=500 |
49 |
Correct |
17 ms |
24044 KB |
n=500 |
50 |
Correct |
20 ms |
24044 KB |
n=500 |
51 |
Correct |
18 ms |
24044 KB |
n=500 |
52 |
Correct |
17 ms |
24044 KB |
n=500 |
53 |
Correct |
17 ms |
24044 KB |
n=500 |
54 |
Correct |
18 ms |
24044 KB |
n=500 |
55 |
Correct |
17 ms |
24044 KB |
n=278 |
56 |
Correct |
17 ms |
24044 KB |
n=500 |
57 |
Correct |
17 ms |
24044 KB |
n=500 |
58 |
Correct |
17 ms |
24044 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23916 KB |
n=5 |
2 |
Correct |
17 ms |
23916 KB |
n=100 |
3 |
Correct |
16 ms |
23916 KB |
n=100 |
4 |
Correct |
16 ms |
23916 KB |
n=100 |
5 |
Correct |
19 ms |
23916 KB |
n=100 |
6 |
Correct |
16 ms |
24044 KB |
n=100 |
7 |
Correct |
16 ms |
23916 KB |
n=100 |
8 |
Correct |
19 ms |
23916 KB |
n=100 |
9 |
Correct |
19 ms |
23916 KB |
n=100 |
10 |
Correct |
17 ms |
23916 KB |
n=100 |
11 |
Correct |
18 ms |
23916 KB |
n=100 |
12 |
Correct |
16 ms |
23916 KB |
n=100 |
13 |
Correct |
16 ms |
23916 KB |
n=100 |
14 |
Correct |
17 ms |
24044 KB |
n=100 |
15 |
Correct |
16 ms |
23916 KB |
n=100 |
16 |
Correct |
19 ms |
23936 KB |
n=100 |
17 |
Correct |
17 ms |
23916 KB |
n=100 |
18 |
Correct |
17 ms |
23916 KB |
n=100 |
19 |
Correct |
17 ms |
23916 KB |
n=100 |
20 |
Correct |
17 ms |
24044 KB |
n=100 |
21 |
Correct |
16 ms |
23916 KB |
n=100 |
22 |
Correct |
20 ms |
23916 KB |
n=100 |
23 |
Correct |
17 ms |
23936 KB |
n=100 |
24 |
Correct |
17 ms |
23916 KB |
n=100 |
25 |
Correct |
17 ms |
23916 KB |
n=100 |
26 |
Correct |
16 ms |
23916 KB |
n=12 |
27 |
Correct |
16 ms |
23916 KB |
n=100 |
28 |
Correct |
17 ms |
24044 KB |
n=500 |
29 |
Correct |
17 ms |
24044 KB |
n=500 |
30 |
Correct |
18 ms |
24044 KB |
n=500 |
31 |
Correct |
17 ms |
24044 KB |
n=500 |
32 |
Correct |
17 ms |
24044 KB |
n=500 |
33 |
Correct |
17 ms |
24044 KB |
n=500 |
34 |
Correct |
17 ms |
24044 KB |
n=500 |
35 |
Correct |
17 ms |
24172 KB |
n=500 |
36 |
Correct |
17 ms |
24044 KB |
n=500 |
37 |
Correct |
17 ms |
24044 KB |
n=500 |
38 |
Correct |
17 ms |
24044 KB |
n=500 |
39 |
Correct |
17 ms |
24172 KB |
n=500 |
40 |
Correct |
17 ms |
24044 KB |
n=500 |
41 |
Correct |
17 ms |
24044 KB |
n=500 |
42 |
Correct |
17 ms |
24044 KB |
n=500 |
43 |
Correct |
17 ms |
24044 KB |
n=500 |
44 |
Correct |
18 ms |
24044 KB |
n=500 |
45 |
Correct |
17 ms |
24044 KB |
n=500 |
46 |
Correct |
17 ms |
24044 KB |
n=500 |
47 |
Correct |
18 ms |
24044 KB |
n=500 |
48 |
Correct |
17 ms |
24044 KB |
n=500 |
49 |
Correct |
17 ms |
24044 KB |
n=500 |
50 |
Correct |
20 ms |
24044 KB |
n=500 |
51 |
Correct |
18 ms |
24044 KB |
n=500 |
52 |
Correct |
17 ms |
24044 KB |
n=500 |
53 |
Correct |
17 ms |
24044 KB |
n=500 |
54 |
Correct |
18 ms |
24044 KB |
n=500 |
55 |
Correct |
17 ms |
24044 KB |
n=278 |
56 |
Correct |
17 ms |
24044 KB |
n=500 |
57 |
Correct |
17 ms |
24044 KB |
n=500 |
58 |
Correct |
17 ms |
24044 KB |
n=500 |
59 |
Correct |
19 ms |
24428 KB |
n=2000 |
60 |
Correct |
20 ms |
24556 KB |
n=2000 |
61 |
Correct |
21 ms |
24556 KB |
n=2000 |
62 |
Correct |
21 ms |
24556 KB |
n=2000 |
63 |
Correct |
20 ms |
24428 KB |
n=2000 |
64 |
Correct |
20 ms |
24556 KB |
n=2000 |
65 |
Correct |
20 ms |
24428 KB |
n=2000 |
66 |
Correct |
20 ms |
24556 KB |
n=2000 |
67 |
Correct |
24 ms |
24428 KB |
n=2000 |
68 |
Correct |
23 ms |
24556 KB |
n=2000 |
69 |
Correct |
19 ms |
24428 KB |
n=2000 |
70 |
Correct |
19 ms |
24428 KB |
n=2000 |
71 |
Correct |
19 ms |
24428 KB |
n=2000 |
72 |
Correct |
20 ms |
24556 KB |
n=2000 |
73 |
Correct |
20 ms |
24428 KB |
n=2000 |
74 |
Correct |
19 ms |
24428 KB |
n=1844 |
75 |
Correct |
19 ms |
24428 KB |
n=2000 |
76 |
Correct |
20 ms |
24428 KB |
n=2000 |
77 |
Correct |
20 ms |
24428 KB |
n=2000 |
78 |
Correct |
20 ms |
24428 KB |
n=2000 |
79 |
Correct |
20 ms |
24428 KB |
n=2000 |
80 |
Correct |
20 ms |
24556 KB |
n=2000 |
81 |
Correct |
21 ms |
24556 KB |
n=2000 |
82 |
Correct |
20 ms |
24428 KB |
n=2000 |
83 |
Correct |
20 ms |
24556 KB |
n=2000 |
84 |
Correct |
20 ms |
24556 KB |
n=2000 |
85 |
Correct |
20 ms |
24428 KB |
n=2000 |
86 |
Correct |
20 ms |
24428 KB |
n=2000 |
87 |
Correct |
20 ms |
24428 KB |
n=2000 |
88 |
Correct |
20 ms |
24556 KB |
n=2000 |
89 |
Correct |
20 ms |
24556 KB |
n=2000 |
90 |
Correct |
20 ms |
24556 KB |
n=2000 |
91 |
Correct |
19 ms |
24428 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23916 KB |
n=5 |
2 |
Correct |
17 ms |
23916 KB |
n=100 |
3 |
Correct |
16 ms |
23916 KB |
n=100 |
4 |
Correct |
16 ms |
23916 KB |
n=100 |
5 |
Correct |
19 ms |
23916 KB |
n=100 |
6 |
Correct |
16 ms |
24044 KB |
n=100 |
7 |
Correct |
16 ms |
23916 KB |
n=100 |
8 |
Correct |
19 ms |
23916 KB |
n=100 |
9 |
Correct |
19 ms |
23916 KB |
n=100 |
10 |
Correct |
17 ms |
23916 KB |
n=100 |
11 |
Correct |
18 ms |
23916 KB |
n=100 |
12 |
Correct |
16 ms |
23916 KB |
n=100 |
13 |
Correct |
16 ms |
23916 KB |
n=100 |
14 |
Correct |
17 ms |
24044 KB |
n=100 |
15 |
Correct |
16 ms |
23916 KB |
n=100 |
16 |
Correct |
19 ms |
23936 KB |
n=100 |
17 |
Correct |
17 ms |
23916 KB |
n=100 |
18 |
Correct |
17 ms |
23916 KB |
n=100 |
19 |
Correct |
17 ms |
23916 KB |
n=100 |
20 |
Correct |
17 ms |
24044 KB |
n=100 |
21 |
Correct |
16 ms |
23916 KB |
n=100 |
22 |
Correct |
20 ms |
23916 KB |
n=100 |
23 |
Correct |
17 ms |
23936 KB |
n=100 |
24 |
Correct |
17 ms |
23916 KB |
n=100 |
25 |
Correct |
17 ms |
23916 KB |
n=100 |
26 |
Correct |
16 ms |
23916 KB |
n=12 |
27 |
Correct |
16 ms |
23916 KB |
n=100 |
28 |
Correct |
17 ms |
24044 KB |
n=500 |
29 |
Correct |
17 ms |
24044 KB |
n=500 |
30 |
Correct |
18 ms |
24044 KB |
n=500 |
31 |
Correct |
17 ms |
24044 KB |
n=500 |
32 |
Correct |
17 ms |
24044 KB |
n=500 |
33 |
Correct |
17 ms |
24044 KB |
n=500 |
34 |
Correct |
17 ms |
24044 KB |
n=500 |
35 |
Correct |
17 ms |
24172 KB |
n=500 |
36 |
Correct |
17 ms |
24044 KB |
n=500 |
37 |
Correct |
17 ms |
24044 KB |
n=500 |
38 |
Correct |
17 ms |
24044 KB |
n=500 |
39 |
Correct |
17 ms |
24172 KB |
n=500 |
40 |
Correct |
17 ms |
24044 KB |
n=500 |
41 |
Correct |
17 ms |
24044 KB |
n=500 |
42 |
Correct |
17 ms |
24044 KB |
n=500 |
43 |
Correct |
17 ms |
24044 KB |
n=500 |
44 |
Correct |
18 ms |
24044 KB |
n=500 |
45 |
Correct |
17 ms |
24044 KB |
n=500 |
46 |
Correct |
17 ms |
24044 KB |
n=500 |
47 |
Correct |
18 ms |
24044 KB |
n=500 |
48 |
Correct |
17 ms |
24044 KB |
n=500 |
49 |
Correct |
17 ms |
24044 KB |
n=500 |
50 |
Correct |
20 ms |
24044 KB |
n=500 |
51 |
Correct |
18 ms |
24044 KB |
n=500 |
52 |
Correct |
17 ms |
24044 KB |
n=500 |
53 |
Correct |
17 ms |
24044 KB |
n=500 |
54 |
Correct |
18 ms |
24044 KB |
n=500 |
55 |
Correct |
17 ms |
24044 KB |
n=278 |
56 |
Correct |
17 ms |
24044 KB |
n=500 |
57 |
Correct |
17 ms |
24044 KB |
n=500 |
58 |
Correct |
17 ms |
24044 KB |
n=500 |
59 |
Correct |
19 ms |
24428 KB |
n=2000 |
60 |
Correct |
20 ms |
24556 KB |
n=2000 |
61 |
Correct |
21 ms |
24556 KB |
n=2000 |
62 |
Correct |
21 ms |
24556 KB |
n=2000 |
63 |
Correct |
20 ms |
24428 KB |
n=2000 |
64 |
Correct |
20 ms |
24556 KB |
n=2000 |
65 |
Correct |
20 ms |
24428 KB |
n=2000 |
66 |
Correct |
20 ms |
24556 KB |
n=2000 |
67 |
Correct |
24 ms |
24428 KB |
n=2000 |
68 |
Correct |
23 ms |
24556 KB |
n=2000 |
69 |
Correct |
19 ms |
24428 KB |
n=2000 |
70 |
Correct |
19 ms |
24428 KB |
n=2000 |
71 |
Correct |
19 ms |
24428 KB |
n=2000 |
72 |
Correct |
20 ms |
24556 KB |
n=2000 |
73 |
Correct |
20 ms |
24428 KB |
n=2000 |
74 |
Correct |
19 ms |
24428 KB |
n=1844 |
75 |
Correct |
19 ms |
24428 KB |
n=2000 |
76 |
Correct |
20 ms |
24428 KB |
n=2000 |
77 |
Correct |
20 ms |
24428 KB |
n=2000 |
78 |
Correct |
20 ms |
24428 KB |
n=2000 |
79 |
Correct |
20 ms |
24428 KB |
n=2000 |
80 |
Correct |
20 ms |
24556 KB |
n=2000 |
81 |
Correct |
21 ms |
24556 KB |
n=2000 |
82 |
Correct |
20 ms |
24428 KB |
n=2000 |
83 |
Correct |
20 ms |
24556 KB |
n=2000 |
84 |
Correct |
20 ms |
24556 KB |
n=2000 |
85 |
Correct |
20 ms |
24428 KB |
n=2000 |
86 |
Correct |
20 ms |
24428 KB |
n=2000 |
87 |
Correct |
20 ms |
24428 KB |
n=2000 |
88 |
Correct |
20 ms |
24556 KB |
n=2000 |
89 |
Correct |
20 ms |
24556 KB |
n=2000 |
90 |
Correct |
20 ms |
24556 KB |
n=2000 |
91 |
Correct |
19 ms |
24428 KB |
n=2000 |
92 |
Correct |
906 ms |
74724 KB |
n=200000 |
93 |
Correct |
1543 ms |
81132 KB |
n=200000 |
94 |
Correct |
1497 ms |
85652 KB |
n=200000 |
95 |
Correct |
873 ms |
74464 KB |
n=200000 |
96 |
Correct |
823 ms |
74464 KB |
n=200000 |
97 |
Correct |
1553 ms |
79980 KB |
n=200000 |
98 |
Correct |
873 ms |
74764 KB |
n=200000 |
99 |
Correct |
971 ms |
74860 KB |
n=200000 |
100 |
Correct |
881 ms |
74596 KB |
n=200000 |
101 |
Correct |
1516 ms |
87448 KB |
n=200000 |
102 |
Correct |
498 ms |
75700 KB |
n=200000 |
103 |
Correct |
491 ms |
75628 KB |
n=200000 |
104 |
Correct |
478 ms |
75628 KB |
n=200000 |
105 |
Correct |
497 ms |
76140 KB |
n=200000 |
106 |
Correct |
493 ms |
76068 KB |
n=200000 |
107 |
Correct |
491 ms |
76140 KB |
n=200000 |
108 |
Correct |
901 ms |
74604 KB |
n=200000 |
109 |
Correct |
901 ms |
74564 KB |
n=200000 |
110 |
Correct |
903 ms |
74632 KB |
n=200000 |
111 |
Correct |
863 ms |
73956 KB |
n=200000 |
112 |
Correct |
1513 ms |
85996 KB |
n=200000 |
113 |
Correct |
1556 ms |
79980 KB |
n=200000 |
114 |
Correct |
847 ms |
74028 KB |
n=200000 |
115 |
Correct |
1650 ms |
77164 KB |
n=200000 |
116 |
Correct |
834 ms |
74720 KB |
n=200000 |
117 |
Correct |
1485 ms |
86380 KB |
n=200000 |
118 |
Correct |
1570 ms |
78408 KB |
n=200000 |
119 |
Correct |
825 ms |
74584 KB |
n=200000 |
120 |
Correct |
1432 ms |
86444 KB |
n=200000 |
121 |
Correct |
1434 ms |
86380 KB |
n=200000 |
122 |
Correct |
1450 ms |
86624 KB |
n=200000 |
123 |
Correct |
516 ms |
75884 KB |
n=200000 |
124 |
Correct |
324 ms |
39404 KB |
n=25264 |