답안 #671863

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
671863 2022-12-14T05:42:58 Z Alihan_8 Birthday gift (IZhO18_treearray) C++17
100 / 100
1395 ms 95184 KB
#include <bits/stdc++.h>
// include <ext/pb_ds/assoc_container.hpp>
// include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
// define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>
#define mpr make_pair
#define ln '\n'
void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
#define int long long
const int N = 2e5+1;
vector <int> g[N];
int val[N], lift[N][20], depth[N];
void dfs(int n, int p){
    depth[n] = depth[p]+1;
    lift[n][0] = p;
    for ( int i = 1; i < 20; i++ ){
        lift[n][i] = lift[lift[n][i-1]][i-1];
    }
    for ( auto to: g[n] ){
        if ( to == p ) continue;
        dfs(to, n);
    }
}
int _lca(int l, int r){
    if ( depth[l] < depth[r] ) swap(l, r);
    int dif = depth[l]-depth[r];
    for ( int i = 19; i >= 0; i-- ){
        if ( dif >> i & 1 ) l = lift[l][i];
    }
    if ( l == r ) return l;
    for ( int i = 19; i >= 0; i-- ){
        if ( lift[l][i] == lift[r][i] ) continue;
        l = lift[l][i], r = lift[r][i];
    }
    return lift[l][0];
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m, q; cin >> n >> m >> q;
    vector <set<int>> st(N);
    set <pair<int,int>> lca_list;
    for ( int i = 1; i < n; i++ ){
        int x, y; cin >> x >> y;
        g[x].pb(y), g[y].pb(x);
    }
    dfs(1, 0);
    for ( int i = 1; i <= m; i++ ){
        cin >> val[i];
        st[val[i]].insert(i);
        if ( i > 1 ) lca_list.insert({_lca(val[i-1], val[i]), i-1});
    }
    while ( q-- ){
        int t; cin >> t;
        if ( t == 1 ){
            int pos, v; cin >> pos >> v;
            st[val[pos]].erase(pos);
            st[v].insert(pos);
            if ( pos > 1 ){
                lca_list.erase({_lca(val[pos-1], val[pos]), pos-1});
                lca_list.insert({_lca(val[pos-1], v), pos-1});
            }
            if ( pos+1 <= m ){
                lca_list.erase({_lca(val[pos+1], val[pos]), pos});
                lca_list.insert({_lca(val[pos+1], v), pos});
            }
            val[pos] = v;
        }
        else{
            int l, r, x; cin >> l >> r >> x;
            auto f = st[x].lower_bound(l);
            if ( f != st[x].end() and *f <= r ){
                cout << *f << ' ' << *f << ln;
                continue;
            }
            auto it = lca_list.lower_bound({x, l});
            if ( it->first == x and it->second < r ) cout << it->second << ' ' << it->second+1 << ln;
            else cout << "-1 -1\n";
        }
    }

    cout << '\n';
}

Compilation message

