Submission #492161

# Submission time Handle Problem Language Result Execution time Memory
492161 2021-12-05T18:39:01 Z infertechno2 Birthday gift (IZhO18_treearray) C++17
100 / 100
1078 ms 71276 KB
#include <bits/stdc++.h>

using namespace std;

typedef int ll;

const ll Size=2e5+10;
ll n,m,q;
ll tin[Size],height[Size],lca_tree[7*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;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23728 KB n=5
2 Correct 12 ms 23780 KB n=100
3 Correct 12 ms 23792 KB n=100
4 Correct 17 ms 23756 KB n=100
5 Correct 15 ms 23812 KB n=100
6 Correct 12 ms 23728 KB n=100
7 Correct 12 ms 23756 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23788 KB n=100
10 Correct 12 ms 23756 KB n=100
11 Correct 14 ms 23848 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23844 KB n=100
14 Correct 11 ms 23756 KB n=100
15 Correct 11 ms 23756 KB n=100
16 Correct 14 ms 23756 KB n=100
17 Correct 12 ms 23756 KB n=100
18 Correct 14 ms 23756 KB n=100
19 Correct 13 ms 23852 KB n=100
20 Correct 12 ms 23800 KB n=100
21 Correct 12 ms 23788 KB n=100
22 Correct 14 ms 23852 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 12 ms 23756 KB n=100
26 Correct 14 ms 23808 KB n=12
27 Correct 11 ms 23756 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23728 KB n=5
2 Correct 12 ms 23780 KB n=100
3 Correct 12 ms 23792 KB n=100
4 Correct 17 ms 23756 KB n=100
5 Correct 15 ms 23812 KB n=100
6 Correct 12 ms 23728 KB n=100
7 Correct 12 ms 23756 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23788 KB n=100
10 Correct 12 ms 23756 KB n=100
11 Correct 14 ms 23848 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23844 KB n=100
14 Correct 11 ms 23756 KB n=100
15 Correct 11 ms 23756 KB n=100
16 Correct 14 ms 23756 KB n=100
17 Correct 12 ms 23756 KB n=100
18 Correct 14 ms 23756 KB n=100
19 Correct 13 ms 23852 KB n=100
20 Correct 12 ms 23800 KB n=100
21 Correct 12 ms 23788 KB n=100
22 Correct 14 ms 23852 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 12 ms 23756 KB n=100
26 Correct 14 ms 23808 KB n=12
27 Correct 11 ms 23756 KB n=100
28 Correct 14 ms 23884 KB n=500
29 Correct 13 ms 23836 KB n=500
30 Correct 13 ms 23884 KB n=500
31 Correct 12 ms 23884 KB n=500
32 Correct 13 ms 23908 KB n=500
33 Correct 12 ms 23888 KB n=500
34 Correct 13 ms 23848 KB n=500
35 Correct 12 ms 23884 KB n=500
36 Correct 12 ms 23868 KB n=500
37 Correct 16 ms 23884 KB n=500
38 Correct 15 ms 23884 KB n=500
39 Correct 13 ms 23884 KB n=500
40 Correct 12 ms 23884 KB n=500
41 Correct 12 ms 23920 KB n=500
42 Correct 13 ms 23884 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 13 ms 23876 KB n=500
45 Correct 12 ms 23916 KB n=500
46 Correct 13 ms 23904 KB n=500
47 Correct 15 ms 23928 KB n=500
48 Correct 17 ms 23884 KB n=500
49 Correct 13 ms 23936 KB n=500
50 Correct 12 ms 23884 KB n=500
51 Correct 13 ms 23892 KB n=500
52 Correct 13 ms 23884 KB n=500
53 Correct 13 ms 23884 KB n=500
54 Correct 13 ms 23884 KB n=500
55 Correct 13 ms 23884 KB n=278
56 Correct 12 ms 23884 KB n=500
57 Correct 12 ms 23908 KB n=500
58 Correct 12 ms 23884 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23728 KB n=5
2 Correct 12 ms 23780 KB n=100
3 Correct 12 ms 23792 KB n=100
4 Correct 17 ms 23756 KB n=100
5 Correct 15 ms 23812 KB n=100
6 Correct 12 ms 23728 KB n=100
7 Correct 12 ms 23756 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23788 KB n=100
10 Correct 12 ms 23756 KB n=100
11 Correct 14 ms 23848 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23844 KB n=100
14 Correct 11 ms 23756 KB n=100
15 Correct 11 ms 23756 KB n=100
16 Correct 14 ms 23756 KB n=100
17 Correct 12 ms 23756 KB n=100
18 Correct 14 ms 23756 KB n=100
19 Correct 13 ms 23852 KB n=100
20 Correct 12 ms 23800 KB n=100
21 Correct 12 ms 23788 KB n=100
22 Correct 14 ms 23852 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 12 ms 23756 KB n=100
26 Correct 14 ms 23808 KB n=12
27 Correct 11 ms 23756 KB n=100
28 Correct 14 ms 23884 KB n=500
29 Correct 13 ms 23836 KB n=500
30 Correct 13 ms 23884 KB n=500
31 Correct 12 ms 23884 KB n=500
32 Correct 13 ms 23908 KB n=500
33 Correct 12 ms 23888 KB n=500
34 Correct 13 ms 23848 KB n=500
35 Correct 12 ms 23884 KB n=500
36 Correct 12 ms 23868 KB n=500
37 Correct 16 ms 23884 KB n=500
38 Correct 15 ms 23884 KB n=500
39 Correct 13 ms 23884 KB n=500
40 Correct 12 ms 23884 KB n=500
41 Correct 12 ms 23920 KB n=500
42 Correct 13 ms 23884 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 13 ms 23876 KB n=500
45 Correct 12 ms 23916 KB n=500
46 Correct 13 ms 23904 KB n=500
47 Correct 15 ms 23928 KB n=500
48 Correct 17 ms 23884 KB n=500
49 Correct 13 ms 23936 KB n=500
50 Correct 12 ms 23884 KB n=500
51 Correct 13 ms 23892 KB n=500
52 Correct 13 ms 23884 KB n=500
53 Correct 13 ms 23884 KB n=500
54 Correct 13 ms 23884 KB n=500
55 Correct 13 ms 23884 KB n=278
56 Correct 12 ms 23884 KB n=500
57 Correct 12 ms 23908 KB n=500
58 Correct 12 ms 23884 KB n=500
59 Correct 16 ms 24080 KB n=2000
60 Correct 20 ms 24268 KB n=2000
61 Correct 16 ms 24140 KB n=2000
62 Correct 15 ms 24152 KB n=2000
63 Correct 17 ms 24140 KB n=2000
64 Correct 16 ms 24172 KB n=2000
65 Correct 17 ms 24128 KB n=2000
66 Correct 17 ms 24252 KB n=2000
67 Correct 15 ms 24140 KB n=2000
68 Correct 16 ms 24176 KB n=2000
69 Correct 14 ms 24140 KB n=2000
70 Correct 15 ms 24140 KB n=2000
71 Correct 16 ms 24184 KB n=2000
72 Correct 14 ms 24140 KB n=2000
73 Correct 14 ms 24084 KB n=2000
74 Correct 15 ms 24064 KB n=1844
75 Correct 14 ms 24140 KB n=2000
76 Correct 15 ms 24140 KB n=2000
77 Correct 15 ms 24172 KB n=2000
78 Correct 16 ms 24164 KB n=2000
79 Correct 16 ms 24140 KB n=2000
80 Correct 16 ms 24264 KB n=2000
81 Correct 19 ms 24196 KB n=2000
82 Correct 15 ms 24176 KB n=2000
83 Correct 15 ms 24140 KB n=2000
84 Correct 15 ms 24164 KB n=2000
85 Correct 16 ms 24208 KB n=2000
86 Correct 18 ms 24196 KB n=2000
87 Correct 16 ms 24172 KB n=2000
88 Correct 16 ms 24288 KB n=2000
89 Correct 15 ms 24208 KB n=2000
90 Correct 17 ms 24284 KB n=2000
91 Correct 17 ms 24180 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23728 KB n=5
2 Correct 12 ms 23780 KB n=100
3 Correct 12 ms 23792 KB n=100
4 Correct 17 ms 23756 KB n=100
5 Correct 15 ms 23812 KB n=100
6 Correct 12 ms 23728 KB n=100
7 Correct 12 ms 23756 KB n=100
8 Correct 12 ms 23756 KB n=100
9 Correct 12 ms 23788 KB n=100
10 Correct 12 ms 23756 KB n=100
11 Correct 14 ms 23848 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23844 KB n=100
14 Correct 11 ms 23756 KB n=100
15 Correct 11 ms 23756 KB n=100
16 Correct 14 ms 23756 KB n=100
17 Correct 12 ms 23756 KB n=100
18 Correct 14 ms 23756 KB n=100
19 Correct 13 ms 23852 KB n=100
20 Correct 12 ms 23800 KB n=100
21 Correct 12 ms 23788 KB n=100
22 Correct 14 ms 23852 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 12 ms 23756 KB n=100
26 Correct 14 ms 23808 KB n=12
27 Correct 11 ms 23756 KB n=100
28 Correct 14 ms 23884 KB n=500
29 Correct 13 ms 23836 KB n=500
30 Correct 13 ms 23884 KB n=500
31 Correct 12 ms 23884 KB n=500
32 Correct 13 ms 23908 KB n=500
33 Correct 12 ms 23888 KB n=500
34 Correct 13 ms 23848 KB n=500
35 Correct 12 ms 23884 KB n=500
36 Correct 12 ms 23868 KB n=500
37 Correct 16 ms 23884 KB n=500
38 Correct 15 ms 23884 KB n=500
39 Correct 13 ms 23884 KB n=500
40 Correct 12 ms 23884 KB n=500
41 Correct 12 ms 23920 KB n=500
42 Correct 13 ms 23884 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 13 ms 23876 KB n=500
45 Correct 12 ms 23916 KB n=500
46 Correct 13 ms 23904 KB n=500
47 Correct 15 ms 23928 KB n=500
48 Correct 17 ms 23884 KB n=500
49 Correct 13 ms 23936 KB n=500
50 Correct 12 ms 23884 KB n=500
51 Correct 13 ms 23892 KB n=500
52 Correct 13 ms 23884 KB n=500
53 Correct 13 ms 23884 KB n=500
54 Correct 13 ms 23884 KB n=500
55 Correct 13 ms 23884 KB n=278
56 Correct 12 ms 23884 KB n=500
57 Correct 12 ms 23908 KB n=500
58 Correct 12 ms 23884 KB n=500
59 Correct 16 ms 24080 KB n=2000
60 Correct 20 ms 24268 KB n=2000
61 Correct 16 ms 24140 KB n=2000
62 Correct 15 ms 24152 KB n=2000
63 Correct 17 ms 24140 KB n=2000
64 Correct 16 ms 24172 KB n=2000
65 Correct 17 ms 24128 KB n=2000
66 Correct 17 ms 24252 KB n=2000
67 Correct 15 ms 24140 KB n=2000
68 Correct 16 ms 24176 KB n=2000
69 Correct 14 ms 24140 KB n=2000
70 Correct 15 ms 24140 KB n=2000
71 Correct 16 ms 24184 KB n=2000
72 Correct 14 ms 24140 KB n=2000
73 Correct 14 ms 24084 KB n=2000
74 Correct 15 ms 24064 KB n=1844
75 Correct 14 ms 24140 KB n=2000
76 Correct 15 ms 24140 KB n=2000
77 Correct 15 ms 24172 KB n=2000
78 Correct 16 ms 24164 KB n=2000
79 Correct 16 ms 24140 KB n=2000
80 Correct 16 ms 24264 KB n=2000
81 Correct 19 ms 24196 KB n=2000
82 Correct 15 ms 24176 KB n=2000
83 Correct 15 ms 24140 KB n=2000
84 Correct 15 ms 24164 KB n=2000
85 Correct 16 ms 24208 KB n=2000
86 Correct 18 ms 24196 KB n=2000
87 Correct 16 ms 24172 KB n=2000
88 Correct 16 ms 24288 KB n=2000
89 Correct 15 ms 24208 KB n=2000
90 Correct 17 ms 24284 KB n=2000
91 Correct 17 ms 24180 KB n=2000
92 Correct 805 ms 59524 KB n=200000
93 Correct 876 ms 64872 KB n=200000
94 Correct 774 ms 69368 KB n=200000
95 Correct 856 ms 59484 KB n=200000
96 Correct 829 ms 59544 KB n=200000
97 Correct 824 ms 63868 KB n=200000
98 Correct 863 ms 59564 KB n=200000
99 Correct 977 ms 58980 KB n=200000
100 Correct 838 ms 59536 KB n=200000
101 Correct 788 ms 70924 KB n=200000
102 Correct 396 ms 60412 KB n=200000
103 Correct 452 ms 60468 KB n=200000
104 Correct 411 ms 60428 KB n=200000
105 Correct 444 ms 60136 KB n=200000
106 Correct 466 ms 60212 KB n=200000
107 Correct 440 ms 60224 KB n=200000
108 Correct 913 ms 59072 KB n=200000
109 Correct 1078 ms 59120 KB n=200000
110 Correct 904 ms 59136 KB n=200000
111 Correct 824 ms 59376 KB n=200000
112 Correct 761 ms 69684 KB n=200000
113 Correct 907 ms 63796 KB n=200000
114 Correct 872 ms 59628 KB n=200000
115 Correct 947 ms 60804 KB n=200000
116 Correct 802 ms 59232 KB n=200000
117 Correct 748 ms 70192 KB n=200000
118 Correct 878 ms 62036 KB n=200000
119 Correct 776 ms 59236 KB n=200000
120 Correct 688 ms 70732 KB n=200000
121 Correct 708 ms 70668 KB n=200000
122 Correct 705 ms 71276 KB n=200000
123 Correct 454 ms 59828 KB n=200000
124 Correct 205 ms 36000 KB n=25264