답안 #492157

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
492157 2021-12-05T18:31:21 Z infertechno2 Birthday gift (IZhO18_treearray) C++17
100 / 100
944 ms 102136 KB
#include <bits/stdc++.h>

using namespace std;

typedef int ll;

const ll Size=4e5+10;
ll n,m,q;
ll tin[Size],height[Size],lca_tree[10*Size],visited[Size],a[Size];
set<ll> all_pairs[Size],all_singles[Size];
vector<ll> adj[Size],euler_tour;

void dfs(ll curr_node,ll depth){
    tin[curr_node]=euler_tour.size();
    height[curr_node]=depth;
    visited[curr_node]=1;
    euler_tour.push_back(curr_node);
    for(auto itr:adj[curr_node]){
        if(!visited[itr]){
            dfs(itr,depth+1);
            euler_tour.push_back(curr_node);
        }
    }
}

void build_lca(ll l,ll r,ll i){
    if(l==r){
        lca_tree[i]=euler_tour[r];
    }else{
        ll mid=(l+r)/2;
        build_lca(l,mid,i*2);
        build_lca(mid+1,r,i*2+1);
        height[lca_tree[i*2]]<height[lca_tree[i*2+1]] ? lca_tree[i]=lca_tree[i*2] : lca_tree[i]=lca_tree[i*2+1];
    }
}

ll query_lca(ll l,ll r,ll i,ll tl,ll tr){
    if(r<tl or l>tr){
        return -1;
    }
    if(l==tl and r==tr){
        return lca_tree[i];
    }else{
        ll mid=(l+r)/2;
        ll n1=query_lca(l,mid,i*2,tl,min(mid,tr));
        ll n2=query_lca(mid+1,r,i*2+1,max(tl,mid+1),tr);
        if(n1==-1)return n2;
        if(n2==-1)return n1;
        return height[n1]<height[n2] ? n1 : n2;
    }
}

ll lca(ll a,ll b){
    if(a>n or a<1 or b>n or b<0)return 0;
    if(tin[a]>tin[b]) swap(a,b);
    return query_lca(0,euler_tour.size()-1,1,tin[a],tin[b]);
}

