#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int maxn = 2e5+3;
vector <int> v[maxn];
int tin[maxn],tout[maxn];
vector <int> up[maxn];
int timer,i,j,p,q,n,m,k,l,Q,a[maxn];
void dfs(int u,int p)
{
int i;
tin[u]=++timer;
up[u][0]=p;
for(i=1;i<=l;i++)
{
up[u][i]=up[up[u][i-1]][i-1];
}
for(i=0;i<v[u].size();i++)
{
int q=v[u][i];
if(q!=p)
{
dfs(q,u);
}
}
tout[u]=++timer;
}
bool upper(int a,int b)
{
return (tin[a]<=tin[b] && tout[a]>=tout[b]);
}
int lca(int a,int b)
{
if(upper(a,b))return a;
if(upper(b,a))return b;
for(i=l;i>=0;i--)
{
if(!upper(up[a][i],b))
a=up[a][i];
}
return up[a][0];
}
set <int> s[maxn],s2[maxn];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m>>Q;
l=1;
while((1<<l)<=n)
l++;
for(i=1;i<n;i++)
{
cin>>p>>q;
v[p].push_back(q);
v[q].push_back(p);
}
for(i=0;i<=n+1;i++)
up[i].resize(l+10);
tout[0]=100000000;
dfs(1,1);
for(i=1;i<=m;i++)
cin>>a[i];
for(int i=1;i<=m;i++)
{
s2[a[i]].insert(i);
if(i+1<=m)
s[lca(a[i],a[i+1])].insert(i);
}
while(Q--)
{
int type;
cin>>type;
if(type==1)
{
cin>>p>>q;
s2[a[p]].erase(p);
if(p+1<=m)
s[lca(a[p],a[p+1])].erase(p);
if(p-1>=1)
s[lca(a[p-1],a[p])].erase(p-1);
s2[q].insert(p);
a[p] = q;
if(p+1<=m)
s[lca(a[p],a[p+1])].insert(p);
if(p-1>=1)
s[lca(a[p-1],a[p])].insert(p-1);
}
else
{
int l,r,vv;
cin>>l>>r>>vv;
auto it = s2[vv].lower_bound(l);
if(it!=s2[vv].end() && (*it)<=r)///nqma takuv ili ne vliza v [l;r]
{
cout<<*it<<" "<<*it<<endl;
continue;
}
else
{
it = s[vv].lower_bound(l);
if(it!=s[vv].end() && (*it)+1<=r)
{
cout<<*it<<" "<<(*it)+1<<endl;
continue;
}
}
cout<<"-1 -1"<<endl;
}
}
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
*/
Compilation message
treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:19:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(i=0;i<v[u].size();i++)
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
30808 KB |
n=5 |
2 |
Correct |
7 ms |
30812 KB |
n=100 |
3 |
Correct |
7 ms |
30812 KB |
n=100 |
4 |
Correct |
7 ms |
30808 KB |
n=100 |
5 |
Correct |
6 ms |
30812 KB |
n=100 |
6 |
Correct |
7 ms |
30812 KB |
n=100 |
7 |
Correct |
7 ms |
30812 KB |
n=100 |
8 |
Correct |
7 ms |
30812 KB |
n=100 |
9 |
Correct |
7 ms |
31008 KB |
n=100 |
10 |
Correct |
6 ms |
30812 KB |
n=100 |
11 |
Correct |
6 ms |
30812 KB |
n=100 |
12 |
Correct |
7 ms |
30812 KB |
n=100 |
13 |
Correct |
7 ms |
31064 KB |
n=100 |
14 |
Correct |
6 ms |
30812 KB |
n=100 |
15 |
Correct |
7 ms |
30812 KB |
n=100 |
16 |
Correct |
7 ms |
30812 KB |
n=100 |
17 |
Correct |
7 ms |
30812 KB |
n=100 |
18 |
Correct |
6 ms |
30812 KB |
n=100 |
19 |
Correct |
6 ms |
30812 KB |
n=100 |
20 |
Correct |
6 ms |
30812 KB |
n=100 |
21 |
Correct |
7 ms |
30812 KB |
n=100 |
22 |
Correct |
7 ms |
30812 KB |
n=100 |
23 |
Correct |
7 ms |
30808 KB |
n=100 |
24 |
Correct |
7 ms |
30812 KB |
n=100 |
25 |
Correct |
6 ms |
30808 KB |
n=100 |
26 |
Correct |
6 ms |
30812 KB |
n=12 |
27 |
Correct |
6 ms |
30812 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
30808 KB |
n=5 |
2 |
Correct |
7 ms |
30812 KB |
n=100 |
3 |
Correct |
7 ms |
30812 KB |
n=100 |
4 |
Correct |
7 ms |
30808 KB |
n=100 |
5 |
Correct |
6 ms |
30812 KB |
n=100 |
6 |
Correct |
7 ms |
30812 KB |
n=100 |
7 |
Correct |
7 ms |
30812 KB |
n=100 |
8 |
Correct |
7 ms |
30812 KB |
n=100 |
9 |
Correct |
7 ms |
31008 KB |
n=100 |
10 |
Correct |
6 ms |
30812 KB |
n=100 |
11 |
Correct |
6 ms |
30812 KB |
n=100 |
12 |
Correct |
7 ms |
30812 KB |
n=100 |
13 |
Correct |
7 ms |
31064 KB |
n=100 |
14 |
Correct |
6 ms |
30812 KB |
n=100 |
15 |
Correct |
7 ms |
30812 KB |
n=100 |
16 |
Correct |
7 ms |
30812 KB |
n=100 |
17 |
Correct |
7 ms |
30812 KB |
n=100 |
18 |
Correct |
6 ms |
30812 KB |
n=100 |
19 |
Correct |
6 ms |
30812 KB |
n=100 |
20 |
Correct |
6 ms |
30812 KB |
n=100 |
21 |
Correct |
7 ms |
30812 KB |
n=100 |
22 |
Correct |
7 ms |
30812 KB |
n=100 |
23 |
Correct |
7 ms |
30808 KB |
n=100 |
24 |
Correct |
7 ms |
30812 KB |
n=100 |
25 |
Correct |
6 ms |
30808 KB |
n=100 |
26 |
Correct |
6 ms |
30812 KB |
n=12 |
27 |
Correct |
6 ms |
30812 KB |
n=100 |
28 |
Correct |
7 ms |
31064 KB |
n=500 |
29 |
Correct |
7 ms |
31068 KB |
n=500 |
30 |
Correct |
7 ms |
31120 KB |
n=500 |
31 |
Correct |
7 ms |
31068 KB |
n=500 |
32 |
Correct |
7 ms |
31068 KB |
n=500 |
33 |
Correct |
7 ms |
31068 KB |
n=500 |
34 |
Correct |
7 ms |
31068 KB |
n=500 |
35 |
Correct |
7 ms |
31068 KB |
n=500 |
36 |
Correct |
7 ms |
31068 KB |
n=500 |
37 |
Correct |
8 ms |
31068 KB |
n=500 |
38 |
Correct |
7 ms |
31064 KB |
n=500 |
39 |
Correct |
7 ms |
31068 KB |
n=500 |
40 |
Correct |
8 ms |
31068 KB |
n=500 |
41 |
Correct |
6 ms |
31064 KB |
n=500 |
42 |
Correct |
7 ms |
31320 KB |
n=500 |
43 |
Correct |
7 ms |
31068 KB |
n=500 |
44 |
Correct |
8 ms |
31068 KB |
n=500 |
45 |
Correct |
7 ms |
31068 KB |
n=500 |
46 |
Correct |
7 ms |
31068 KB |
n=500 |
47 |
Correct |
6 ms |
31068 KB |
n=500 |
48 |
Correct |
7 ms |
31064 KB |
n=500 |
49 |
Correct |
8 ms |
31064 KB |
n=500 |
50 |
Correct |
7 ms |
31068 KB |
n=500 |
51 |
Correct |
7 ms |
31068 KB |
n=500 |
52 |
Correct |
7 ms |
31068 KB |
n=500 |
53 |
Correct |
8 ms |
30956 KB |
n=500 |
54 |
Correct |
7 ms |
31068 KB |
n=500 |
55 |
Correct |
6 ms |
31068 KB |
n=278 |
56 |
Correct |
8 ms |
31068 KB |
n=500 |
57 |
Correct |
7 ms |
31068 KB |
n=500 |
58 |
Correct |
7 ms |
31068 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
30808 KB |
n=5 |
2 |
Correct |
7 ms |
30812 KB |
n=100 |
3 |
Correct |
7 ms |
30812 KB |
n=100 |
4 |
Correct |
7 ms |
30808 KB |
n=100 |
5 |
Correct |
6 ms |
30812 KB |
n=100 |
6 |
Correct |
7 ms |
30812 KB |
n=100 |
7 |
Correct |
7 ms |
30812 KB |
n=100 |
8 |
Correct |
7 ms |
30812 KB |
n=100 |
9 |
Correct |
7 ms |
31008 KB |
n=100 |
10 |
Correct |
6 ms |
30812 KB |
n=100 |
11 |
Correct |
6 ms |
30812 KB |
n=100 |
12 |
Correct |
7 ms |
30812 KB |
n=100 |
13 |
Correct |
7 ms |
31064 KB |
n=100 |
14 |
Correct |
6 ms |
30812 KB |
n=100 |
15 |
Correct |
7 ms |
30812 KB |
n=100 |
16 |
Correct |
7 ms |
30812 KB |
n=100 |
17 |
Correct |
7 ms |
30812 KB |
n=100 |
18 |
Correct |
6 ms |
30812 KB |
n=100 |
19 |
Correct |
6 ms |
30812 KB |
n=100 |
20 |
Correct |
6 ms |
30812 KB |
n=100 |
21 |
Correct |
7 ms |
30812 KB |
n=100 |
22 |
Correct |
7 ms |
30812 KB |
n=100 |
23 |
Correct |
7 ms |
30808 KB |
n=100 |
24 |
Correct |
7 ms |
30812 KB |
n=100 |
25 |
Correct |
6 ms |
30808 KB |
n=100 |
26 |
Correct |
6 ms |
30812 KB |
n=12 |
27 |
Correct |
6 ms |
30812 KB |
n=100 |
28 |
Correct |
7 ms |
31064 KB |
n=500 |
29 |
Correct |
7 ms |
31068 KB |
n=500 |
30 |
Correct |
7 ms |
31120 KB |
n=500 |
31 |
Correct |
7 ms |
31068 KB |
n=500 |
32 |
Correct |
7 ms |
31068 KB |
n=500 |
33 |
Correct |
7 ms |
31068 KB |
n=500 |
34 |
Correct |
7 ms |
31068 KB |
n=500 |
35 |
Correct |
7 ms |
31068 KB |
n=500 |
36 |
Correct |
7 ms |
31068 KB |
n=500 |
37 |
Correct |
8 ms |
31068 KB |
n=500 |
38 |
Correct |
7 ms |
31064 KB |
n=500 |
39 |
Correct |
7 ms |
31068 KB |
n=500 |
40 |
Correct |
8 ms |
31068 KB |
n=500 |
41 |
Correct |
6 ms |
31064 KB |
n=500 |
42 |
Correct |
7 ms |
31320 KB |
n=500 |
43 |
Correct |
7 ms |
31068 KB |
n=500 |
44 |
Correct |
8 ms |
31068 KB |
n=500 |
45 |
Correct |
7 ms |
31068 KB |
n=500 |
46 |
Correct |
7 ms |
31068 KB |
n=500 |
47 |
Correct |
6 ms |
31068 KB |
n=500 |
48 |
Correct |
7 ms |
31064 KB |
n=500 |
49 |
Correct |
8 ms |
31064 KB |
n=500 |
50 |
Correct |
7 ms |
31068 KB |
n=500 |
51 |
Correct |
7 ms |
31068 KB |
n=500 |
52 |
Correct |
7 ms |
31068 KB |
n=500 |
53 |
Correct |
8 ms |
30956 KB |
n=500 |
54 |
Correct |
7 ms |
31068 KB |
n=500 |
55 |
Correct |
6 ms |
31068 KB |
n=278 |
56 |
Correct |
8 ms |
31068 KB |
n=500 |
57 |
Correct |
7 ms |
31068 KB |
n=500 |
58 |
Correct |
7 ms |
31068 KB |
n=500 |
59 |
Correct |
9 ms |
31320 KB |
n=2000 |
60 |
Correct |
10 ms |
31576 KB |
n=2000 |
61 |
Correct |
9 ms |
31324 KB |
n=2000 |
62 |
Correct |
9 ms |
31324 KB |
n=2000 |
63 |
Correct |
9 ms |
31320 KB |
n=2000 |
64 |
Correct |
9 ms |
31324 KB |
n=2000 |
65 |
Correct |
9 ms |
31324 KB |
n=2000 |
66 |
Correct |
8 ms |
31324 KB |
n=2000 |
67 |
Correct |
9 ms |
31324 KB |
n=2000 |
68 |
Correct |
10 ms |
31320 KB |
n=2000 |
69 |
Correct |
9 ms |
31324 KB |
n=2000 |
70 |
Correct |
9 ms |
31320 KB |
n=2000 |
71 |
Correct |
8 ms |
31324 KB |
n=2000 |
72 |
Correct |
8 ms |
31520 KB |
n=2000 |
73 |
Correct |
9 ms |
31320 KB |
n=2000 |
74 |
Correct |
9 ms |
31324 KB |
n=1844 |
75 |
Correct |
8 ms |
31324 KB |
n=2000 |
76 |
Correct |
9 ms |
31324 KB |
n=2000 |
77 |
Correct |
9 ms |
31324 KB |
n=2000 |
78 |
Correct |
9 ms |
31324 KB |
n=2000 |
79 |
Correct |
9 ms |
31324 KB |
n=2000 |
80 |
Correct |
8 ms |
31576 KB |
n=2000 |
81 |
Correct |
9 ms |
31528 KB |
n=2000 |
82 |
Correct |
9 ms |
31320 KB |
n=2000 |
83 |
Correct |
8 ms |
31324 KB |
n=2000 |
84 |
Correct |
9 ms |
31324 KB |
n=2000 |
85 |
Correct |
9 ms |
31324 KB |
n=2000 |
86 |
Correct |
9 ms |
31276 KB |
n=2000 |
87 |
Correct |
8 ms |
31320 KB |
n=2000 |
88 |
Correct |
9 ms |
31576 KB |
n=2000 |
89 |
Correct |
8 ms |
31580 KB |
n=2000 |
90 |
Correct |
9 ms |
31580 KB |
n=2000 |
91 |
Correct |
9 ms |
31324 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
30808 KB |
n=5 |
2 |
Correct |
7 ms |
30812 KB |
n=100 |
3 |
Correct |
7 ms |
30812 KB |
n=100 |
4 |
Correct |
7 ms |
30808 KB |
n=100 |
5 |
Correct |
6 ms |
30812 KB |
n=100 |
6 |
Correct |
7 ms |
30812 KB |
n=100 |
7 |
Correct |
7 ms |
30812 KB |
n=100 |
8 |
Correct |
7 ms |
30812 KB |
n=100 |
9 |
Correct |
7 ms |
31008 KB |
n=100 |
10 |
Correct |
6 ms |
30812 KB |
n=100 |
11 |
Correct |
6 ms |
30812 KB |
n=100 |
12 |
Correct |
7 ms |
30812 KB |
n=100 |
13 |
Correct |
7 ms |
31064 KB |
n=100 |
14 |
Correct |
6 ms |
30812 KB |
n=100 |
15 |
Correct |
7 ms |
30812 KB |
n=100 |
16 |
Correct |
7 ms |
30812 KB |
n=100 |
17 |
Correct |
7 ms |
30812 KB |
n=100 |
18 |
Correct |
6 ms |
30812 KB |
n=100 |
19 |
Correct |
6 ms |
30812 KB |
n=100 |
20 |
Correct |
6 ms |
30812 KB |
n=100 |
21 |
Correct |
7 ms |
30812 KB |
n=100 |
22 |
Correct |
7 ms |
30812 KB |
n=100 |
23 |
Correct |
7 ms |
30808 KB |
n=100 |
24 |
Correct |
7 ms |
30812 KB |
n=100 |
25 |
Correct |
6 ms |
30808 KB |
n=100 |
26 |
Correct |
6 ms |
30812 KB |
n=12 |
27 |
Correct |
6 ms |
30812 KB |
n=100 |
28 |
Correct |
7 ms |
31064 KB |
n=500 |
29 |
Correct |
7 ms |
31068 KB |
n=500 |
30 |
Correct |
7 ms |
31120 KB |
n=500 |
31 |
Correct |
7 ms |
31068 KB |
n=500 |
32 |
Correct |
7 ms |
31068 KB |
n=500 |
33 |
Correct |
7 ms |
31068 KB |
n=500 |
34 |
Correct |
7 ms |
31068 KB |
n=500 |
35 |
Correct |
7 ms |
31068 KB |
n=500 |
36 |
Correct |
7 ms |
31068 KB |
n=500 |
37 |
Correct |
8 ms |
31068 KB |
n=500 |
38 |
Correct |
7 ms |
31064 KB |
n=500 |
39 |
Correct |
7 ms |
31068 KB |
n=500 |
40 |
Correct |
8 ms |
31068 KB |
n=500 |
41 |
Correct |
6 ms |
31064 KB |
n=500 |
42 |
Correct |
7 ms |
31320 KB |
n=500 |
43 |
Correct |
7 ms |
31068 KB |
n=500 |
44 |
Correct |
8 ms |
31068 KB |
n=500 |
45 |
Correct |
7 ms |
31068 KB |
n=500 |
46 |
Correct |
7 ms |
31068 KB |
n=500 |
47 |
Correct |
6 ms |
31068 KB |
n=500 |
48 |
Correct |
7 ms |
31064 KB |
n=500 |
49 |
Correct |
8 ms |
31064 KB |
n=500 |
50 |
Correct |
7 ms |
31068 KB |
n=500 |
51 |
Correct |
7 ms |
31068 KB |
n=500 |
52 |
Correct |
7 ms |
31068 KB |
n=500 |
53 |
Correct |
8 ms |
30956 KB |
n=500 |
54 |
Correct |
7 ms |
31068 KB |
n=500 |
55 |
Correct |
6 ms |
31068 KB |
n=278 |
56 |
Correct |
8 ms |
31068 KB |
n=500 |
57 |
Correct |
7 ms |
31068 KB |
n=500 |
58 |
Correct |
7 ms |
31068 KB |
n=500 |
59 |
Correct |
9 ms |
31320 KB |
n=2000 |
60 |
Correct |
10 ms |
31576 KB |
n=2000 |
61 |
Correct |
9 ms |
31324 KB |
n=2000 |
62 |
Correct |
9 ms |
31324 KB |
n=2000 |
63 |
Correct |
9 ms |
31320 KB |
n=2000 |
64 |
Correct |
9 ms |
31324 KB |
n=2000 |
65 |
Correct |
9 ms |
31324 KB |
n=2000 |
66 |
Correct |
8 ms |
31324 KB |
n=2000 |
67 |
Correct |
9 ms |
31324 KB |
n=2000 |
68 |
Correct |
10 ms |
31320 KB |
n=2000 |
69 |
Correct |
9 ms |
31324 KB |
n=2000 |
70 |
Correct |
9 ms |
31320 KB |
n=2000 |
71 |
Correct |
8 ms |
31324 KB |
n=2000 |
72 |
Correct |
8 ms |
31520 KB |
n=2000 |
73 |
Correct |
9 ms |
31320 KB |
n=2000 |
74 |
Correct |
9 ms |
31324 KB |
n=1844 |
75 |
Correct |
8 ms |
31324 KB |
n=2000 |
76 |
Correct |
9 ms |
31324 KB |
n=2000 |
77 |
Correct |
9 ms |
31324 KB |
n=2000 |
78 |
Correct |
9 ms |
31324 KB |
n=2000 |
79 |
Correct |
9 ms |
31324 KB |
n=2000 |
80 |
Correct |
8 ms |
31576 KB |
n=2000 |
81 |
Correct |
9 ms |
31528 KB |
n=2000 |
82 |
Correct |
9 ms |
31320 KB |
n=2000 |
83 |
Correct |
8 ms |
31324 KB |
n=2000 |
84 |
Correct |
9 ms |
31324 KB |
n=2000 |
85 |
Correct |
9 ms |
31324 KB |
n=2000 |
86 |
Correct |
9 ms |
31276 KB |
n=2000 |
87 |
Correct |
8 ms |
31320 KB |
n=2000 |
88 |
Correct |
9 ms |
31576 KB |
n=2000 |
89 |
Correct |
8 ms |
31580 KB |
n=2000 |
90 |
Correct |
9 ms |
31580 KB |
n=2000 |
91 |
Correct |
9 ms |
31324 KB |
n=2000 |
92 |
Correct |
618 ms |
89324 KB |
n=200000 |
93 |
Correct |
650 ms |
95832 KB |
n=200000 |
94 |
Correct |
465 ms |
100436 KB |
n=200000 |
95 |
Correct |
650 ms |
89532 KB |
n=200000 |
96 |
Correct |
543 ms |
89300 KB |
n=200000 |
97 |
Correct |
783 ms |
94680 KB |
n=200000 |
98 |
Correct |
571 ms |
89164 KB |
n=200000 |
99 |
Correct |
682 ms |
89424 KB |
n=200000 |
100 |
Correct |
618 ms |
89316 KB |
n=200000 |
101 |
Correct |
434 ms |
102356 KB |
n=200000 |
102 |
Correct |
416 ms |
90392 KB |
n=200000 |
103 |
Correct |
350 ms |
90332 KB |
n=200000 |
104 |
Correct |
379 ms |
90452 KB |
n=200000 |
105 |
Correct |
374 ms |
90804 KB |
n=200000 |
106 |
Correct |
365 ms |
90880 KB |
n=200000 |
107 |
Correct |
376 ms |
90976 KB |
n=200000 |
108 |
Correct |
674 ms |
89424 KB |
n=200000 |
109 |
Correct |
643 ms |
89344 KB |
n=200000 |
110 |
Correct |
673 ms |
89372 KB |
n=200000 |
111 |
Correct |
657 ms |
88700 KB |
n=200000 |
112 |
Correct |
489 ms |
100696 KB |
n=200000 |
113 |
Correct |
769 ms |
94556 KB |
n=200000 |
114 |
Correct |
573 ms |
88896 KB |
n=200000 |
115 |
Correct |
865 ms |
92072 KB |
n=200000 |
116 |
Correct |
591 ms |
89800 KB |
n=200000 |
117 |
Correct |
446 ms |
101268 KB |
n=200000 |
118 |
Correct |
857 ms |
93316 KB |
n=200000 |
119 |
Correct |
613 ms |
89328 KB |
n=200000 |
120 |
Correct |
414 ms |
100988 KB |
n=200000 |
121 |
Correct |
399 ms |
101212 KB |
n=200000 |
122 |
Correct |
357 ms |
101488 KB |
n=200000 |
123 |
Correct |
391 ms |
90640 KB |
n=200000 |
124 |
Correct |
96 ms |
46524 KB |
n=25264 |