#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
#define mid ((l+r)>>1)
const int mxn = 4e5+10;
int n,m,q;
pii dfn[mxn];
pii sparse[mxn][20];
int dep[mxn];
vector<int> tree[mxn];
vector<int> eul;
int arr[mxn];
int ppp = 0;
multiset<pair<int,pii>> segtree[mxn*4];
void dfs(int now,int par){
dfn[now].fs = eul.size();
eul.push_back(now);
for(auto nxt:tree[now]){
if(nxt == par)continue;
dep[nxt] = dep[now]+1;
dfs(nxt,now);
eul.push_back(now);
}
dfn[now].sc = eul.size();
eul.push_back(now);
}
void build(){
for(int i = 0;i<eul.size();i++){
sparse[i][0] = make_pair(dep[eul[i]],eul[i]);
}
for(int i = 1;i<20;i++){
for(int j = 0;j+(1<<i)<=eul.size();j++)sparse[j][i] = min(sparse[j][i-1],sparse[j+(1<<(i-1))][i-1]);
}
return;
}
int anc(int l,int r){
l = dfn[l].fs;
r = dfn[r].fs;
if(l>r)swap(l,r);
int d = r-l+1;
int h = __lg(d);
return min(sparse[l][h],sparse[r-(1<<h)+1][h]).sc;
}
void maketree(int now,int l,int r){
if(l == r){
segtree[now].insert(make_pair(arr[l],make_pair(l,l)));
return;
}
maketree(now*2+1,l,mid);
maketree(now*2+2,mid+1,r);
for(auto &i:segtree[now*2+1])segtree[now].insert(i);
for(auto &i:segtree[now*2+2])segtree[now].insert(i);
segtree[now].insert({anc(arr[mid],arr[mid+1]),{mid,mid+1}});
//cout<<now<<":"<<l<<' '<<r<<":";for(auto &i:segtree[now])cout<<i.fs<<',';cout<<endl;
return;
}
void modify(int now,int l,int r,int p,int v){
segtree[now].erase(segtree[now].find({arr[p],{p,p}}));
segtree[now].insert({v,{p,p}});
if(p != l){
segtree[now].erase(segtree[now].find({anc(arr[p-1],arr[p]),{p-1,p}}));
segtree[now].insert({anc(arr[p-1],v),{p-1,p}});
}
if(p != r){
segtree[now].erase(segtree[now].find({anc(arr[p],arr[p+1]),{p,p+1}}));
segtree[now].insert({anc(arr[p+1],v),{p,p+1}});
}
if(l == r)return;
if(mid>=p)modify(now*2+1,l,mid,p,v);
else modify(now*2+2,mid+1,r,p,v);
return;
}
pii getval(int now,int l,int r,int s,int e,int tar){
if(l>=s&&e>=r){
auto it = segtree[now].lower_bound({tar,{0,0}});
if(it != segtree[now].end()&&it->fs == tar){
return it->sc;
}
else return {-1,-1};
}
if(mid>=e)return getval(now*2+1,l,mid,s,e,tar);
else if(mid<s)return getval(now*2+2,mid+1,r,s,e,tar);
else{
if(anc(arr[mid],arr[mid+1]) == tar)return {mid,mid+1};
auto re = getval(now*2+1,l,mid,s,e,tar);
if(re.fs != -1)return re;
else return getval(now*2+2,mid+1,r,s,e,tar);
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m>>q;
for(int i = 1;i<n;i++){
int a,b;
cin>>a>>b;
tree[a].push_back(b);
tree[b].push_back(a);
}
for(int i = 1;i<=m;i++){
cin>>arr[i];
}
dfs(1,1);
build();
maketree(0,1,m);
//for(auto &i:segtree[0])cout<<i.fs<<' '<<i.sc.fs<<','<<i.sc.sc<<';';
while(q--){
int tp;
cin>>tp;
if(tp == 1){
int p,v;
cin>>p>>v;
modify(0,1,m,p,v);
arr[p] = v;
}
else{
int l,r,v;
cin>>l>>r>>v;
auto re = getval(0,1,m,l,r,v);
cout<<re.fs<<' '<<re.sc<<'\n';
}
}
return 0;
}
Compilation message
treearray.cpp: In function 'void build()':
treearray.cpp:37:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i = 0;i<eul.size();i++){
| ~^~~~~~~~~~~
treearray.cpp:41:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int j = 0;j+(1<<i)<=eul.size();j++)sparse[j][i] = min(sparse[j][i-1],sparse[j+(1<<(i-1))][i-1]);
| ~~~~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
90712 KB |
n=5 |
2 |
Correct |
18 ms |
90972 KB |
n=100 |
3 |
Correct |
18 ms |
90968 KB |
n=100 |
4 |
Correct |
18 ms |
90972 KB |
n=100 |
5 |
Correct |
18 ms |
90972 KB |
n=100 |
6 |
Correct |
17 ms |
90972 KB |
n=100 |
7 |
Correct |
18 ms |
91008 KB |
n=100 |
8 |
Correct |
17 ms |
91224 KB |
n=100 |
9 |
Correct |
20 ms |
90828 KB |
n=100 |
10 |
Correct |
20 ms |
90972 KB |
n=100 |
11 |
Correct |
18 ms |
90972 KB |
n=100 |
12 |
Correct |
18 ms |
90968 KB |
n=100 |
13 |
Correct |
17 ms |
90804 KB |
n=100 |
14 |
Correct |
20 ms |
90968 KB |
n=100 |
15 |
Correct |
18 ms |
90968 KB |
n=100 |
16 |
Correct |
18 ms |
90968 KB |
n=100 |
17 |
Correct |
17 ms |
90972 KB |
n=100 |
18 |
Correct |
17 ms |
90968 KB |
n=100 |
19 |
Correct |
17 ms |
90968 KB |
n=100 |
20 |
Correct |
18 ms |
91012 KB |
n=100 |
21 |
Correct |
20 ms |
90772 KB |
n=100 |
22 |
Correct |
18 ms |
90968 KB |
n=100 |
23 |
Correct |
18 ms |
90968 KB |
n=100 |
24 |
Correct |
17 ms |
91168 KB |
n=100 |
25 |
Correct |
18 ms |
90980 KB |
n=100 |
26 |
Correct |
18 ms |
90716 KB |
n=12 |
27 |
Correct |
18 ms |
90972 KB |
n=100 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
90712 KB |
n=5 |
2 |
Correct |
18 ms |
90972 KB |
n=100 |
3 |
Correct |
18 ms |
90968 KB |
n=100 |
4 |
Correct |
18 ms |
90972 KB |
n=100 |
5 |
Correct |
18 ms |
90972 KB |
n=100 |
6 |
Correct |
17 ms |
90972 KB |
n=100 |
7 |
Correct |
18 ms |
91008 KB |
n=100 |
8 |
Correct |
17 ms |
91224 KB |
n=100 |
9 |
Correct |
20 ms |
90828 KB |
n=100 |
10 |
Correct |
20 ms |
90972 KB |
n=100 |
11 |
Correct |
18 ms |
90972 KB |
n=100 |
12 |
Correct |
18 ms |
90968 KB |
n=100 |
13 |
Correct |
17 ms |
90804 KB |
n=100 |
14 |
Correct |
20 ms |
90968 KB |
n=100 |
15 |
Correct |
18 ms |
90968 KB |
n=100 |
16 |
Correct |
18 ms |
90968 KB |
n=100 |
17 |
Correct |
17 ms |
90972 KB |
n=100 |
18 |
Correct |
17 ms |
90968 KB |
n=100 |
19 |
Correct |
17 ms |
90968 KB |
n=100 |
20 |
Correct |
18 ms |
91012 KB |
n=100 |
21 |
Correct |
20 ms |
90772 KB |
n=100 |
22 |
Correct |
18 ms |
90968 KB |
n=100 |
23 |
Correct |
18 ms |
90968 KB |
n=100 |
24 |
Correct |
17 ms |
91168 KB |
n=100 |
25 |
Correct |
18 ms |
90980 KB |
n=100 |
26 |
Correct |
18 ms |
90716 KB |
n=12 |
27 |
Correct |
18 ms |
90972 KB |
n=100 |
28 |
Correct |
23 ms |
91732 KB |
n=500 |
29 |
Correct |
20 ms |
91532 KB |
n=500 |
30 |
Correct |
20 ms |
91480 KB |
n=500 |
31 |
Correct |
21 ms |
91480 KB |
n=500 |
32 |
Correct |
20 ms |
91484 KB |
n=500 |
33 |
Correct |
20 ms |
91484 KB |
n=500 |
34 |
Correct |
20 ms |
91484 KB |
n=500 |
35 |
Correct |
20 ms |
91484 KB |
n=500 |
36 |
Correct |
18 ms |
91484 KB |
n=500 |
37 |
Correct |
19 ms |
91344 KB |
n=500 |
38 |
Correct |
19 ms |
91520 KB |
n=500 |
39 |
Correct |
19 ms |
91484 KB |
n=500 |
40 |
Correct |
19 ms |
91484 KB |
n=500 |
41 |
Correct |
20 ms |
91480 KB |
n=500 |
42 |
Correct |
23 ms |
91740 KB |
n=500 |
43 |
Correct |
22 ms |
91484 KB |
n=500 |
44 |
Correct |
23 ms |
91480 KB |
n=500 |
45 |
Correct |
20 ms |
91484 KB |
n=500 |
46 |
Correct |
20 ms |
91484 KB |
n=500 |
47 |
Correct |
20 ms |
91484 KB |
n=500 |
48 |
Correct |
20 ms |
91484 KB |
n=500 |
49 |
Correct |
20 ms |
91624 KB |
n=500 |
50 |
Correct |
20 ms |
91480 KB |
n=500 |
51 |
Correct |
20 ms |
91484 KB |
n=500 |
52 |
Correct |
20 ms |
91480 KB |
n=500 |
53 |
Correct |
20 ms |
91520 KB |
n=500 |
54 |
Correct |
20 ms |
91484 KB |
n=500 |
55 |
Correct |
19 ms |
91228 KB |
n=278 |
56 |
Correct |
24 ms |
91532 KB |
n=500 |
57 |
Correct |
21 ms |
91484 KB |
n=500 |
58 |
Correct |
20 ms |
91480 KB |
n=500 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
90712 KB |
n=5 |
2 |
Correct |
18 ms |
90972 KB |
n=100 |
3 |
Correct |
18 ms |
90968 KB |
n=100 |
4 |
Correct |
18 ms |
90972 KB |
n=100 |
5 |
Correct |
18 ms |
90972 KB |
n=100 |
6 |
Correct |
17 ms |
90972 KB |
n=100 |
7 |
Correct |
18 ms |
91008 KB |
n=100 |
8 |
Correct |
17 ms |
91224 KB |
n=100 |
9 |
Correct |
20 ms |
90828 KB |
n=100 |
10 |
Correct |
20 ms |
90972 KB |
n=100 |
11 |
Correct |
18 ms |
90972 KB |
n=100 |
12 |
Correct |
18 ms |
90968 KB |
n=100 |
13 |
Correct |
17 ms |
90804 KB |
n=100 |
14 |
Correct |
20 ms |
90968 KB |
n=100 |
15 |
Correct |
18 ms |
90968 KB |
n=100 |
16 |
Correct |
18 ms |
90968 KB |
n=100 |
17 |
Correct |
17 ms |
90972 KB |
n=100 |
18 |
Correct |
17 ms |
90968 KB |
n=100 |
19 |
Correct |
17 ms |
90968 KB |
n=100 |
20 |
Correct |
18 ms |
91012 KB |
n=100 |
21 |
Correct |
20 ms |
90772 KB |
n=100 |
22 |
Correct |
18 ms |
90968 KB |
n=100 |
23 |
Correct |
18 ms |
90968 KB |
n=100 |
24 |
Correct |
17 ms |
91168 KB |
n=100 |
25 |
Correct |
18 ms |
90980 KB |
n=100 |
26 |
Correct |
18 ms |
90716 KB |
n=12 |
27 |
Correct |
18 ms |
90972 KB |
n=100 |
28 |
Correct |
23 ms |
91732 KB |
n=500 |
29 |
Correct |
20 ms |
91532 KB |
n=500 |
30 |
Correct |
20 ms |
91480 KB |
n=500 |
31 |
Correct |
21 ms |
91480 KB |
n=500 |
32 |
Correct |
20 ms |
91484 KB |
n=500 |
33 |
Correct |
20 ms |
91484 KB |
n=500 |
34 |
Correct |
20 ms |
91484 KB |
n=500 |
35 |
Correct |
20 ms |
91484 KB |
n=500 |
36 |
Correct |
18 ms |
91484 KB |
n=500 |
37 |
Correct |
19 ms |
91344 KB |
n=500 |
38 |
Correct |
19 ms |
91520 KB |
n=500 |
39 |
Correct |
19 ms |
91484 KB |
n=500 |
40 |
Correct |
19 ms |
91484 KB |
n=500 |
41 |
Correct |
20 ms |
91480 KB |
n=500 |
42 |
Correct |
23 ms |
91740 KB |
n=500 |
43 |
Correct |
22 ms |
91484 KB |
n=500 |
44 |
Correct |
23 ms |
91480 KB |
n=500 |
45 |
Correct |
20 ms |
91484 KB |
n=500 |
46 |
Correct |
20 ms |
91484 KB |
n=500 |
47 |
Correct |
20 ms |
91484 KB |
n=500 |
48 |
Correct |
20 ms |
91484 KB |
n=500 |
49 |
Correct |
20 ms |
91624 KB |
n=500 |
50 |
Correct |
20 ms |
91480 KB |
n=500 |
51 |
Correct |
20 ms |
91484 KB |
n=500 |
52 |
Correct |
20 ms |
91480 KB |
n=500 |
53 |
Correct |
20 ms |
91520 KB |
n=500 |
54 |
Correct |
20 ms |
91484 KB |
n=500 |
55 |
Correct |
19 ms |
91228 KB |
n=278 |
56 |
Correct |
24 ms |
91532 KB |
n=500 |
57 |
Correct |
21 ms |
91484 KB |
n=500 |
58 |
Correct |
20 ms |
91480 KB |
n=500 |
59 |
Correct |
32 ms |
95832 KB |
n=2000 |
60 |
Correct |
32 ms |
95940 KB |
n=2000 |
61 |
Correct |
38 ms |
95832 KB |
n=2000 |
62 |
Correct |
33 ms |
95836 KB |
n=2000 |
63 |
Correct |
31 ms |
95580 KB |
n=2000 |
64 |
Correct |
34 ms |
95836 KB |
n=2000 |
65 |
Correct |
31 ms |
95576 KB |
n=2000 |
66 |
Correct |
32 ms |
95832 KB |
n=2000 |
67 |
Correct |
34 ms |
96092 KB |
n=2000 |
68 |
Correct |
32 ms |
95836 KB |
n=2000 |
69 |
Correct |
24 ms |
95720 KB |
n=2000 |
70 |
Correct |
28 ms |
95676 KB |
n=2000 |
71 |
Correct |
25 ms |
95832 KB |
n=2000 |
72 |
Correct |
28 ms |
95964 KB |
n=2000 |
73 |
Correct |
26 ms |
95580 KB |
n=2000 |
74 |
Correct |
29 ms |
95580 KB |
n=1844 |
75 |
Correct |
26 ms |
95580 KB |
n=2000 |
76 |
Correct |
33 ms |
95836 KB |
n=2000 |
77 |
Correct |
32 ms |
95620 KB |
n=2000 |
78 |
Correct |
35 ms |
95836 KB |
n=2000 |
79 |
Correct |
32 ms |
95836 KB |
n=2000 |
80 |
Correct |
33 ms |
95892 KB |
n=2000 |
81 |
Correct |
38 ms |
95832 KB |
n=2000 |
82 |
Correct |
32 ms |
95836 KB |
n=2000 |
83 |
Correct |
34 ms |
95712 KB |
n=2000 |
84 |
Correct |
34 ms |
95576 KB |
n=2000 |
85 |
Correct |
34 ms |
95616 KB |
n=2000 |
86 |
Correct |
37 ms |
96092 KB |
n=2000 |
87 |
Correct |
31 ms |
95580 KB |
n=2000 |
88 |
Correct |
32 ms |
95836 KB |
n=2000 |
89 |
Correct |
33 ms |
95712 KB |
n=2000 |
90 |
Correct |
32 ms |
95832 KB |
n=2000 |
91 |
Correct |
25 ms |
95836 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
90712 KB |
n=5 |
2 |
Correct |
18 ms |
90972 KB |
n=100 |
3 |
Correct |
18 ms |
90968 KB |
n=100 |
4 |
Correct |
18 ms |
90972 KB |
n=100 |
5 |
Correct |
18 ms |
90972 KB |
n=100 |
6 |
Correct |
17 ms |
90972 KB |
n=100 |
7 |
Correct |
18 ms |
91008 KB |
n=100 |
8 |
Correct |
17 ms |
91224 KB |
n=100 |
9 |
Correct |
20 ms |
90828 KB |
n=100 |
10 |
Correct |
20 ms |
90972 KB |
n=100 |
11 |
Correct |
18 ms |
90972 KB |
n=100 |
12 |
Correct |
18 ms |
90968 KB |
n=100 |
13 |
Correct |
17 ms |
90804 KB |
n=100 |
14 |
Correct |
20 ms |
90968 KB |
n=100 |
15 |
Correct |
18 ms |
90968 KB |
n=100 |
16 |
Correct |
18 ms |
90968 KB |
n=100 |
17 |
Correct |
17 ms |
90972 KB |
n=100 |
18 |
Correct |
17 ms |
90968 KB |
n=100 |
19 |
Correct |
17 ms |
90968 KB |
n=100 |
20 |
Correct |
18 ms |
91012 KB |
n=100 |
21 |
Correct |
20 ms |
90772 KB |
n=100 |
22 |
Correct |
18 ms |
90968 KB |
n=100 |
23 |
Correct |
18 ms |
90968 KB |
n=100 |
24 |
Correct |
17 ms |
91168 KB |
n=100 |
25 |
Correct |
18 ms |
90980 KB |
n=100 |
26 |
Correct |
18 ms |
90716 KB |
n=12 |
27 |
Correct |
18 ms |
90972 KB |
n=100 |
28 |
Correct |
23 ms |
91732 KB |
n=500 |
29 |
Correct |
20 ms |
91532 KB |
n=500 |
30 |
Correct |
20 ms |
91480 KB |
n=500 |
31 |
Correct |
21 ms |
91480 KB |
n=500 |
32 |
Correct |
20 ms |
91484 KB |
n=500 |
33 |
Correct |
20 ms |
91484 KB |
n=500 |
34 |
Correct |
20 ms |
91484 KB |
n=500 |
35 |
Correct |
20 ms |
91484 KB |
n=500 |
36 |
Correct |
18 ms |
91484 KB |
n=500 |
37 |
Correct |
19 ms |
91344 KB |
n=500 |
38 |
Correct |
19 ms |
91520 KB |
n=500 |
39 |
Correct |
19 ms |
91484 KB |
n=500 |
40 |
Correct |
19 ms |
91484 KB |
n=500 |
41 |
Correct |
20 ms |
91480 KB |
n=500 |
42 |
Correct |
23 ms |
91740 KB |
n=500 |
43 |
Correct |
22 ms |
91484 KB |
n=500 |
44 |
Correct |
23 ms |
91480 KB |
n=500 |
45 |
Correct |
20 ms |
91484 KB |
n=500 |
46 |
Correct |
20 ms |
91484 KB |
n=500 |
47 |
Correct |
20 ms |
91484 KB |
n=500 |
48 |
Correct |
20 ms |
91484 KB |
n=500 |
49 |
Correct |
20 ms |
91624 KB |
n=500 |
50 |
Correct |
20 ms |
91480 KB |
n=500 |
51 |
Correct |
20 ms |
91484 KB |
n=500 |
52 |
Correct |
20 ms |
91480 KB |
n=500 |
53 |
Correct |
20 ms |
91520 KB |
n=500 |
54 |
Correct |
20 ms |
91484 KB |
n=500 |
55 |
Correct |
19 ms |
91228 KB |
n=278 |
56 |
Correct |
24 ms |
91532 KB |
n=500 |
57 |
Correct |
21 ms |
91484 KB |
n=500 |
58 |
Correct |
20 ms |
91480 KB |
n=500 |
59 |
Correct |
32 ms |
95832 KB |
n=2000 |
60 |
Correct |
32 ms |
95940 KB |
n=2000 |
61 |
Correct |
38 ms |
95832 KB |
n=2000 |
62 |
Correct |
33 ms |
95836 KB |
n=2000 |
63 |
Correct |
31 ms |
95580 KB |
n=2000 |
64 |
Correct |
34 ms |
95836 KB |
n=2000 |
65 |
Correct |
31 ms |
95576 KB |
n=2000 |
66 |
Correct |
32 ms |
95832 KB |
n=2000 |
67 |
Correct |
34 ms |
96092 KB |
n=2000 |
68 |
Correct |
32 ms |
95836 KB |
n=2000 |
69 |
Correct |
24 ms |
95720 KB |
n=2000 |
70 |
Correct |
28 ms |
95676 KB |
n=2000 |
71 |
Correct |
25 ms |
95832 KB |
n=2000 |
72 |
Correct |
28 ms |
95964 KB |
n=2000 |
73 |
Correct |
26 ms |
95580 KB |
n=2000 |
74 |
Correct |
29 ms |
95580 KB |
n=1844 |
75 |
Correct |
26 ms |
95580 KB |
n=2000 |
76 |
Correct |
33 ms |
95836 KB |
n=2000 |
77 |
Correct |
32 ms |
95620 KB |
n=2000 |
78 |
Correct |
35 ms |
95836 KB |
n=2000 |
79 |
Correct |
32 ms |
95836 KB |
n=2000 |
80 |
Correct |
33 ms |
95892 KB |
n=2000 |
81 |
Correct |
38 ms |
95832 KB |
n=2000 |
82 |
Correct |
32 ms |
95836 KB |
n=2000 |
83 |
Correct |
34 ms |
95712 KB |
n=2000 |
84 |
Correct |
34 ms |
95576 KB |
n=2000 |
85 |
Correct |
34 ms |
95616 KB |
n=2000 |
86 |
Correct |
37 ms |
96092 KB |
n=2000 |
87 |
Correct |
31 ms |
95580 KB |
n=2000 |
88 |
Correct |
32 ms |
95836 KB |
n=2000 |
89 |
Correct |
33 ms |
95712 KB |
n=2000 |
90 |
Correct |
32 ms |
95832 KB |
n=2000 |
91 |
Correct |
25 ms |
95836 KB |
n=2000 |
92 |
Runtime error |
2350 ms |
262144 KB |
Execution killed with signal 9 |
93 |
Halted |
0 ms |
0 KB |
- |