#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define FOR(i,a,b) for (auto i = (a); i <= (b); ++i)
#define NFOR(i,a,b) for(auto i = (a); i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) lli(x.size())
#define mp(i,a) make_pair(i,a)
#define pb(a) push_back(a)
#define bit(x,b) (x&(1LL<<b))
typedef long long int lli;
typedef pair <lli,lli> ii;
typedef pair <lli,ii> iii;
typedef vector <lli> vi;
vi al[200001];
set<ii> ans[200001];
int par[200001][32],din[200001],dout[200001],ti=0,n,m,q,arr[200001];
void dfs(int node,int p)
{
par[node][0]=p;
for(int i=1;i<32;++i)
par[node][i]=par[par[node][i-1]][i-1];
din[node]=ti++;
for(int i=0;i<al[node].size();++i)
if(al[node][i]!=p)
dfs(al[node][i],node);
dout[node]=ti++;
}
bool isa(int f,int s)
{
return (din[f]<=din[s])&&(dout[f]>=dout[s]);
}
int lca(int f,int s)
{
if(isa(f,s))
return f;
if(isa(s,f))
return s;
for(int i=31;i>=0;--i)
if(!isa(par[f][i],s))
f=par[f][i];
return par[f][0];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m>>q;
for(int i=0;i<n-1;++i)
{
int f,s;
cin>>f>>s;
al[f].pb(s);
al[s].pb(f);
}
dfs(1,1);
for(int i=0;i<m;++i)
cin>>arr[i];
for(int i=0;i<m;++i)
{
ans[arr[i]].insert(mp(i,i));
int l;
if(i!=0)
{
l=lca(arr[i-1],arr[i]);
ans[l].insert(mp(i-1,i));
}
if(i!=m-1)
{
l=lca(arr[i+1],arr[i]);
ans[l].insert(mp(i,i+1));
}
}
for(int i=0;i<q;++i)
{
int t,f,s,l,v;
cin>>t>>f>>s;
if(t==1)
{
f--;
ans[arr[f]].erase(mp(f,f));
if(f!=0)
{
l=lca(arr[f-1],arr[f]);
ans[l].erase(mp(f-1,f));
}
if(f!=m-1)
{
l=lca(arr[f+1],arr[f]);
ans[l].erase(mp(f,f+1));
}
arr[f]=s;
ans[s].insert(mp(f,f));
if(f!=0)
{
l=lca(arr[f-1],arr[f]);
ans[l].insert(mp(f-1,f));
}
if(f!=m-1)
{
l=lca(arr[f+1],arr[f]);
ans[l].insert(mp(f,f+1));
}
}
else
{
f--;
s--;
cin>>v;
auto it=ans[v].lower_bound(mp(f,0));
if(it!=ans[v].end())
{
if((*it).S<=s)
cout<<(*it).F+1<<' '<<(*it).S+1<<endl;
else
cout<<-1<<' '<<-1<<endl;
}
else
cout<<-1<<' '<<-1<<endl;
}
}
return(0);
}
Compilation message
treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:26:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<al[node].size();++i)
~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
n=5 |
2 |
Correct |
10 ms |
14464 KB |
n=100 |
3 |
Correct |
11 ms |
14464 KB |
n=100 |
4 |
Correct |
10 ms |
14464 KB |
n=100 |
5 |
Correct |
10 ms |
14464 KB |
n=100 |
6 |
Correct |
10 ms |
14464 KB |
n=100 |
7 |
Correct |
12 ms |
14464 KB |
n=100 |
8 |
Correct |
11 ms |
14592 KB |
n=100 |
9 |
Correct |
13 ms |
14464 KB |
n=100 |
10 |
Correct |
10 ms |
14464 KB |
n=100 |
11 |
Correct |
11 ms |
14592 KB |
n=100 |
12 |
Correct |
12 ms |
14464 KB |
n=100 |
13 |
Correct |
12 ms |
14464 KB |
n=100 |
14 |
Correct |
10 ms |
14464 KB |
n=100 |
15 |
Correct |
10 ms |
14464 KB |
n=100 |
16 |
Correct |
12 ms |
14464 KB |
n=100 |
17 |
Correct |
12 ms |
14464 KB |
n=100 |
18 |
Correct |
10 ms |
14464 KB |
n=100 |
19 |
Correct |
10 ms |
14464 KB |
n=100 |
20 |
Correct |
12 ms |
14464 KB |
n=100 |
21 |
Correct |
10 ms |
14464 KB |
n=100 |
22 |
Correct |
12 ms |
14464 KB |
n=100 |
23 |
Correct |
10 ms |
14464 KB |
n=100 |
24 |
Correct |
11 ms |
14464 KB |
n=100 |
25 |
Correct |
10 ms |
14464 KB |
n=100 |
26 |
Correct |
10 ms |
14464 KB |
n=12 |
27 |
Correct |
11 ms |
14464 KB |
n=100 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
n=5 |
2 |
Correct |
10 ms |
14464 KB |
n=100 |
3 |
Correct |
11 ms |
14464 KB |
n=100 |
4 |
Correct |
10 ms |
14464 KB |
n=100 |
5 |
Correct |
10 ms |
14464 KB |
n=100 |
6 |
Correct |
10 ms |
14464 KB |
n=100 |
7 |
Correct |
12 ms |
14464 KB |
n=100 |
8 |
Correct |
11 ms |
14592 KB |
n=100 |
9 |
Correct |
13 ms |
14464 KB |
n=100 |
10 |
Correct |
10 ms |
14464 KB |
n=100 |
11 |
Correct |
11 ms |
14592 KB |
n=100 |
12 |
Correct |
12 ms |
14464 KB |
n=100 |
13 |
Correct |
12 ms |
14464 KB |
n=100 |
14 |
Correct |
10 ms |
14464 KB |
n=100 |
15 |
Correct |
10 ms |
14464 KB |
n=100 |
16 |
Correct |
12 ms |
14464 KB |
n=100 |
17 |
Correct |
12 ms |
14464 KB |
n=100 |
18 |
Correct |
10 ms |
14464 KB |
n=100 |
19 |
Correct |
10 ms |
14464 KB |
n=100 |
20 |
Correct |
12 ms |
14464 KB |
n=100 |
21 |
Correct |
10 ms |
14464 KB |
n=100 |
22 |
Correct |
12 ms |
14464 KB |
n=100 |
23 |
Correct |
10 ms |
14464 KB |
n=100 |
24 |
Correct |
11 ms |
14464 KB |
n=100 |
25 |
Correct |
10 ms |
14464 KB |
n=100 |
26 |
Correct |
10 ms |
14464 KB |
n=12 |
27 |
Correct |
11 ms |
14464 KB |
n=100 |
28 |
Correct |
12 ms |
14592 KB |
n=500 |
29 |
Correct |
14 ms |
14592 KB |
n=500 |
30 |
Correct |
13 ms |
14592 KB |
n=500 |
31 |
Correct |
11 ms |
14592 KB |
n=500 |
32 |
Correct |
12 ms |
14592 KB |
n=500 |
33 |
Correct |
13 ms |
14592 KB |
n=500 |
34 |
Correct |
14 ms |
14592 KB |
n=500 |
35 |
Correct |
13 ms |
14592 KB |
n=500 |
36 |
Correct |
12 ms |
14592 KB |
n=500 |
37 |
Correct |
14 ms |
14720 KB |
n=500 |
38 |
Correct |
11 ms |
14592 KB |
n=500 |
39 |
Correct |
13 ms |
14628 KB |
n=500 |
40 |
Correct |
13 ms |
14592 KB |
n=500 |
41 |
Correct |
11 ms |
14592 KB |
n=500 |
42 |
Correct |
11 ms |
14592 KB |
n=500 |
43 |
Correct |
12 ms |
14676 KB |
n=500 |
44 |
Correct |
11 ms |
14592 KB |
n=500 |
45 |
Correct |
14 ms |
14592 KB |
n=500 |
46 |
Correct |
12 ms |
14592 KB |
n=500 |
47 |
Correct |
13 ms |
14592 KB |
n=500 |
48 |
Correct |
12 ms |
14592 KB |
n=500 |
49 |
Correct |
14 ms |
14592 KB |
n=500 |
50 |
Correct |
12 ms |
14592 KB |
n=500 |
51 |
Correct |
11 ms |
14620 KB |
n=500 |
52 |
Correct |
13 ms |
14592 KB |
n=500 |
53 |
Correct |
11 ms |
14592 KB |
n=500 |
54 |
Correct |
13 ms |
14592 KB |
n=500 |
55 |
Correct |
10 ms |
14592 KB |
n=278 |
56 |
Correct |
12 ms |
14592 KB |
n=500 |
57 |
Correct |
12 ms |
14592 KB |
n=500 |
58 |
Correct |
14 ms |
14592 KB |
n=500 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
n=5 |
2 |
Correct |
10 ms |
14464 KB |
n=100 |
3 |
Correct |
11 ms |
14464 KB |
n=100 |
4 |
Correct |
10 ms |
14464 KB |
n=100 |
5 |
Correct |
10 ms |
14464 KB |
n=100 |
6 |
Correct |
10 ms |
14464 KB |
n=100 |
7 |
Correct |
12 ms |
14464 KB |
n=100 |
8 |
Correct |
11 ms |
14592 KB |
n=100 |
9 |
Correct |
13 ms |
14464 KB |
n=100 |
10 |
Correct |
10 ms |
14464 KB |
n=100 |
11 |
Correct |
11 ms |
14592 KB |
n=100 |
12 |
Correct |
12 ms |
14464 KB |
n=100 |
13 |
Correct |
12 ms |
14464 KB |
n=100 |
14 |
Correct |
10 ms |
14464 KB |
n=100 |
15 |
Correct |
10 ms |
14464 KB |
n=100 |
16 |
Correct |
12 ms |
14464 KB |
n=100 |
17 |
Correct |
12 ms |
14464 KB |
n=100 |
18 |
Correct |
10 ms |
14464 KB |
n=100 |
19 |
Correct |
10 ms |
14464 KB |
n=100 |
20 |
Correct |
12 ms |
14464 KB |
n=100 |
21 |
Correct |
10 ms |
14464 KB |
n=100 |
22 |
Correct |
12 ms |
14464 KB |
n=100 |
23 |
Correct |
10 ms |
14464 KB |
n=100 |
24 |
Correct |
11 ms |
14464 KB |
n=100 |
25 |
Correct |
10 ms |
14464 KB |
n=100 |
26 |
Correct |
10 ms |
14464 KB |
n=12 |
27 |
Correct |
11 ms |
14464 KB |
n=100 |
28 |
Correct |
12 ms |
14592 KB |
n=500 |
29 |
Correct |
14 ms |
14592 KB |
n=500 |
30 |
Correct |
13 ms |
14592 KB |
n=500 |
31 |
Correct |
11 ms |
14592 KB |
n=500 |
32 |
Correct |
12 ms |
14592 KB |
n=500 |
33 |
Correct |
13 ms |
14592 KB |
n=500 |
34 |
Correct |
14 ms |
14592 KB |
n=500 |
35 |
Correct |
13 ms |
14592 KB |
n=500 |
36 |
Correct |
12 ms |
14592 KB |
n=500 |
37 |
Correct |
14 ms |
14720 KB |
n=500 |
38 |
Correct |
11 ms |
14592 KB |
n=500 |
39 |
Correct |
13 ms |
14628 KB |
n=500 |
40 |
Correct |
13 ms |
14592 KB |
n=500 |
41 |
Correct |
11 ms |
14592 KB |
n=500 |
42 |
Correct |
11 ms |
14592 KB |
n=500 |
43 |
Correct |
12 ms |
14676 KB |
n=500 |
44 |
Correct |
11 ms |
14592 KB |
n=500 |
45 |
Correct |
14 ms |
14592 KB |
n=500 |
46 |
Correct |
12 ms |
14592 KB |
n=500 |
47 |
Correct |
13 ms |
14592 KB |
n=500 |
48 |
Correct |
12 ms |
14592 KB |
n=500 |
49 |
Correct |
14 ms |
14592 KB |
n=500 |
50 |
Correct |
12 ms |
14592 KB |
n=500 |
51 |
Correct |
11 ms |
14620 KB |
n=500 |
52 |
Correct |
13 ms |
14592 KB |
n=500 |
53 |
Correct |
11 ms |
14592 KB |
n=500 |
54 |
Correct |
13 ms |
14592 KB |
n=500 |
55 |
Correct |
10 ms |
14592 KB |
n=278 |
56 |
Correct |
12 ms |
14592 KB |
n=500 |
57 |
Correct |
12 ms |
14592 KB |
n=500 |
58 |
Correct |
14 ms |
14592 KB |
n=500 |
59 |
Correct |
17 ms |
15104 KB |
n=2000 |
60 |
Correct |
16 ms |
15232 KB |
n=2000 |
61 |
Correct |
17 ms |
15232 KB |
n=2000 |
62 |
Correct |
18 ms |
15104 KB |
n=2000 |
63 |
Correct |
18 ms |
15104 KB |
n=2000 |
64 |
Correct |
20 ms |
15104 KB |
n=2000 |
65 |
Correct |
16 ms |
15104 KB |
n=2000 |
66 |
Correct |
17 ms |
15232 KB |
n=2000 |
67 |
Correct |
18 ms |
15104 KB |
n=2000 |
68 |
Correct |
17 ms |
15104 KB |
n=2000 |
69 |
Correct |
18 ms |
15104 KB |
n=2000 |
70 |
Correct |
18 ms |
15104 KB |
n=2000 |
71 |
Correct |
18 ms |
15104 KB |
n=2000 |
72 |
Correct |
18 ms |
15104 KB |
n=2000 |
73 |
Correct |
18 ms |
15104 KB |
n=2000 |
74 |
Correct |
17 ms |
15104 KB |
n=1844 |
75 |
Correct |
20 ms |
15104 KB |
n=2000 |
76 |
Correct |
20 ms |
15104 KB |
n=2000 |
77 |
Correct |
21 ms |
15104 KB |
n=2000 |
78 |
Correct |
18 ms |
15104 KB |
n=2000 |
79 |
Correct |
20 ms |
15128 KB |
n=2000 |
80 |
Correct |
16 ms |
15232 KB |
n=2000 |
81 |
Correct |
16 ms |
15104 KB |
n=2000 |
82 |
Correct |
18 ms |
15104 KB |
n=2000 |
83 |
Correct |
19 ms |
15232 KB |
n=2000 |
84 |
Correct |
20 ms |
15104 KB |
n=2000 |
85 |
Correct |
17 ms |
15104 KB |
n=2000 |
86 |
Correct |
20 ms |
15104 KB |
n=2000 |
87 |
Correct |
20 ms |
15104 KB |
n=2000 |
88 |
Correct |
18 ms |
15232 KB |
n=2000 |
89 |
Correct |
16 ms |
15232 KB |
n=2000 |
90 |
Correct |
18 ms |
15232 KB |
n=2000 |
91 |
Correct |
21 ms |
15104 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
n=5 |
2 |
Correct |
10 ms |
14464 KB |
n=100 |
3 |
Correct |
11 ms |
14464 KB |
n=100 |
4 |
Correct |
10 ms |
14464 KB |
n=100 |
5 |
Correct |
10 ms |
14464 KB |
n=100 |
6 |
Correct |
10 ms |
14464 KB |
n=100 |
7 |
Correct |
12 ms |
14464 KB |
n=100 |
8 |
Correct |
11 ms |
14592 KB |
n=100 |
9 |
Correct |
13 ms |
14464 KB |
n=100 |
10 |
Correct |
10 ms |
14464 KB |
n=100 |
11 |
Correct |
11 ms |
14592 KB |
n=100 |
12 |
Correct |
12 ms |
14464 KB |
n=100 |
13 |
Correct |
12 ms |
14464 KB |
n=100 |
14 |
Correct |
10 ms |
14464 KB |
n=100 |
15 |
Correct |
10 ms |
14464 KB |
n=100 |
16 |
Correct |
12 ms |
14464 KB |
n=100 |
17 |
Correct |
12 ms |
14464 KB |
n=100 |
18 |
Correct |
10 ms |
14464 KB |
n=100 |
19 |
Correct |
10 ms |
14464 KB |
n=100 |
20 |
Correct |
12 ms |
14464 KB |
n=100 |
21 |
Correct |
10 ms |
14464 KB |
n=100 |
22 |
Correct |
12 ms |
14464 KB |
n=100 |
23 |
Correct |
10 ms |
14464 KB |
n=100 |
24 |
Correct |
11 ms |
14464 KB |
n=100 |
25 |
Correct |
10 ms |
14464 KB |
n=100 |
26 |
Correct |
10 ms |
14464 KB |
n=12 |
27 |
Correct |
11 ms |
14464 KB |
n=100 |
28 |
Correct |
12 ms |
14592 KB |
n=500 |
29 |
Correct |
14 ms |
14592 KB |
n=500 |
30 |
Correct |
13 ms |
14592 KB |
n=500 |
31 |
Correct |
11 ms |
14592 KB |
n=500 |
32 |
Correct |
12 ms |
14592 KB |
n=500 |
33 |
Correct |
13 ms |
14592 KB |
n=500 |
34 |
Correct |
14 ms |
14592 KB |
n=500 |
35 |
Correct |
13 ms |
14592 KB |
n=500 |
36 |
Correct |
12 ms |
14592 KB |
n=500 |
37 |
Correct |
14 ms |
14720 KB |
n=500 |
38 |
Correct |
11 ms |
14592 KB |
n=500 |
39 |
Correct |
13 ms |
14628 KB |
n=500 |
40 |
Correct |
13 ms |
14592 KB |
n=500 |
41 |
Correct |
11 ms |
14592 KB |
n=500 |
42 |
Correct |
11 ms |
14592 KB |
n=500 |
43 |
Correct |
12 ms |
14676 KB |
n=500 |
44 |
Correct |
11 ms |
14592 KB |
n=500 |
45 |
Correct |
14 ms |
14592 KB |
n=500 |
46 |
Correct |
12 ms |
14592 KB |
n=500 |
47 |
Correct |
13 ms |
14592 KB |
n=500 |
48 |
Correct |
12 ms |
14592 KB |
n=500 |
49 |
Correct |
14 ms |
14592 KB |
n=500 |
50 |
Correct |
12 ms |
14592 KB |
n=500 |
51 |
Correct |
11 ms |
14620 KB |
n=500 |
52 |
Correct |
13 ms |
14592 KB |
n=500 |
53 |
Correct |
11 ms |
14592 KB |
n=500 |
54 |
Correct |
13 ms |
14592 KB |
n=500 |
55 |
Correct |
10 ms |
14592 KB |
n=278 |
56 |
Correct |
12 ms |
14592 KB |
n=500 |
57 |
Correct |
12 ms |
14592 KB |
n=500 |
58 |
Correct |
14 ms |
14592 KB |
n=500 |
59 |
Correct |
17 ms |
15104 KB |
n=2000 |
60 |
Correct |
16 ms |
15232 KB |
n=2000 |
61 |
Correct |
17 ms |
15232 KB |
n=2000 |
62 |
Correct |
18 ms |
15104 KB |
n=2000 |
63 |
Correct |
18 ms |
15104 KB |
n=2000 |
64 |
Correct |
20 ms |
15104 KB |
n=2000 |
65 |
Correct |
16 ms |
15104 KB |
n=2000 |
66 |
Correct |
17 ms |
15232 KB |
n=2000 |
67 |
Correct |
18 ms |
15104 KB |
n=2000 |
68 |
Correct |
17 ms |
15104 KB |
n=2000 |
69 |
Correct |
18 ms |
15104 KB |
n=2000 |
70 |
Correct |
18 ms |
15104 KB |
n=2000 |
71 |
Correct |
18 ms |
15104 KB |
n=2000 |
72 |
Correct |
18 ms |
15104 KB |
n=2000 |
73 |
Correct |
18 ms |
15104 KB |
n=2000 |
74 |
Correct |
17 ms |
15104 KB |
n=1844 |
75 |
Correct |
20 ms |
15104 KB |
n=2000 |
76 |
Correct |
20 ms |
15104 KB |
n=2000 |
77 |
Correct |
21 ms |
15104 KB |
n=2000 |
78 |
Correct |
18 ms |
15104 KB |
n=2000 |
79 |
Correct |
20 ms |
15128 KB |
n=2000 |
80 |
Correct |
16 ms |
15232 KB |
n=2000 |
81 |
Correct |
16 ms |
15104 KB |
n=2000 |
82 |
Correct |
18 ms |
15104 KB |
n=2000 |
83 |
Correct |
19 ms |
15232 KB |
n=2000 |
84 |
Correct |
20 ms |
15104 KB |
n=2000 |
85 |
Correct |
17 ms |
15104 KB |
n=2000 |
86 |
Correct |
20 ms |
15104 KB |
n=2000 |
87 |
Correct |
20 ms |
15104 KB |
n=2000 |
88 |
Correct |
18 ms |
15232 KB |
n=2000 |
89 |
Correct |
16 ms |
15232 KB |
n=2000 |
90 |
Correct |
18 ms |
15232 KB |
n=2000 |
91 |
Correct |
21 ms |
15104 KB |
n=2000 |
92 |
Correct |
1414 ms |
82528 KB |
n=200000 |
93 |
Correct |
1244 ms |
87544 KB |
n=200000 |
94 |
Correct |
1085 ms |
90280 KB |
n=200000 |
95 |
Correct |
1272 ms |
82276 KB |
n=200000 |
96 |
Correct |
1266 ms |
82284 KB |
n=200000 |
97 |
Correct |
1376 ms |
86528 KB |
n=200000 |
98 |
Correct |
1312 ms |
82416 KB |
n=200000 |
99 |
Correct |
1389 ms |
82552 KB |
n=200000 |
100 |
Correct |
1439 ms |
82284 KB |
n=200000 |
101 |
Correct |
1000 ms |
91204 KB |
n=200000 |
102 |
Correct |
1095 ms |
83704 KB |
n=200000 |
103 |
Correct |
1104 ms |
83704 KB |
n=200000 |
104 |
Correct |
1095 ms |
83704 KB |
n=200000 |
105 |
Correct |
1070 ms |
84088 KB |
n=200000 |
106 |
Correct |
1100 ms |
84188 KB |
n=200000 |
107 |
Correct |
1063 ms |
84052 KB |
n=200000 |
108 |
Correct |
1335 ms |
82488 KB |
n=200000 |
109 |
Correct |
1371 ms |
82492 KB |
n=200000 |
110 |
Correct |
1329 ms |
82552 KB |
n=200000 |
111 |
Correct |
1267 ms |
81964 KB |
n=200000 |
112 |
Correct |
906 ms |
90488 KB |
n=200000 |
113 |
Correct |
1407 ms |
86528 KB |
n=200000 |
114 |
Correct |
1287 ms |
81892 KB |
n=200000 |
115 |
Correct |
1741 ms |
84444 KB |
n=200000 |
116 |
Correct |
1253 ms |
82428 KB |
n=200000 |
117 |
Correct |
922 ms |
90612 KB |
n=200000 |
118 |
Correct |
1505 ms |
85492 KB |
n=200000 |
119 |
Correct |
1305 ms |
82404 KB |
n=200000 |
120 |
Correct |
898 ms |
90100 KB |
n=200000 |
121 |
Correct |
843 ms |
90108 KB |
n=200000 |
122 |
Correct |
861 ms |
90436 KB |
n=200000 |
123 |
Correct |
1093 ms |
83832 KB |
n=200000 |
124 |
Correct |
340 ms |
33480 KB |
n=25264 |