void solve(){
    cin>>n>>m>>q;
    for(ll i=0;i<n-1;i++){
        ll from,to;
        cin>>from>>to;
        adj[from].push_back(to);
        adj[to].push_back(from);
    }
    for(ll i=1;i<=m;i++){
        cin>>a[i];
    }
    dfs(1,1);
    build_lca(0,euler_tour.size()-1,1);
    for(ll i=1;i<=m;i++){
        ll Lca=lca(a[i],a[i+1]);
        all_pairs[Lca].insert(i);
        all_singles[a[i]].insert(i);
    }
    for(ll i=0;i<q;i++){
        ll type;
        cin>>type;
        if(type==1){
            ll pos,val;
            cin>>pos>>val;
            ll lca1=lca(a[pos-1],a[pos]);
            ll lca2=lca(a[pos],a[pos+1]);
            ll new_lca1=lca(a[pos-1],val);
            ll new_lca2=lca(val,a[pos+1]);
            all_singles[a[pos]].erase(pos);
            a[pos]=val;
            all_singles[a[pos]].insert(pos);
            all_pairs[lca1].erase(pos-1); all_pairs[lca2].erase(pos);
            all_pairs[new_lca1].insert(pos-1); all_pairs[new_lca2].insert(pos);
        }
        if(type==2){
            ll l,r,v;
            cin>>l>>r>>v;
            auto pair_found=all_pairs[v].lower_bound(l);
            auto single_found=all_singles[v].lower_bound(l);
            if(single_found!=all_singles[v].end() and *single_found<=r){
                cout<<*single_found<<" "<<*single_found<<"\n";
            }else{
                if(pair_found!=all_pairs[v].end() and *pair_found<r){
                    cout<<*pair_found<<" "<<*pair_found+1<<"\n";
                }else{
                    cout<<-1<<" "<<-1<<"\n";
                }
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t=1;
    while(t--){
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 47308 KB n=5
2 Correct 26 ms 47336 KB n=100
3 Correct 21 ms 47308 KB n=100
4 Correct 22 ms 47304 KB n=100
5 Correct 22 ms 47308 KB n=100
6 Correct 22 ms 47196 KB n=100
7 Correct 22 ms 47308 KB n=100
8 Correct 22 ms 47308 KB n=100
9 Correct 21 ms 47312 KB n=100
10 Correct 22 ms 47308 KB n=100
11 Correct 21 ms 47316 KB n=100
12 Correct 22 ms 47332 KB n=100
13 Correct 22 ms 47224 KB n=100
14 Correct 22 ms 47308 KB n=100
15 Correct 25 ms 47412 KB n=100
16 Correct 22 ms 47308 KB n=100
17 Correct 22 ms 47240 KB n=100
18 Correct 22 ms 47292 KB n=100
19 Correct 22 ms 47252 KB n=100
20 Correct 22 ms 47308 KB n=100
21 Correct 22 ms 47332 KB n=100
22 Correct 22 ms 47308 KB n=100
23 Correct 22 ms 47236 KB n=100
24 Correct 25 ms 47252 KB n=100
25 Correct 21 ms 47324 KB n=100
26 Correct 22 ms 47320 KB n=12
27 Correct 27 ms 47312 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 47308 KB n=5
2 Correct 26 ms 47336 KB n=100
3 Correct 21 ms 47308 KB n=100
4 Correct 22 ms 47304 KB n=100
5 Correct 22 ms 47308 KB n=100
6 Correct 22 ms 47196 KB n=100
7 Correct 22 ms 47308 KB n=100
8 Correct 22 ms 47308 KB n=100
9 Correct 21 ms 47312 KB n=100
10 Correct 22 ms 47308 KB n=100
11 Correct 21 ms 47316 KB n=100
12 Correct 22 ms 47332 KB n=100
13 Correct 22 ms 47224 KB n=100
14 Correct 22 ms 47308 KB n=100
15 Correct 25 ms 47412 KB n=100
16 Correct 22 ms 47308 KB n=100
17 Correct 22 ms 47240 KB n=100
18 Correct 22 ms 47292 KB n=100
19 Correct 22 ms 47252 KB n=100
20 Correct 22 ms 47308 KB n=100
21 Correct 22 ms 47332 KB n=100
22 Correct 22 ms 47308 KB n=100
23 Correct 22 ms 47236 KB n=100
24 Correct 25 ms 47252 KB n=100
25 Correct 21 ms 47324 KB n=100
26 Correct 22 ms 47320 KB n=12
27 Correct 27 ms 47312 KB n=100
28 Correct 22 ms 47308 KB n=500
29 Correct 23 ms 47308 KB n=500
30 Correct 23 ms 47368 KB n=500
31 Correct 22 ms 47320 KB n=500
32 Correct 24 ms 47288 KB n=500
33 Correct 23 ms 47308 KB n=500
34 Correct 23 ms 47332 KB n=500
35 Correct 23 ms 47332 KB n=500
36 Correct 22 ms 47292 KB n=500
37 Correct 23 ms 47324 KB n=500
38 Correct 23 ms 47288 KB n=500
39 Correct 26 ms 47300 KB n=500
40 Correct 22 ms 47308 KB n=500
41 Correct 23 ms 47308 KB n=500
42 Correct 25 ms 47400 KB n=500
43 Correct 24 ms 47340 KB n=500
44 Correct 23 ms 47308 KB n=500
45 Correct 22 ms 47312 KB n=500
46 Correct 23 ms 47352 KB n=500
47 Correct 22 ms 47308 KB n=500
48 Correct 23 ms 47436 KB n=500
49 Correct 23 ms 47384 KB n=500
50 Correct 25 ms 47384 KB n=500
51 Correct 23 ms 47332 KB n=500
52 Correct 22 ms 47368 KB n=500
53 Correct 23 ms 47308 KB n=500
54 Correct 23 ms 47308 KB n=500
55 Correct 22 ms 47308 KB n=278
56 Correct 23 ms 47376 KB n=500
57 Correct 23 ms 47308 KB n=500
58 Correct 26 ms 47380 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 47308 KB n=5
2 Correct 26 ms 47336 KB n=100
3 Correct 21 ms 47308 KB n=100
4 Correct 22 ms 47304 KB n=100
5 Correct 22 ms 47308 KB n=100
6 Correct 22 ms 47196 KB n=100
7 Correct 22 ms 47308 KB n=100
8 Correct 22 ms 47308 KB n=100
9 Correct 21 ms 47312 KB n=100
10 Correct 22 ms 47308 KB n=100
11 Correct 21 ms 47316 KB n=100
12 Correct 22 ms 47332 KB n=100
13 Correct 22 ms 47224 KB n=100
14 Correct 22 ms 47308 KB n=100
15 Correct 25 ms 47412 KB n=100
16 Correct 22 ms 47308 KB n=100
17 Correct 22 ms 47240 KB n=100
18 Correct 22 ms 47292 KB n=100
19 Correct 22 ms 47252 KB n=100
20 Correct 22 ms 47308 KB n=100
21 Correct 22 ms 47332 KB n=100
22 Correct 22 ms 47308 KB n=100
23 Correct 22 ms 47236 KB n=100
24 Correct 25 ms 47252 KB n=100
25 Correct 21 ms 47324 KB n=100
26 Correct 22 ms 47320 KB n=12
27 Correct 27 ms 47312 KB n=100
28 Correct 22 ms 47308 KB n=500
29 Correct 23 ms 47308 KB n=500
30 Correct 23 ms 47368 KB n=500
31 Correct 22 ms 47320 KB n=500
32 Correct 24 ms 47288 KB n=500
33 Correct 23 ms 47308 KB n=500
34 Correct 23 ms 47332 KB n=500
35 Correct 23 ms 47332 KB n=500
36 Correct 22 ms 47292 KB n=500
37 Correct 23 ms 47324 KB n=500
38 Correct 23 ms 47288 KB n=500
39 Correct 26 ms 47300 KB n=500
40 Correct 22 ms 47308 KB n=500
41 Correct 23 ms 47308 KB n=500
42 Correct 25 ms 47400 KB n=500
43 Correct 24 ms 47340 KB n=500
44 Correct 23 ms 47308 KB n=500
45 Correct 22 ms 47312 KB n=500
46 Correct 23 ms 47352 KB n=500
47 Correct 22 ms 47308 KB n=500
48 Correct 23 ms 47436 KB n=500
49 Correct 23 ms 47384 KB n=500
50 Correct 25 ms 47384 KB n=500
51 Correct 23 ms 47332 KB n=500
52 Correct 22 ms 47368 KB n=500
53 Correct 23 ms 47308 KB n=500
54 Correct 23 ms 47308 KB n=500
55 Correct 22 ms 47308 KB n=278
56 Correct 23 ms 47376 KB n=500
57 Correct 23 ms 47308 KB n=500
58 Correct 26 ms 47380 KB n=500
59 Correct 27 ms 47632 KB n=2000
60 Correct 28 ms 47768 KB n=2000
61 Correct 27 ms 47652 KB n=2000
62 Correct 27 ms 47668 KB n=2000
63 Correct 26 ms 47564 KB n=2000
64 Correct 29 ms 47708 KB n=2000
65 Correct 26 ms 47564 KB n=2000
66 Correct 25 ms 47680 KB n=2000
67 Correct 26 ms 47648 KB n=2000
68 Correct 25 ms 47692 KB n=2000
69 Correct 24 ms 47580 KB n=2000
70 Correct 24 ms 47564 KB n=2000
71 Correct 24 ms 47680 KB n=2000
72 Correct 24 ms 47620 KB n=2000
73 Correct 25 ms 47564 KB n=2000
74 Correct 29 ms 47564 KB n=1844
75 Correct 24 ms 47664 KB n=2000
76 Correct 26 ms 47656 KB n=2000
77 Correct 26 ms 47532 KB n=2000
78 Correct 26 ms 47552 KB n=2000
79 Correct 26 ms 47564 KB n=2000
80 Correct 25 ms 47716 KB n=2000
81 Correct 26 ms 47692 KB n=2000
82 Correct 25 ms 47564 KB n=2000
83 Correct 27 ms 47716 KB n=2000
84 Correct 28 ms 47668 KB n=2000
85 Correct 27 ms 47692 KB n=2000
86 Correct 26 ms 47628 KB n=2000
87 Correct 26 ms 47564 KB n=2000
88 Correct 25 ms 47692 KB n=2000
89 Correct 25 ms 47652 KB n=2000
90 Correct 25 ms 47772 KB n=2000
91 Correct 25 ms 47564 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 47308 KB n=5
2 Correct 26 ms 47336 KB n=100
3 Correct 21 ms 47308 KB n=100
4 Correct 22 ms 47304 KB n=100
5 Correct 22 ms 47308 KB n=100
6 Correct 22 ms 47196 KB n=100
7 Correct 22 ms 47308 KB n=100
8 Correct 22 ms 47308 KB n=100
9 Correct 21 ms 47312 KB n=100
10 Correct 22 ms 47308 KB n=100
11 Correct 21 ms 47316 KB n=100
12 Correct 22 ms 47332 KB n=100
13 Correct 22 ms 47224 KB n=100
14 Correct 22 ms 47308 KB n=100
15 Correct 25 ms 47412 KB n=100
16 Correct 22 ms 47308 KB n=100
17 Correct 22 ms 47240 KB n=100
18 Correct 22 ms 47292 KB n=100
19 Correct 22 ms 47252 KB n=100
20 Correct 22 ms 47308 KB n=100
21 Correct 22 ms 47332 KB n=100
22 Correct 22 ms 47308 KB n=100
23 Correct 22 ms 47236 KB n=100
24 Correct 25 ms 47252 KB n=100
25 Correct 21 ms 47324 KB n=100
26 Correct 22 ms 47320 KB n=12
27 Correct 27 ms 47312 KB n=100
28 Correct 22 ms 47308 KB n=500
29 Correct 23 ms 47308 KB n=500
30 Correct 23 ms 47368 KB n=500
31 Correct 22 ms 47320 KB n=500
32 Correct 24 ms 47288 KB n=500
33 Correct 23 ms 47308 KB n=500
34 Correct 23 ms 47332 KB n=500
35 Correct 23 ms 47332 KB n=500
36 Correct 22 ms 47292 KB n=500
37 Correct 23 ms 47324 KB n=500
38 Correct 23 ms 47288 KB n=500
39 Correct 26 ms 47300 KB n=500
40 Correct 22 ms 47308 KB n=500
41 Correct 23 ms 47308 KB n=500
42 Correct 25 ms 47400 KB n=500
43 Correct 24 ms 47340 KB n=500
44 Correct 23 ms 47308 KB n=500
45 Correct 22 ms 47312 KB n=500
46 Correct 23 ms 47352 KB n=500
47 Correct 22 ms 47308 KB n=500
48 Correct 23 ms 47436 KB n=500
49 Correct 23 ms 47384 KB n=500
50 Correct 25 ms 47384 KB n=500
51 Correct 23 ms 47332 KB n=500
52 Correct 22 ms 47368 KB n=500
53 Correct 23 ms 47308 KB n=500
54 Correct 23 ms 47308 KB n=500
55 Correct 22 ms 47308 KB n=278
56 Correct 23 ms 47376 KB n=500
57 Correct 23 ms 47308 KB n=500
58 Correct 26 ms 47380 KB n=500
59 Correct 27 ms 47632 KB n=2000
60 Correct 28 ms 47768 KB n=2000
61 Correct 27 ms 47652 KB n=2000
62 Correct 27 ms 47668 KB n=2000
63 Correct 26 ms 47564 KB n=2000
64 Correct 29 ms 47708 KB n=2000
65 Correct 26 ms 47564 KB n=2000
66 Correct 25 ms 47680 KB n=2000
67 Correct 26 ms 47648 KB n=2000
68 Correct 25 ms 47692 KB n=2000
69 Correct 24 ms 47580 KB n=2000
70 Correct 24 ms 47564 KB n=2000
71 Correct 24 ms 47680 KB n=2000
72 Correct 24 ms 47620 KB n=2000
73 Correct 25 ms 47564 KB n=2000
74 Correct 29 ms 47564 KB n=1844
75 Correct 24 ms 47664 KB n=2000
76 Correct 26 ms 47656 KB n=2000
77 Correct 26 ms 47532 KB n=2000
78 Correct 26 ms 47552 KB n=2000
79 Correct 26 ms 47564 KB n=2000
80 Correct 25 ms 47716 KB n=2000
81 Correct 26 ms 47692 KB n=2000
82 Correct 25 ms 47564 KB n=2000
83 Correct 27 ms 47716 KB n=2000
84 Correct 28 ms 47668 KB n=2000
85 Correct 27 ms 47692 KB n=2000
86 Correct 26 ms 47628 KB n=2000
87 Correct 26 ms 47564 KB n=2000
88 Correct 25 ms 47692 KB n=2000
89 Correct 25 ms 47652 KB n=2000
90 Correct 25 ms 47772 KB n=2000
91 Correct 25 ms 47564 KB n=2000
92 Correct 835 ms 83224 KB n=200000
93 Correct 812 ms 96176 KB n=200000
94 Correct 764 ms 100552 KB n=200000
95 Correct 809 ms 89576 KB n=200000
96 Correct 831 ms 89432 KB n=200000
97 Correct 845 ms 94816 KB n=200000
98 Correct 834 ms 89568 KB n=200000
99 Correct 944 ms 89728 KB n=200000
100 Correct 835 ms 89520 KB n=200000
101 Correct 740 ms 102136 KB n=200000
102 Correct 451 ms 90604 KB n=200000
103 Correct 435 ms 90688 KB n=200000
104 Correct 431 ms 90716 KB n=200000
105 Correct 433 ms 91044 KB n=200000
106 Correct 419 ms 91064 KB n=200000
107 Correct 408 ms 91084 KB n=200000
108 Correct 818 ms 89584 KB n=200000
109 Correct 836 ms 89524 KB n=200000
110 Correct 826 ms 89500 KB n=200000
111 Correct 802 ms 89008 KB n=200000
112 Correct 739 ms 100908 KB n=200000
113 Correct 795 ms 94772 KB n=200000
114 Correct 846 ms 88992 KB n=200000
115 Correct 886 ms 92088 KB n=200000
116 Correct 773 ms 89616 KB n=200000
117 Correct 751 ms 101496 KB n=200000
118 Correct 835 ms 93364 KB n=200000
119 Correct 771 ms 89552 KB n=200000
120 Correct 687 ms 101140 KB n=200000
121 Correct 700 ms 101168 KB n=200000
122 Correct 695 ms 101524 KB n=200000
123 Correct 435 ms 90928 KB n=200000
124 Correct 220 ms 61448 KB n=25264