답안 #1096263

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1096263 2024-10-04T07:26:58 Z Zero_OP Birthday gift (IZhO18_treearray) C++14
100 / 100
515 ms 127312 KB
#include "bits/stdc++.h"

using namespace std;

#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define rall(v) rbegin(v), rend(v)

const int MAX = 4e5 + 5;

int n, m, q, timerdfs, a[MAX], adjLCA[MAX], depth[MAX], pos[MAX], st[20][MAX];
vector<int> adj[MAX];
set<int> one[MAX], two[MAX];

int query(set<int>& S, int l, int r){
    auto it = S.lower_bound(l);
    if(it == S.end()) return -1;
    if(*it <= r) return *it;
    return -1;
}

void dfs(int u){
    st[0][pos[u] = timerdfs++] = u;
    for(int v : adj[u]){
        adj[v].erase(find(all(adj[v]), u));
        depth[v] = depth[u] + 1;
        dfs(v);
        st[0][timerdfs++] = u;
    }
}

int f(int u, int v){
    return depth[u] < depth[v] ? u : v;
}

void prepareRMQ(){
    for(int i = 1; (1 << i) <= timerdfs; ++i){
        for(int j = 0; j + (1 << i) <= timerdfs; ++j){
            st[i][j] = f(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
        }
    }
}

int getLCA(int u, int v){
    u = pos[u]; v = pos[v];
    if(u > v) swap(u, v);
    int k = 31 - __builtin_clz(v - u + 1);
    return f(st[k][u], st[k][v - (1 << k) + 1]);
}

void deleteAdj(int i){
    two[adjLCA[i]].erase(i);
}

void refine(int i){
    adjLCA[i] = getLCA(a[i], a[i + 1]);
    two[adjLCA[i]].insert(i);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    if(fopen("task.inp", "r")){
        freopen("task.inp", "r", stdin);
        freopen("task.out", "w", stdout);
    }

    cin >> n >> m >> q;
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v); adj[v].push_back(u);
    }

    for(int i = 1; i <= m; ++i){
        cin >> a[i];
    }

    dfs(1);
    prepareRMQ();

    for(int i = 1; i <= m; ++i){
        one[a[i]].insert(i);
        if(i < m) {
            adjLCA[i] = getLCA(a[i], a[i + 1]);
            two[adjLCA[i]].insert(i);
        }
    }

    while(q--){
        int type;
        cin >> type;
        if(type == 1){
            int pos, val;
            cin >> pos >> val;

            one[a[pos]].erase(pos);
            a[pos] = val;
            one[a[pos]].insert(pos);

            if(pos < m) deleteAdj(pos), refine(pos);
            if(pos > 1) deleteAdj(pos - 1), refine(pos - 1);

        } else{
            int l, r, x;
            cin >> l >> r >> x;

            int q1 = query(one[x], l, r);
            if(q1 != -1){
                cout << q1 << ' ' << q1 << '\n';
//                cout << "YES\n";
                continue;
            }

            int q2 = query(two[x], l, r - 1);
            if(q2 != -1){
                cout << q2 << ' ' << q2 + 1 << '\n';
//                cout << "YES\n";
                continue;
            } else{
//                cout << "NO\n";
                cout << -1 << ' ' << -1 << '\n';
            }
        }
    }

    return 0;
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         freopen("task.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         freopen("task.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 47452 KB n=5
2 Correct 20 ms 47452 KB n=100
3 Correct 19 ms 47452 KB n=100
4 Correct 19 ms 47416 KB n=100
5 Correct 19 ms 47452 KB n=100
6 Correct 19 ms 47452 KB n=100
7 Correct 19 ms 47448 KB n=100
8 Correct 19 ms 47452 KB n=100
9 Correct 22 ms 47440 KB n=100
10 Correct 19 ms 47448 KB n=100
11 Correct 19 ms 47380 KB n=100
12 Correct 20 ms 47452 KB n=100
13 Correct 20 ms 47704 KB n=100
14 Correct 20 ms 47356 KB n=100
15 Correct 22 ms 47452 KB n=100
16 Correct 21 ms 47428 KB n=100
17 Correct 19 ms 47452 KB n=100
18 Correct 20 ms 47448 KB n=100
19 Correct 20 ms 47452 KB n=100
20 Correct 21 ms 47448 KB n=100
21 Correct 19 ms 47452 KB n=100
22 Correct 20 ms 47448 KB n=100
23 Correct 21 ms 47452 KB n=100
24 Correct 20 ms 47448 KB n=100
25 Correct 20 ms 47452 KB n=100
26 Correct 21 ms 47452 KB n=12
27 Correct 22 ms 47452 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 47452 KB n=5
2 Correct 20 ms 47452 KB n=100
3 Correct 19 ms 47452 KB n=100
4 Correct 19 ms 47416 KB n=100
5 Correct 19 ms 47452 KB n=100
6 Correct 19 ms 47452 KB n=100
7 Correct 19 ms 47448 KB n=100
8 Correct 19 ms 47452 KB n=100
9 Correct 22 ms 47440 KB n=100
10 Correct 19 ms 47448 KB n=100
11 Correct 19 ms 47380 KB n=100
12 Correct 20 ms 47452 KB n=100
13 Correct 20 ms 47704 KB n=100
14 Correct 20 ms 47356 KB n=100
15 Correct 22 ms 47452 KB n=100
16 Correct 21 ms 47428 KB n=100
17 Correct 19 ms 47452 KB n=100
18 Correct 20 ms 47448 KB n=100
19 Correct 20 ms 47452 KB n=100
20 Correct 21 ms 47448 KB n=100
21 Correct 19 ms 47452 KB n=100
22 Correct 20 ms 47448 KB n=100
23 Correct 21 ms 47452 KB n=100
24 Correct 20 ms 47448 KB n=100
25 Correct 20 ms 47452 KB n=100
26 Correct 21 ms 47452 KB n=12
27 Correct 22 ms 47452 KB n=100
28 Correct 20 ms 47448 KB n=500
29 Correct 19 ms 47656 KB n=500
30 Correct 21 ms 47432 KB n=500
31 Correct 20 ms 47448 KB n=500
32 Correct 20 ms 47452 KB n=500
33 Correct 25 ms 47452 KB n=500
34 Correct 20 ms 47448 KB n=500
35 Correct 20 ms 47452 KB n=500
36 Correct 22 ms 47448 KB n=500
37 Correct 27 ms 47456 KB n=500
38 Correct 20 ms 47448 KB n=500
39 Correct 20 ms 47452 KB n=500
40 Correct 19 ms 47452 KB n=500
41 Correct 20 ms 47452 KB n=500
42 Correct 20 ms 47448 KB n=500
43 Correct 21 ms 47452 KB n=500
44 Correct 19 ms 47644 KB n=500
45 Correct 20 ms 47452 KB n=500
46 Correct 20 ms 47452 KB n=500
47 Correct 20 ms 47452 KB n=500
48 Correct 20 ms 47452 KB n=500
49 Correct 20 ms 47468 KB n=500
50 Correct 20 ms 47448 KB n=500
51 Correct 20 ms 47416 KB n=500
52 Correct 20 ms 47452 KB n=500
53 Correct 20 ms 47452 KB n=500
54 Correct 20 ms 47436 KB n=500
55 Correct 23 ms 47440 KB n=278
56 Correct 20 ms 47452 KB n=500
57 Correct 20 ms 47452 KB n=500
58 Correct 19 ms 47576 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 47452 KB n=5
2 Correct 20 ms 47452 KB n=100
3 Correct 19 ms 47452 KB n=100
4 Correct 19 ms 47416 KB n=100
5 Correct 19 ms 47452 KB n=100
6 Correct 19 ms 47452 KB n=100
7 Correct 19 ms 47448 KB n=100
8 Correct 19 ms 47452 KB n=100
9 Correct 22 ms 47440 KB n=100
10 Correct 19 ms 47448 KB n=100
11 Correct 19 ms 47380 KB n=100
12 Correct 20 ms 47452 KB n=100
13 Correct 20 ms 47704 KB n=100
14 Correct 20 ms 47356 KB n=100
15 Correct 22 ms 47452 KB n=100
16 Correct 21 ms 47428 KB n=100
17 Correct 19 ms 47452 KB n=100
18 Correct 20 ms 47448 KB n=100
19 Correct 20 ms 47452 KB n=100
20 Correct 21 ms 47448 KB n=100
21 Correct 19 ms 47452 KB n=100
22 Correct 20 ms 47448 KB n=100
23 Correct 21 ms 47452 KB n=100
24 Correct 20 ms 47448 KB n=100
25 Correct 20 ms 47452 KB n=100
26 Correct 21 ms 47452 KB n=12
27 Correct 22 ms 47452 KB n=100
28 Correct 20 ms 47448 KB n=500
29 Correct 19 ms 47656 KB n=500
30 Correct 21 ms 47432 KB n=500
31 Correct 20 ms 47448 KB n=500
32 Correct 20 ms 47452 KB n=500
33 Correct 25 ms 47452 KB n=500
34 Correct 20 ms 47448 KB n=500
35 Correct 20 ms 47452 KB n=500
36 Correct 22 ms 47448 KB n=500
37 Correct 27 ms 47456 KB n=500
38 Correct 20 ms 47448 KB n=500
39 Correct 20 ms 47452 KB n=500
40 Correct 19 ms 47452 KB n=500
41 Correct 20 ms 47452 KB n=500
42 Correct 20 ms 47448 KB n=500
43 Correct 21 ms 47452 KB n=500
44 Correct 19 ms 47644 KB n=500
45 Correct 20 ms 47452 KB n=500
46 Correct 20 ms 47452 KB n=500
47 Correct 20 ms 47452 KB n=500
48 Correct 20 ms 47452 KB n=500
49 Correct 20 ms 47468 KB n=500
50 Correct 20 ms 47448 KB n=500
51 Correct 20 ms 47416 KB n=500
52 Correct 20 ms 47452 KB n=500
53 Correct 20 ms 47452 KB n=500
54 Correct 20 ms 47436 KB n=500
55 Correct 23 ms 47440 KB n=278
56 Correct 20 ms 47452 KB n=500
57 Correct 20 ms 47452 KB n=500
58 Correct 19 ms 47576 KB n=500
59 Correct 23 ms 47964 KB n=2000
60 Correct 22 ms 47964 KB n=2000
61 Correct 22 ms 47960 KB n=2000
62 Correct 23 ms 47952 KB n=2000
63 Correct 22 ms 47960 KB n=2000
64 Correct 23 ms 47964 KB n=2000
65 Correct 22 ms 47964 KB n=2000
66 Correct 22 ms 47960 KB n=2000
67 Correct 22 ms 47964 KB n=2000
68 Correct 23 ms 47964 KB n=2000
69 Correct 24 ms 47952 KB n=2000
70 Correct 22 ms 47964 KB n=2000
71 Correct 22 ms 47940 KB n=2000
72 Correct 21 ms 47960 KB n=2000
73 Correct 24 ms 47956 KB n=2000
74 Correct 21 ms 47964 KB n=1844
75 Correct 22 ms 48036 KB n=2000
76 Correct 22 ms 47960 KB n=2000
77 Correct 21 ms 47964 KB n=2000
78 Correct 21 ms 47964 KB n=2000
79 Correct 21 ms 47960 KB n=2000
80 Correct 21 ms 47964 KB n=2000
81 Correct 21 ms 47964 KB n=2000
82 Correct 22 ms 47960 KB n=2000
83 Correct 22 ms 47960 KB n=2000
84 Correct 21 ms 47820 KB n=2000
85 Correct 21 ms 47952 KB n=2000
86 Correct 22 ms 47960 KB n=2000
87 Correct 22 ms 47960 KB n=2000
88 Correct 21 ms 47964 KB n=2000
89 Correct 21 ms 47964 KB n=2000
90 Correct 23 ms 47956 KB n=2000
91 Correct 22 ms 47964 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 47452 KB n=5
2 Correct 20 ms 47452 KB n=100
3 Correct 19 ms 47452 KB n=100
4 Correct 19 ms 47416 KB n=100
5 Correct 19 ms 47452 KB n=100
6 Correct 19 ms 47452 KB n=100
7 Correct 19 ms 47448 KB n=100
8 Correct 19 ms 47452 KB n=100
9 Correct 22 ms 47440 KB n=100
10 Correct 19 ms 47448 KB n=100
11 Correct 19 ms 47380 KB n=100
12 Correct 20 ms 47452 KB n=100
13 Correct 20 ms 47704 KB n=100
14 Correct 20 ms 47356 KB n=100
15 Correct 22 ms 47452 KB n=100
16 Correct 21 ms 47428 KB n=100
17 Correct 19 ms 47452 KB n=100
18 Correct 20 ms 47448 KB n=100
19 Correct 20 ms 47452 KB n=100
20 Correct 21 ms 47448 KB n=100
21 Correct 19 ms 47452 KB n=100
22 Correct 20 ms 47448 KB n=100
23 Correct 21 ms 47452 KB n=100
24 Correct 20 ms 47448 KB n=100
25 Correct 20 ms 47452 KB n=100
26 Correct 21 ms 47452 KB n=12
27 Correct 22 ms 47452 KB n=100
28 Correct 20 ms 47448 KB n=500
29 Correct 19 ms 47656 KB n=500
30 Correct 21 ms 47432 KB n=500
31 Correct 20 ms 47448 KB n=500
32 Correct 20 ms 47452 KB n=500
33 Correct 25 ms 47452 KB n=500
34 Correct 20 ms 47448 KB n=500
35 Correct 20 ms 47452 KB n=500
36 Correct 22 ms 47448 KB n=500
37 Correct 27 ms 47456 KB n=500
38 Correct 20 ms 47448 KB n=500
39 Correct 20 ms 47452 KB n=500
40 Correct 19 ms 47452 KB n=500
41 Correct 20 ms 47452 KB n=500
42 Correct 20 ms 47448 KB n=500
43 Correct 21 ms 47452 KB n=500
44 Correct 19 ms 47644 KB n=500
45 Correct 20 ms 47452 KB n=500
46 Correct 20 ms 47452 KB n=500
47 Correct 20 ms 47452 KB n=500
48 Correct 20 ms 47452 KB n=500
49 Correct 20 ms 47468 KB n=500
50 Correct 20 ms 47448 KB n=500
51 Correct 20 ms 47416 KB n=500
52 Correct 20 ms 47452 KB n=500
53 Correct 20 ms 47452 KB n=500
54 Correct 20 ms 47436 KB n=500
55 Correct 23 ms 47440 KB n=278
56 Correct 20 ms 47452 KB n=500
57 Correct 20 ms 47452 KB n=500
58 Correct 19 ms 47576 KB n=500
59 Correct 23 ms 47964 KB n=2000
60 Correct 22 ms 47964 KB n=2000
61 Correct 22 ms 47960 KB n=2000
62 Correct 23 ms 47952 KB n=2000
63 Correct 22 ms 47960 KB n=2000
64 Correct 23 ms 47964 KB n=2000
65 Correct 22 ms 47964 KB n=2000
66 Correct 22 ms 47960 KB n=2000
67 Correct 22 ms 47964 KB n=2000
68 Correct 23 ms 47964 KB n=2000
69 Correct 24 ms 47952 KB n=2000
70 Correct 22 ms 47964 KB n=2000
71 Correct 22 ms 47940 KB n=2000
72 Correct 21 ms 47960 KB n=2000
73 Correct 24 ms 47956 KB n=2000
74 Correct 21 ms 47964 KB n=1844
75 Correct 22 ms 48036 KB n=2000
76 Correct 22 ms 47960 KB n=2000
77 Correct 21 ms 47964 KB n=2000
78 Correct 21 ms 47964 KB n=2000
79 Correct 21 ms 47960 KB n=2000
80 Correct 21 ms 47964 KB n=2000
81 Correct 21 ms 47964 KB n=2000
82 Correct 22 ms 47960 KB n=2000
83 Correct 22 ms 47960 KB n=2000
84 Correct 21 ms 47820 KB n=2000
85 Correct 21 ms 47952 KB n=2000
86 Correct 22 ms 47960 KB n=2000
87 Correct 22 ms 47960 KB n=2000
88 Correct 21 ms 47964 KB n=2000
89 Correct 21 ms 47964 KB n=2000
90 Correct 23 ms 47956 KB n=2000
91 Correct 22 ms 47964 KB n=2000
92 Correct 396 ms 111672 KB n=200000
93 Correct 412 ms 119892 KB n=200000
94 Correct 369 ms 125520 KB n=200000
95 Correct 414 ms 111560 KB n=200000
96 Correct 362 ms 111612 KB n=200000
97 Correct 361 ms 118096 KB n=200000
98 Correct 356 ms 111560 KB n=200000
99 Correct 422 ms 111696 KB n=200000
100 Correct 371 ms 111820 KB n=200000
101 Correct 365 ms 127312 KB n=200000
102 Correct 252 ms 112680 KB n=200000
103 Correct 243 ms 112720 KB n=200000
104 Correct 244 ms 112724 KB n=200000
105 Correct 243 ms 113112 KB n=200000
106 Correct 241 ms 113232 KB n=200000
107 Correct 230 ms 113236 KB n=200000
108 Correct 370 ms 111532 KB n=200000
109 Correct 357 ms 111700 KB n=200000
110 Correct 385 ms 111980 KB n=200000
111 Correct 440 ms 110924 KB n=200000
112 Correct 383 ms 125704 KB n=200000
113 Correct 445 ms 118080 KB n=200000
114 Correct 460 ms 111348 KB n=200000
115 Correct 515 ms 114696 KB n=200000
116 Correct 438 ms 111812 KB n=200000
117 Correct 405 ms 126452 KB n=200000
118 Correct 469 ms 116308 KB n=200000
119 Correct 432 ms 111816 KB n=200000
120 Correct 361 ms 126508 KB n=200000
121 Correct 464 ms 126516 KB n=200000
122 Correct 382 ms 126804 KB n=200000
123 Correct 273 ms 112900 KB n=200000
124 Correct 151 ms 64336 KB n=25264