treearray.cpp: In function 'void IO(std::string)':
treearray.cpp:11:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:11:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14372 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14420 KB n=100
5 Correct 7 ms 14420 KB n=100
6 Correct 7 ms 14420 KB n=100
7 Correct 8 ms 14456 KB n=100
8 Correct 8 ms 14364 KB n=100
9 Correct 8 ms 14364 KB n=100
10 Correct 8 ms 14420 KB n=100
11 Correct 8 ms 14420 KB n=100
12 Correct 8 ms 14420 KB n=100
13 Correct 8 ms 14368 KB n=100
14 Correct 9 ms 14420 KB n=100
15 Correct 8 ms 14420 KB n=100
16 Correct 7 ms 14368 KB n=100
17 Correct 8 ms 14452 KB n=100
18 Correct 8 ms 14368 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 8 ms 14420 KB n=100
21 Correct 9 ms 14420 KB n=100
22 Correct 8 ms 14420 KB n=100
23 Correct 8 ms 14420 KB n=100
24 Correct 8 ms 14420 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14420 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14372 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14420 KB n=100
5 Correct 7 ms 14420 KB n=100
6 Correct 7 ms 14420 KB n=100
7 Correct 8 ms 14456 KB n=100
8 Correct 8 ms 14364 KB n=100
9 Correct 8 ms 14364 KB n=100
10 Correct 8 ms 14420 KB n=100
11 Correct 8 ms 14420 KB n=100
12 Correct 8 ms 14420 KB n=100
13 Correct 8 ms 14368 KB n=100
14 Correct 9 ms 14420 KB n=100
15 Correct 8 ms 14420 KB n=100
16 Correct 7 ms 14368 KB n=100
17 Correct 8 ms 14452 KB n=100
18 Correct 8 ms 14368 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 8 ms 14420 KB n=100
21 Correct 9 ms 14420 KB n=100
22 Correct 8 ms 14420 KB n=100
23 Correct 8 ms 14420 KB n=100
24 Correct 8 ms 14420 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14420 KB n=100
28 Correct 9 ms 14500 KB n=500
29 Correct 8 ms 14548 KB n=500
30 Correct 8 ms 14508 KB n=500
31 Correct 9 ms 14584 KB n=500
32 Correct 8 ms 14548 KB n=500
33 Correct 8 ms 14508 KB n=500
34 Correct 8 ms 14504 KB n=500
35 Correct 8 ms 14548 KB n=500
36 Correct 8 ms 14620 KB n=500
37 Correct 10 ms 14592 KB n=500
38 Correct 8 ms 14548 KB n=500
39 Correct 8 ms 14508 KB n=500
40 Correct 8 ms 14548 KB n=500
41 Correct 8 ms 14548 KB n=500
42 Correct 8 ms 14504 KB n=500
43 Correct 9 ms 14548 KB n=500
44 Correct 8 ms 14548 KB n=500
45 Correct 8 ms 14548 KB n=500
46 Correct 10 ms 14548 KB n=500
47 Correct 10 ms 14548 KB n=500
48 Correct 8 ms 14548 KB n=500
49 Correct 8 ms 14576 KB n=500
50 Correct 8 ms 14548 KB n=500
51 Correct 8 ms 14548 KB n=500
52 Correct 9 ms 14508 KB n=500
53 Correct 8 ms 14548 KB n=500
54 Correct 8 ms 14624 KB n=500
55 Correct 8 ms 14500 KB n=278
56 Correct 9 ms 14548 KB n=500
57 Correct 8 ms 14504 KB n=500
58 Correct 9 ms 14532 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14372 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14420 KB n=100
5 Correct 7 ms 14420 KB n=100
6 Correct 7 ms 14420 KB n=100
7 Correct 8 ms 14456 KB n=100
8 Correct 8 ms 14364 KB n=100
9 Correct 8 ms 14364 KB n=100
10 Correct 8 ms 14420 KB n=100
11 Correct 8 ms 14420 KB n=100
12 Correct 8 ms 14420 KB n=100
13 Correct 8 ms 14368 KB n=100
14 Correct 9 ms 14420 KB n=100
15 Correct 8 ms 14420 KB n=100
16 Correct 7 ms 14368 KB n=100
17 Correct 8 ms 14452 KB n=100
18 Correct 8 ms 14368 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 8 ms 14420 KB n=100
21 Correct 9 ms 14420 KB n=100
22 Correct 8 ms 14420 KB n=100
23 Correct 8 ms 14420 KB n=100
24 Correct 8 ms 14420 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14420 KB n=100
28 Correct 9 ms 14500 KB n=500
29 Correct 8 ms 14548 KB n=500
30 Correct 8 ms 14508 KB n=500
31 Correct 9 ms 14584 KB n=500
32 Correct 8 ms 14548 KB n=500
33 Correct 8 ms 14508 KB n=500
34 Correct 8 ms 14504 KB n=500
35 Correct 8 ms 14548 KB n=500
36 Correct 8 ms 14620 KB n=500
37 Correct 10 ms 14592 KB n=500
38 Correct 8 ms 14548 KB n=500
39 Correct 8 ms 14508 KB n=500
40 Correct 8 ms 14548 KB n=500
41 Correct 8 ms 14548 KB n=500
42 Correct 8 ms 14504 KB n=500
43 Correct 9 ms 14548 KB n=500
44 Correct 8 ms 14548 KB n=500
45 Correct 8 ms 14548 KB n=500
46 Correct 10 ms 14548 KB n=500
47 Correct 10 ms 14548 KB n=500
48 Correct 8 ms 14548 KB n=500
49 Correct 8 ms 14576 KB n=500
50 Correct 8 ms 14548 KB n=500
51 Correct 8 ms 14548 KB n=500
52 Correct 9 ms 14508 KB n=500
53 Correct 8 ms 14548 KB n=500
54 Correct 8 ms 14624 KB n=500
55 Correct 8 ms 14500 KB n=278
56 Correct 9 ms 14548 KB n=500
57 Correct 8 ms 14504 KB n=500
58 Correct 9 ms 14532 KB n=500
59 Correct 11 ms 15120 KB n=2000
60 Correct 11 ms 15188 KB n=2000
61 Correct 11 ms 15188 KB n=2000
62 Correct 11 ms 15060 KB n=2000
63 Correct 10 ms 15060 KB n=2000
64 Correct 12 ms 15188 KB n=2000
65 Correct 11 ms 15016 KB n=2000
66 Correct 11 ms 15188 KB n=2000
67 Correct 11 ms 15060 KB n=2000
68 Correct 11 ms 15060 KB n=2000
69 Correct 10 ms 15060 KB n=2000
70 Correct 10 ms 15148 KB n=2000
71 Correct 10 ms 15060 KB n=2000
72 Correct 10 ms 15060 KB n=2000
73 Correct 10 ms 15060 KB n=2000
74 Correct 11 ms 15060 KB n=1844
75 Correct 10 ms 15148 KB n=2000
76 Correct 11 ms 15060 KB n=2000
77 Correct 11 ms 15060 KB n=2000
78 Correct 11 ms 15152 KB n=2000
79 Correct 11 ms 15060 KB n=2000
80 Correct 11 ms 15144 KB n=2000
81 Correct 12 ms 15092 KB n=2000
82 Correct 10 ms 15060 KB n=2000
83 Correct 11 ms 15076 KB n=2000
84 Correct 11 ms 15140 KB n=2000
85 Correct 11 ms 15060 KB n=2000
86 Correct 11 ms 15060 KB n=2000
87 Correct 11 ms 15060 KB n=2000
88 Correct 11 ms 15148 KB n=2000
89 Correct 11 ms 15148 KB n=2000
90 Correct 11 ms 15188 KB n=2000
91 Correct 10 ms 15104 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14372 KB n=100
3 Correct 8 ms 14420 KB n=100
4 Correct 8 ms 14420 KB n=100
5 Correct 7 ms 14420 KB n=100
6 Correct 7 ms 14420 KB n=100
7 Correct 8 ms 14456 KB n=100
8 Correct 8 ms 14364 KB n=100
9 Correct 8 ms 14364 KB n=100
10 Correct 8 ms 14420 KB n=100
11 Correct 8 ms 14420 KB n=100
12 Correct 8 ms 14420 KB n=100
13 Correct 8 ms 14368 KB n=100
14 Correct 9 ms 14420 KB n=100
15 Correct 8 ms 14420 KB n=100
16 Correct 7 ms 14368 KB n=100
17 Correct 8 ms 14452 KB n=100
18 Correct 8 ms 14368 KB n=100
19 Correct 8 ms 14420 KB n=100
20 Correct 8 ms 14420 KB n=100
21 Correct 9 ms 14420 KB n=100
22 Correct 8 ms 14420 KB n=100
23 Correct 8 ms 14420 KB n=100
24 Correct 8 ms 14420 KB n=100
25 Correct 8 ms 14420 KB n=100
26 Correct 8 ms 14420 KB n=12
27 Correct 8 ms 14420 KB n=100
28 Correct 9 ms 14500 KB n=500
29 Correct 8 ms 14548 KB n=500
30 Correct 8 ms 14508 KB n=500
31 Correct 9 ms 14584 KB n=500
32 Correct 8 ms 14548 KB n=500
33 Correct 8 ms 14508 KB n=500
34 Correct 8 ms 14504 KB n=500
35 Correct 8 ms 14548 KB n=500
36 Correct 8 ms 14620 KB n=500
37 Correct 10 ms 14592 KB n=500
38 Correct 8 ms 14548 KB n=500
39 Correct 8 ms 14508 KB n=500
40 Correct 8 ms 14548 KB n=500
41 Correct 8 ms 14548 KB n=500
42 Correct 8 ms 14504 KB n=500
43 Correct 9 ms 14548 KB n=500
44 Correct 8 ms 14548 KB n=500
45 Correct 8 ms 14548 KB n=500
46 Correct 10 ms 14548 KB n=500
47 Correct 10 ms 14548 KB n=500
48 Correct 8 ms 14548 KB n=500
49 Correct 8 ms 14576 KB n=500
50 Correct 8 ms 14548 KB n=500
51 Correct 8 ms 14548 KB n=500
52 Correct 9 ms 14508 KB n=500
53 Correct 8 ms 14548 KB n=500
54 Correct 8 ms 14624 KB n=500
55 Correct 8 ms 14500 KB n=278
56 Correct 9 ms 14548 KB n=500
57 Correct 8 ms 14504 KB n=500
58 Correct 9 ms 14532 KB n=500
59 Correct 11 ms 15120 KB n=2000
60 Correct 11 ms 15188 KB n=2000
61 Correct 11 ms 15188 KB n=2000
62 Correct 11 ms 15060 KB n=2000
63 Correct 10 ms 15060 KB n=2000
64 Correct 12 ms 15188 KB n=2000
65 Correct 11 ms 15016 KB n=2000
66 Correct 11 ms 15188 KB n=2000
67 Correct 11 ms 15060 KB n=2000
68 Correct 11 ms 15060 KB n=2000
69 Correct 10 ms 15060 KB n=2000
70 Correct 10 ms 15148 KB n=2000
71 Correct 10 ms 15060 KB n=2000
72 Correct 10 ms 15060 KB n=2000
73 Correct 10 ms 15060 KB n=2000
74 Correct 11 ms 15060 KB n=1844
75 Correct 10 ms 15148 KB n=2000
76 Correct 11 ms 15060 KB n=2000
77 Correct 11 ms 15060 KB n=2000
78 Correct 11 ms 15152 KB n=2000
79 Correct 11 ms 15060 KB n=2000
80 Correct 11 ms 15144 KB n=2000
81 Correct 12 ms 15092 KB n=2000
82 Correct 10 ms 15060 KB n=2000
83 Correct 11 ms 15076 KB n=2000
84 Correct 11 ms 15140 KB n=2000
85 Correct 11 ms 15060 KB n=2000
86 Correct 11 ms 15060 KB n=2000
87 Correct 11 ms 15060 KB n=2000
88 Correct 11 ms 15148 KB n=2000
89 Correct 11 ms 15148 KB n=2000
90 Correct 11 ms 15188 KB n=2000
91 Correct 10 ms 15104 KB n=2000
92 Correct 692 ms 86336 KB n=200000
93 Correct 1312 ms 91096 KB n=200000
94 Correct 1334 ms 94076 KB n=200000
95 Correct 695 ms 86036 KB n=200000
96 Correct 670 ms 86100 KB n=200000
97 Correct 1298 ms 90368 KB n=200000
98 Correct 672 ms 86148 KB n=200000
99 Correct 877 ms 86540 KB n=200000
100 Correct 695 ms 86132 KB n=200000
101 Correct 1276 ms 95184 KB n=200000
102 Correct 434 ms 87532 KB n=200000
103 Correct 445 ms 87520 KB n=200000
104 Correct 450 ms 87528 KB n=200000
105 Correct 455 ms 87960 KB n=200000
106 Correct 446 ms 87880 KB n=200000
107 Correct 443 ms 87892 KB n=200000
108 Correct 810 ms 86368 KB n=200000
109 Correct 810 ms 86396 KB n=200000
110 Correct 831 ms 86468 KB n=200000
111 Correct 703 ms 85596 KB n=200000
112 Correct 1274 ms 94356 KB n=200000
113 Correct 1302 ms 90512 KB n=200000
114 Correct 683 ms 85564 KB n=200000
115 Correct 1274 ms 88252 KB n=200000
116 Correct 739 ms 86180 KB n=200000
117 Correct 1395 ms 94552 KB n=200000
118 Correct 1352 ms 89200 KB n=200000
119 Correct 643 ms 86168 KB n=200000
120 Correct 1180 ms 94236 KB n=200000
121 Correct 1209 ms 94184 KB n=200000
122 Correct 1231 ms 94308 KB n=200000
123 Correct 450 ms 87676 KB n=200000
124 Correct 291 ms 33200 KB n=25264