답안 #500225

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
500225 2021-12-30T13:25:21 Z nickmet2004 Birthday gift (IZhO18_treearray) C++11
100 / 100
1244 ms 87128 KB
#include<bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5;
int n , m , q , a[N];
vector<int> adj[N];
int P[N][20],depth[N];
set<int> A[N] , B[N];
void dfs(int u , int p = 0){
    P[u][0] = p;
    for(int i = 1; i <= 18; ++i)P[u][i] = P[P[u][i - 1]][i - 1];
    for(int v : adj[u]){
        if(v==p)continue;
        depth[v]=depth[u]+1;
        dfs(v , u);
    }
}
int LCA(int u , int v){
    if(depth[u] < depth[v])swap(u , v);
    int k = depth[u] - depth[v];
    for(int i = 18; ~i; --i){
        if(k>>i&1) u = P[u][i];
    }
    if(u==v)return u;
    for(int i= 18; ~i; --i){
        if(P[u][i] != P[v][i]) u = P[u][i] , v= P[v][i];
    }
    return P[u][0];
}
void upd(int pos , int v){
    int y;
    if(pos >1){
        y = LCA(a[pos - 1] ,a[pos]);
        A[y].erase(pos - 1);
        A[LCA(a[pos - 1] , v)].insert(pos - 1);
    }
    if(pos < m){
        y = LCA(a[pos] , a[pos + 1]);
        A[y].erase(pos);
        A[LCA(a[pos+1] , v)].insert(pos);
    }
    B[a[pos]].erase(pos);
    B[v].insert(pos);
    a[pos]=v;
    return;
}
void get(int l , int r , int v){
    auto it = B[v].lower_bound(l);
    if(it != B[v].end() && *it <= r){
        cout << *it << " " << *it << endl;
        return;
    }
    it = A[v].lower_bound(l);
    if(it != A[v].end() && *it + 1 <= r){
        cout << *it << " " << *it + 1<<endl;
        return;
    }
    cout<<-1 << " " << -1<<endl;
}
int main (){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m>>q;
    int u  ,v;
    for(int i= 1; i< n; ++i){
        cin >> u >> v;
        adj[u].emplace_back(v);adj[v].emplace_back(u);
    }
    for(int i= 1; i<= m; ++i)cin >> a[i];
    dfs(1);
    for(int i= 1; i <= m; ++i){
        int X = LCA(a[i] , a[i + 1]);
        if(i<m)A[X].insert(i);
        B[a[i]].insert(i);
    }
    while(q--){
        int x;
        cin >> x;
        if(x==1){
            int pos , v;
            cin >> pos >> v;
            upd(pos , v);
        }else{
            int l , r ,v;
            cin >> l >> r >> v;
            get( l  , r , v);
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23812 KB n=5
2 Correct 12 ms 23756 KB n=100
3 Correct 11 ms 23788 KB n=100
4 Correct 12 ms 23776 KB n=100
5 Correct 12 ms 23756 KB n=100
6 Correct 11 ms 23756 KB n=100
7 Correct 12 ms 23756 KB n=100
8 Correct 11 ms 23756 KB n=100
9 Correct 11 ms 23804 KB n=100
10 Correct 11 ms 23796 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23796 KB n=100
15 Correct 11 ms 23812 KB n=100
16 Correct 11 ms 23812 KB n=100
17 Correct 14 ms 23808 KB n=100
18 Correct 11 ms 23812 KB n=100
19 Correct 11 ms 23756 KB n=100
20 Correct 11 ms 23828 KB n=100
21 Correct 14 ms 23792 KB n=100
22 Correct 12 ms 23796 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 11 ms 23780 KB n=100
26 Correct 11 ms 23796 KB n=12
27 Correct 11 ms 23756 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23812 KB n=5
2 Correct 12 ms 23756 KB n=100
3 Correct 11 ms 23788 KB n=100
4 Correct 12 ms 23776 KB n=100
5 Correct 12 ms 23756 KB n=100
6 Correct 11 ms 23756 KB n=100
7 Correct 12 ms 23756 KB n=100
8 Correct 11 ms 23756 KB n=100
9 Correct 11 ms 23804 KB n=100
10 Correct 11 ms 23796 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23796 KB n=100
15 Correct 11 ms 23812 KB n=100
16 Correct 11 ms 23812 KB n=100
17 Correct 14 ms 23808 KB n=100
18 Correct 11 ms 23812 KB n=100
19 Correct 11 ms 23756 KB n=100
20 Correct 11 ms 23828 KB n=100
21 Correct 14 ms 23792 KB n=100
22 Correct 12 ms 23796 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 11 ms 23780 KB n=100
26 Correct 11 ms 23796 KB n=12
27 Correct 11 ms 23756 KB n=100
28 Correct 12 ms 23952 KB n=500
29 Correct 13 ms 23884 KB n=500
30 Correct 13 ms 23884 KB n=500
31 Correct 16 ms 23932 KB n=500
32 Correct 13 ms 23884 KB n=500
33 Correct 13 ms 23884 KB n=500
34 Correct 12 ms 23948 KB n=500
35 Correct 13 ms 23884 KB n=500
36 Correct 17 ms 23824 KB n=500
37 Correct 13 ms 23884 KB n=500
38 Correct 13 ms 23884 KB n=500
39 Correct 13 ms 23884 KB n=500
40 Correct 16 ms 23884 KB n=500
41 Correct 13 ms 23884 KB n=500
42 Correct 13 ms 23816 KB n=500
43 Correct 13 ms 23828 KB n=500
44 Correct 14 ms 23876 KB n=500
45 Correct 13 ms 23836 KB n=500
46 Correct 16 ms 23948 KB n=500
47 Correct 14 ms 23884 KB n=500
48 Correct 12 ms 23884 KB n=500
49 Correct 12 ms 23884 KB n=500
50 Correct 15 ms 23884 KB n=500
51 Correct 13 ms 23932 KB n=500
52 Correct 16 ms 23952 KB n=500
53 Correct 13 ms 23884 KB n=500
54 Correct 13 ms 23884 KB n=500
55 Correct 14 ms 23884 KB n=278
56 Correct 12 ms 23948 KB n=500
57 Correct 11 ms 23884 KB n=500
58 Correct 13 ms 23916 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23812 KB n=5
2 Correct 12 ms 23756 KB n=100
3 Correct 11 ms 23788 KB n=100
4 Correct 12 ms 23776 KB n=100
5 Correct 12 ms 23756 KB n=100
6 Correct 11 ms 23756 KB n=100
7 Correct 12 ms 23756 KB n=100
8 Correct 11 ms 23756 KB n=100
9 Correct 11 ms 23804 KB n=100
10 Correct 11 ms 23796 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23796 KB n=100
15 Correct 11 ms 23812 KB n=100
16 Correct 11 ms 23812 KB n=100
17 Correct 14 ms 23808 KB n=100
18 Correct 11 ms 23812 KB n=100
19 Correct 11 ms 23756 KB n=100
20 Correct 11 ms 23828 KB n=100
21 Correct 14 ms 23792 KB n=100
22 Correct 12 ms 23796 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 11 ms 23780 KB n=100
26 Correct 11 ms 23796 KB n=12
27 Correct 11 ms 23756 KB n=100
28 Correct 12 ms 23952 KB n=500
29 Correct 13 ms 23884 KB n=500
30 Correct 13 ms 23884 KB n=500
31 Correct 16 ms 23932 KB n=500
32 Correct 13 ms 23884 KB n=500
33 Correct 13 ms 23884 KB n=500
34 Correct 12 ms 23948 KB n=500
35 Correct 13 ms 23884 KB n=500
36 Correct 17 ms 23824 KB n=500
37 Correct 13 ms 23884 KB n=500
38 Correct 13 ms 23884 KB n=500
39 Correct 13 ms 23884 KB n=500
40 Correct 16 ms 23884 KB n=500
41 Correct 13 ms 23884 KB n=500
42 Correct 13 ms 23816 KB n=500
43 Correct 13 ms 23828 KB n=500
44 Correct 14 ms 23876 KB n=500
45 Correct 13 ms 23836 KB n=500
46 Correct 16 ms 23948 KB n=500
47 Correct 14 ms 23884 KB n=500
48 Correct 12 ms 23884 KB n=500
49 Correct 12 ms 23884 KB n=500
50 Correct 15 ms 23884 KB n=500
51 Correct 13 ms 23932 KB n=500
52 Correct 16 ms 23952 KB n=500
53 Correct 13 ms 23884 KB n=500
54 Correct 13 ms 23884 KB n=500
55 Correct 14 ms 23884 KB n=278
56 Correct 12 ms 23948 KB n=500
57 Correct 11 ms 23884 KB n=500
58 Correct 13 ms 23916 KB n=500
59 Correct 15 ms 24336 KB n=2000
60 Correct 14 ms 24420 KB n=2000
61 Correct 17 ms 24304 KB n=2000
62 Correct 15 ms 24268 KB n=2000
63 Correct 18 ms 24252 KB n=2000
64 Correct 15 ms 24268 KB n=2000
65 Correct 15 ms 24228 KB n=2000
66 Correct 16 ms 24388 KB n=2000
67 Correct 21 ms 24280 KB n=2000
68 Correct 15 ms 24336 KB n=2000
69 Correct 15 ms 24296 KB n=2000
70 Correct 16 ms 24268 KB n=2000
71 Correct 21 ms 24208 KB n=2000
72 Correct 17 ms 24268 KB n=2000
73 Correct 22 ms 24300 KB n=2000
74 Correct 18 ms 24204 KB n=1844
75 Correct 24 ms 24292 KB n=2000
76 Correct 16 ms 24188 KB n=2000
77 Correct 19 ms 24204 KB n=2000
78 Correct 18 ms 24284 KB n=2000
79 Correct 18 ms 24204 KB n=2000
80 Correct 18 ms 24344 KB n=2000
81 Correct 23 ms 24328 KB n=2000
82 Correct 22 ms 24288 KB n=2000
83 Correct 21 ms 24364 KB n=2000
84 Correct 18 ms 24204 KB n=2000
85 Correct 21 ms 24196 KB n=2000
86 Correct 19 ms 24268 KB n=2000
87 Correct 17 ms 24208 KB n=2000
88 Correct 19 ms 24344 KB n=2000
89 Correct 18 ms 24336 KB n=2000
90 Correct 17 ms 24400 KB n=2000
91 Correct 19 ms 24296 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23812 KB n=5
2 Correct 12 ms 23756 KB n=100
3 Correct 11 ms 23788 KB n=100
4 Correct 12 ms 23776 KB n=100
5 Correct 12 ms 23756 KB n=100
6 Correct 11 ms 23756 KB n=100
7 Correct 12 ms 23756 KB n=100
8 Correct 11 ms 23756 KB n=100
9 Correct 11 ms 23804 KB n=100
10 Correct 11 ms 23796 KB n=100
11 Correct 12 ms 23756 KB n=100
12 Correct 12 ms 23756 KB n=100
13 Correct 12 ms 23756 KB n=100
14 Correct 12 ms 23796 KB n=100
15 Correct 11 ms 23812 KB n=100
16 Correct 11 ms 23812 KB n=100
17 Correct 14 ms 23808 KB n=100
18 Correct 11 ms 23812 KB n=100
19 Correct 11 ms 23756 KB n=100
20 Correct 11 ms 23828 KB n=100
21 Correct 14 ms 23792 KB n=100
22 Correct 12 ms 23796 KB n=100
23 Correct 12 ms 23756 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 11 ms 23780 KB n=100
26 Correct 11 ms 23796 KB n=12
27 Correct 11 ms 23756 KB n=100
28 Correct 12 ms 23952 KB n=500
29 Correct 13 ms 23884 KB n=500
30 Correct 13 ms 23884 KB n=500
31 Correct 16 ms 23932 KB n=500
32 Correct 13 ms 23884 KB n=500
33 Correct 13 ms 23884 KB n=500
34 Correct 12 ms 23948 KB n=500
35 Correct 13 ms 23884 KB n=500
36 Correct 17 ms 23824 KB n=500
37 Correct 13 ms 23884 KB n=500
38 Correct 13 ms 23884 KB n=500
39 Correct 13 ms 23884 KB n=500
40 Correct 16 ms 23884 KB n=500
41 Correct 13 ms 23884 KB n=500
42 Correct 13 ms 23816 KB n=500
43 Correct 13 ms 23828 KB n=500
44 Correct 14 ms 23876 KB n=500
45 Correct 13 ms 23836 KB n=500
46 Correct 16 ms 23948 KB n=500
47 Correct 14 ms 23884 KB n=500
48 Correct 12 ms 23884 KB n=500
49 Correct 12 ms 23884 KB n=500
50 Correct 15 ms 23884 KB n=500
51 Correct 13 ms 23932 KB n=500
52 Correct 16 ms 23952 KB n=500
53 Correct 13 ms 23884 KB n=500
54 Correct 13 ms 23884 KB n=500
55 Correct 14 ms 23884 KB n=278
56 Correct 12 ms 23948 KB n=500
57 Correct 11 ms 23884 KB n=500
58 Correct 13 ms 23916 KB n=500
59 Correct 15 ms 24336 KB n=2000
60 Correct 14 ms 24420 KB n=2000
61 Correct 17 ms 24304 KB n=2000
62 Correct 15 ms 24268 KB n=2000
63 Correct 18 ms 24252 KB n=2000
64 Correct 15 ms 24268 KB n=2000
65 Correct 15 ms 24228 KB n=2000
66 Correct 16 ms 24388 KB n=2000
67 Correct 21 ms 24280 KB n=2000
68 Correct 15 ms 24336 KB n=2000
69 Correct 15 ms 24296 KB n=2000
70 Correct 16 ms 24268 KB n=2000
71 Correct 21 ms 24208 KB n=2000
72 Correct 17 ms 24268 KB n=2000
73 Correct 22 ms 24300 KB n=2000
74 Correct 18 ms 24204 KB n=1844
75 Correct 24 ms 24292 KB n=2000
76 Correct 16 ms 24188 KB n=2000
77 Correct 19 ms 24204 KB n=2000
78 Correct 18 ms 24284 KB n=2000
79 Correct 18 ms 24204 KB n=2000
80 Correct 18 ms 24344 KB n=2000
81 Correct 23 ms 24328 KB n=2000
82 Correct 22 ms 24288 KB n=2000
83 Correct 21 ms 24364 KB n=2000
84 Correct 18 ms 24204 KB n=2000
85 Correct 21 ms 24196 KB n=2000
86 Correct 19 ms 24268 KB n=2000
87 Correct 17 ms 24208 KB n=2000
88 Correct 19 ms 24344 KB n=2000
89 Correct 18 ms 24336 KB n=2000
90 Correct 17 ms 24400 KB n=2000
91 Correct 19 ms 24296 KB n=2000
92 Correct 881 ms 74440 KB n=200000
93 Correct 1156 ms 81036 KB n=200000
94 Correct 1118 ms 85428 KB n=200000
95 Correct 820 ms 74220 KB n=200000
96 Correct 834 ms 74308 KB n=200000
97 Correct 1123 ms 79864 KB n=200000
98 Correct 874 ms 74464 KB n=200000
99 Correct 1028 ms 74592 KB n=200000
100 Correct 820 ms 74432 KB n=200000
101 Correct 1057 ms 87128 KB n=200000
102 Correct 670 ms 75512 KB n=200000
103 Correct 622 ms 75564 KB n=200000
104 Correct 713 ms 75548 KB n=200000
105 Correct 756 ms 75844 KB n=200000
106 Correct 728 ms 75904 KB n=200000
107 Correct 696 ms 75888 KB n=200000
108 Correct 872 ms 74592 KB n=200000
109 Correct 895 ms 74400 KB n=200000
110 Correct 930 ms 74420 KB n=200000
111 Correct 859 ms 73748 KB n=200000
112 Correct 1244 ms 85732 KB n=200000
113 Correct 1184 ms 79748 KB n=200000
114 Correct 806 ms 73896 KB n=200000
115 Correct 1189 ms 76900 KB n=200000
116 Correct 796 ms 74580 KB n=200000
117 Correct 1010 ms 86196 KB n=200000
118 Correct 1124 ms 78312 KB n=200000
119 Correct 810 ms 74420 KB n=200000
120 Correct 1014 ms 86084 KB n=200000
121 Correct 1008 ms 86108 KB n=200000
122 Correct 998 ms 86440 KB n=200000
123 Correct 673 ms 75764 KB n=200000
124 Correct 261 ms 38980 KB n=25264