답안 #1114256

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1114256 2024-11-18T13:01:55 Z dead0ne Birthday gift (IZhO18_treearray) C++17
100 / 100
1132 ms 82504 KB
#include <bits/stdc++.h>
#define pb push_back
#define spc << " " <<
#define endl "\n"
#define all(x) x.begin(), x.end()
#define int long long
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define st first
#define nd second
#define mid (l+r)/2
#define inf 1e15
#define MOD 998244353
#define MX 200005
using namespace std;

vi edges[MX];
int par[19][MX], dep[MX];
void dfs(int node, int pare){
    par[0][node]=pare;
    for(auto i:edges[node]){
        if(i!=pare){
            dep[i]=dep[node]+1;
            dfs(i, node);
        }
    }
}
int lca(int v, int u){
    if(dep[u]>dep[v]) swap(v,u);
    for(int i=dep[v]-dep[u]; i>0; i-=i&-i) v=par[__lg(i&-i)][v];
    if(v==u) return v;
    for(int i=18; i>=0; i--){
        if(par[i][v]!=par[i][u]){
            v=par[i][v];
            u=par[i][u];
        }
    }
    return par[0][v];
}


void solve(){
    int n,m,q; cin >> n >> m >> q;
    for(int i=1; i<n; i++){
        int a,b; cin >> a >> b;
        edges[a].pb(b);
        edges[b].pb(a);
    }
    par[0][1]=1;
    dep[1]=0;
    dfs(1, 0);
    for(int i=1; i<=18; i++){
        for(int j=1; j<=n; j++){
            par[i][j] = par[i-1][par[i-1][j]];
        }
    }
    int arr[m+1];
    set<ii> ans[n+1];
    for(int i=1; i<=m; i++){
        cin >> arr[i];
        ans[arr[i]].insert({i,i});
        if(i>1){
            ans[lca(arr[i], arr[i-1])].insert({i-1, i});
        }
    }
    for(int i=1; i<=q; i++){
        int ty; cin >> ty;
        if(ty==1){
            int pos,v; cin >> pos >> v;
            ans[arr[pos]].erase({pos,pos});
            if(pos>1) ans[lca(arr[pos], arr[pos-1])].erase({pos-1,pos});
            if(pos<m) ans[lca(arr[pos], arr[pos+1])].erase({pos,pos+1});
            arr[pos]=v;
            ans[arr[pos]].insert({pos,pos});
            if(pos>1) ans[lca(arr[pos], arr[pos-1])].insert({pos-1,pos});
            if(pos<m) ans[lca(arr[pos], arr[pos+1])].insert({pos,pos+1});
        }
        else{
            int l,r,v; cin >> l >> r >> v;
            auto it=(ans[v].lower_bound({l,l}));
            if(it==ans[v].end() || (*it).nd>r) cout << "-1 -1\n";
            else{
                assert((*it).st>=l && (*it).nd<=r);
                cout << (*it).st spc (*it).nd << endl;
            }
        }
    }
}


signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    #ifdef Local
    freopen("in","r",stdin);
    freopen("out","w",stdout);
    /*#else
    freopen("248.in","r",stdin);
    freopen("248.out","w",stdout);*/
    #endif


    int t=1;
    //cin >> t;
    while(t--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 27984 KB n=5
2 Correct 4 ms 19792 KB n=100
3 Correct 3 ms 19792 KB n=100
4 Correct 3 ms 19792 KB n=100
5 Correct 4 ms 19960 KB n=100
6 Correct 4 ms 19792 KB n=100
7 Correct 4 ms 21924 KB n=100
8 Correct 4 ms 17744 KB n=100
9 Correct 4 ms 17912 KB n=100
10 Correct 4 ms 19792 KB n=100
11 Correct 4 ms 19792 KB n=100
12 Correct 3 ms 19792 KB n=100
13 Correct 4 ms 21840 KB n=100
14 Correct 3 ms 19792 KB n=100
15 Correct 5 ms 23888 KB n=100
16 Correct 4 ms 22008 KB n=100
17 Correct 4 ms 24060 KB n=100
18 Correct 4 ms 17912 KB n=100
19 Correct 3 ms 11600 KB n=100
20 Correct 3 ms 15696 KB n=100
21 Correct 4 ms 23888 KB n=100
22 Correct 5 ms 23888 KB n=100
23 Correct 4 ms 17744 KB n=100
24 Correct 3 ms 15696 KB n=100
25 Correct 3 ms 15696 KB n=100
26 Correct 3 ms 15696 KB n=12
27 Correct 2 ms 11600 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 27984 KB n=5
2 Correct 4 ms 19792 KB n=100
3 Correct 3 ms 19792 KB n=100
4 Correct 3 ms 19792 KB n=100
5 Correct 4 ms 19960 KB n=100
6 Correct 4 ms 19792 KB n=100
7 Correct 4 ms 21924 KB n=100
8 Correct 4 ms 17744 KB n=100
9 Correct 4 ms 17912 KB n=100
10 Correct 4 ms 19792 KB n=100
11 Correct 4 ms 19792 KB n=100
12 Correct 3 ms 19792 KB n=100
13 Correct 4 ms 21840 KB n=100
14 Correct 3 ms 19792 KB n=100
15 Correct 5 ms 23888 KB n=100
16 Correct 4 ms 22008 KB n=100
17 Correct 4 ms 24060 KB n=100
18 Correct 4 ms 17912 KB n=100
19 Correct 3 ms 11600 KB n=100
20 Correct 3 ms 15696 KB n=100
21 Correct 4 ms 23888 KB n=100
22 Correct 5 ms 23888 KB n=100
23 Correct 4 ms 17744 KB n=100
24 Correct 3 ms 15696 KB n=100
25 Correct 3 ms 15696 KB n=100
26 Correct 3 ms 15696 KB n=12
27 Correct 2 ms 11600 KB n=100
28 Correct 4 ms 11856 KB n=500
29 Correct 4 ms 15808 KB n=500
30 Correct 5 ms 15784 KB n=500
31 Correct 4 ms 18000 KB n=500
32 Correct 4 ms 11856 KB n=500
33 Correct 4 ms 15952 KB n=500
34 Correct 3 ms 13904 KB n=500
35 Correct 4 ms 11856 KB n=500
36 Correct 4 ms 18000 KB n=500
37 Correct 5 ms 18000 KB n=500
38 Correct 5 ms 20048 KB n=500
39 Correct 4 ms 20048 KB n=500
40 Correct 5 ms 20048 KB n=500
41 Correct 5 ms 20048 KB n=500
42 Correct 4 ms 22096 KB n=500
43 Correct 4 ms 20048 KB n=500
44 Correct 4 ms 20048 KB n=500
45 Correct 4 ms 24088 KB n=500
46 Correct 4 ms 20048 KB n=500
47 Correct 5 ms 22096 KB n=500
48 Correct 6 ms 24144 KB n=500
49 Correct 4 ms 24144 KB n=500
50 Correct 4 ms 24144 KB n=500
51 Correct 4 ms 22096 KB n=500
52 Correct 4 ms 20216 KB n=500
53 Correct 4 ms 20048 KB n=500
54 Correct 3 ms 13904 KB n=500
55 Correct 3 ms 15968 KB n=278
56 Correct 4 ms 18000 KB n=500
57 Correct 3 ms 18000 KB n=500
58 Correct 3 ms 15952 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 27984 KB n=5
2 Correct 4 ms 19792 KB n=100
3 Correct 3 ms 19792 KB n=100
4 Correct 3 ms 19792 KB n=100
5 Correct 4 ms 19960 KB n=100
6 Correct 4 ms 19792 KB n=100
7 Correct 4 ms 21924 KB n=100
8 Correct 4 ms 17744 KB n=100
9 Correct 4 ms 17912 KB n=100
10 Correct 4 ms 19792 KB n=100
11 Correct 4 ms 19792 KB n=100
12 Correct 3 ms 19792 KB n=100
13 Correct 4 ms 21840 KB n=100
14 Correct 3 ms 19792 KB n=100
15 Correct 5 ms 23888 KB n=100
16 Correct 4 ms 22008 KB n=100
17 Correct 4 ms 24060 KB n=100
18 Correct 4 ms 17912 KB n=100
19 Correct 3 ms 11600 KB n=100
20 Correct 3 ms 15696 KB n=100
21 Correct 4 ms 23888 KB n=100
22 Correct 5 ms 23888 KB n=100
23 Correct 4 ms 17744 KB n=100
24 Correct 3 ms 15696 KB n=100
25 Correct 3 ms 15696 KB n=100
26 Correct 3 ms 15696 KB n=12
27 Correct 2 ms 11600 KB n=100
28 Correct 4 ms 11856 KB n=500
29 Correct 4 ms 15808 KB n=500
30 Correct 5 ms 15784 KB n=500
31 Correct 4 ms 18000 KB n=500
32 Correct 4 ms 11856 KB n=500
33 Correct 4 ms 15952 KB n=500
34 Correct 3 ms 13904 KB n=500
35 Correct 4 ms 11856 KB n=500
36 Correct 4 ms 18000 KB n=500
37 Correct 5 ms 18000 KB n=500
38 Correct 5 ms 20048 KB n=500
39 Correct 4 ms 20048 KB n=500
40 Correct 5 ms 20048 KB n=500
41 Correct 5 ms 20048 KB n=500
42 Correct 4 ms 22096 KB n=500
43 Correct 4 ms 20048 KB n=500
44 Correct 4 ms 20048 KB n=500
45 Correct 4 ms 24088 KB n=500
46 Correct 4 ms 20048 KB n=500
47 Correct 5 ms 22096 KB n=500
48 Correct 6 ms 24144 KB n=500
49 Correct 4 ms 24144 KB n=500
50 Correct 4 ms 24144 KB n=500
51 Correct 4 ms 22096 KB n=500
52 Correct 4 ms 20216 KB n=500
53 Correct 4 ms 20048 KB n=500
54 Correct 3 ms 13904 KB n=500
55 Correct 3 ms 15968 KB n=278
56 Correct 4 ms 18000 KB n=500
57 Correct 3 ms 18000 KB n=500
58 Correct 3 ms 15952 KB n=500
59 Correct 6 ms 18512 KB n=2000
60 Correct 5 ms 16464 KB n=2000
61 Correct 5 ms 18512 KB n=2000
62 Correct 5 ms 12368 KB n=2000
63 Correct 6 ms 16464 KB n=2000
64 Correct 5 ms 14416 KB n=2000
65 Correct 7 ms 16476 KB n=2000
66 Correct 5 ms 16464 KB n=2000
67 Correct 6 ms 16464 KB n=2000
68 Correct 5 ms 12368 KB n=2000
69 Correct 5 ms 12368 KB n=2000
70 Correct 5 ms 20560 KB n=2000
71 Correct 6 ms 18512 KB n=2000
72 Correct 6 ms 14416 KB n=2000
73 Correct 5 ms 14584 KB n=2000
74 Correct 6 ms 16464 KB n=1844
75 Correct 5 ms 16632 KB n=2000
76 Correct 6 ms 12368 KB n=2000
77 Correct 6 ms 12368 KB n=2000
78 Correct 6 ms 18512 KB n=2000
79 Correct 6 ms 16464 KB n=2000
80 Correct 8 ms 16464 KB n=2000
81 Correct 6 ms 18512 KB n=2000
82 Correct 6 ms 16464 KB n=2000
83 Correct 5 ms 18512 KB n=2000
84 Correct 5 ms 14416 KB n=2000
85 Correct 6 ms 20560 KB n=2000
86 Correct 6 ms 16464 KB n=2000
87 Correct 6 ms 16464 KB n=2000
88 Correct 6 ms 22352 KB n=2000
89 Correct 6 ms 26448 KB n=2000
90 Correct 6 ms 20304 KB n=2000
91 Correct 5 ms 20560 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 27984 KB n=5
2 Correct 4 ms 19792 KB n=100
3 Correct 3 ms 19792 KB n=100
4 Correct 3 ms 19792 KB n=100
5 Correct 4 ms 19960 KB n=100
6 Correct 4 ms 19792 KB n=100
7 Correct 4 ms 21924 KB n=100
8 Correct 4 ms 17744 KB n=100
9 Correct 4 ms 17912 KB n=100
10 Correct 4 ms 19792 KB n=100
11 Correct 4 ms 19792 KB n=100
12 Correct 3 ms 19792 KB n=100
13 Correct 4 ms 21840 KB n=100
14 Correct 3 ms 19792 KB n=100
15 Correct 5 ms 23888 KB n=100
16 Correct 4 ms 22008 KB n=100
17 Correct 4 ms 24060 KB n=100
18 Correct 4 ms 17912 KB n=100
19 Correct 3 ms 11600 KB n=100
20 Correct 3 ms 15696 KB n=100
21 Correct 4 ms 23888 KB n=100
22 Correct 5 ms 23888 KB n=100
23 Correct 4 ms 17744 KB n=100
24 Correct 3 ms 15696 KB n=100
25 Correct 3 ms 15696 KB n=100
26 Correct 3 ms 15696 KB n=12
27 Correct 2 ms 11600 KB n=100
28 Correct 4 ms 11856 KB n=500
29 Correct 4 ms 15808 KB n=500
30 Correct 5 ms 15784 KB n=500
31 Correct 4 ms 18000 KB n=500
32 Correct 4 ms 11856 KB n=500
33 Correct 4 ms 15952 KB n=500
34 Correct 3 ms 13904 KB n=500
35 Correct 4 ms 11856 KB n=500
36 Correct 4 ms 18000 KB n=500
37 Correct 5 ms 18000 KB n=500
38 Correct 5 ms 20048 KB n=500
39 Correct 4 ms 20048 KB n=500
40 Correct 5 ms 20048 KB n=500
41 Correct 5 ms 20048 KB n=500
42 Correct 4 ms 22096 KB n=500
43 Correct 4 ms 20048 KB n=500
44 Correct 4 ms 20048 KB n=500
45 Correct 4 ms 24088 KB n=500
46 Correct 4 ms 20048 KB n=500
47 Correct 5 ms 22096 KB n=500
48 Correct 6 ms 24144 KB n=500
49 Correct 4 ms 24144 KB n=500
50 Correct 4 ms 24144 KB n=500
51 Correct 4 ms 22096 KB n=500
52 Correct 4 ms 20216 KB n=500
53 Correct 4 ms 20048 KB n=500
54 Correct 3 ms 13904 KB n=500
55 Correct 3 ms 15968 KB n=278
56 Correct 4 ms 18000 KB n=500
57 Correct 3 ms 18000 KB n=500
58 Correct 3 ms 15952 KB n=500
59 Correct 6 ms 18512 KB n=2000
60 Correct 5 ms 16464 KB n=2000
61 Correct 5 ms 18512 KB n=2000
62 Correct 5 ms 12368 KB n=2000
63 Correct 6 ms 16464 KB n=2000
64 Correct 5 ms 14416 KB n=2000
65 Correct 7 ms 16476 KB n=2000
66 Correct 5 ms 16464 KB n=2000
67 Correct 6 ms 16464 KB n=2000
68 Correct 5 ms 12368 KB n=2000
69 Correct 5 ms 12368 KB n=2000
70 Correct 5 ms 20560 KB n=2000
71 Correct 6 ms 18512 KB n=2000
72 Correct 6 ms 14416 KB n=2000
73 Correct 5 ms 14584 KB n=2000
74 Correct 6 ms 16464 KB n=1844
75 Correct 5 ms 16632 KB n=2000
76 Correct 6 ms 12368 KB n=2000
77 Correct 6 ms 12368 KB n=2000
78 Correct 6 ms 18512 KB n=2000
79 Correct 6 ms 16464 KB n=2000
80 Correct 8 ms 16464 KB n=2000
81 Correct 6 ms 18512 KB n=2000
82 Correct 6 ms 16464 KB n=2000
83 Correct 5 ms 18512 KB n=2000
84 Correct 5 ms 14416 KB n=2000
85 Correct 6 ms 20560 KB n=2000
86 Correct 6 ms 16464 KB n=2000
87 Correct 6 ms 16464 KB n=2000
88 Correct 6 ms 22352 KB n=2000
89 Correct 6 ms 26448 KB n=2000
90 Correct 6 ms 20304 KB n=2000
91 Correct 5 ms 20560 KB n=2000
92 Correct 773 ms 81604 KB n=200000
93 Correct 1099 ms 80456 KB n=200000
94 Correct 1049 ms 80148 KB n=200000
95 Correct 786 ms 82012 KB n=200000
96 Correct 775 ms 82104 KB n=200000
97 Correct 1132 ms 80652 KB n=200000
98 Correct 784 ms 82352 KB n=200000
99 Correct 933 ms 80972 KB n=200000
100 Correct 735 ms 81412 KB n=200000
101 Correct 825 ms 79576 KB n=200000
102 Correct 404 ms 82352 KB n=200000
103 Correct 384 ms 82408 KB n=200000
104 Correct 369 ms 82504 KB n=200000
105 Correct 354 ms 82188 KB n=200000
106 Correct 359 ms 82172 KB n=200000
107 Correct 376 ms 82136 KB n=200000
108 Correct 742 ms 81236 KB n=200000
109 Correct 777 ms 81124 KB n=200000
110 Correct 763 ms 81224 KB n=200000
111 Correct 766 ms 81848 KB n=200000
112 Correct 878 ms 79832 KB n=200000
113 Correct 895 ms 80576 KB n=200000
114 Correct 819 ms 82108 KB n=200000
115 Correct 1118 ms 80536 KB n=200000
116 Correct 658 ms 82460 KB n=200000
117 Correct 855 ms 79596 KB n=200000
118 Correct 1010 ms 80488 KB n=200000
119 Correct 723 ms 81596 KB n=200000
120 Correct 875 ms 79240 KB n=200000
121 Correct 818 ms 79176 KB n=200000
122 Correct 852 ms 79688 KB n=200000
123 Correct 361 ms 81784 KB n=200000
124 Correct 142 ms 32840 KB n=25264