답안 #951265

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
951265 2024-03-21T14:05:17 Z LOLOLO Birthday gift (IZhO18_treearray) C++17
100 / 100
920 ms 138412 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 5e5 + 100;
vector <int> pos[N], ed[N];
int p[N][20], in[N], ou[N], timer = 1, a[N];

void dfs(int u, int v) {
    in[u] = timer++;
    p[u][0] = v;
    for (int i = 1; i < 20; i++)
        p[u][i] = p[p[u][i - 1]][i - 1];

    for (auto x : ed[u]) {
        if (x == v)
            continue;

        dfs(x, u);
    }
    ou[u] = timer;
}

bool is(int a, int b) {
    return in[a] <= in[b] && ou[a] >= ou[b];
}

int lca(int a, int b) {
    if (is(a, b))
        return a;

    if (is(b, a))
        return b;

    for (int i = 19; i >= 0; i--) {
        if (is(p[a][i], b) == 0)
            a = p[a][i];
    }

    return p[a][0];
}

set <int> adj[N], el[N];

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

    int n, m, q;
    cin >> n >> m >> q;

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        ed[a].pb(b);
        ed[b].pb(a);
    }

    dfs(1, 1);
    for (int i = 1; i <= m; i++) {
        cin >> a[i];
        el[a[i]].insert(i);
    }

    for (int i = 1; i < m; i++) {
        adj[lca(a[i], a[i + 1])].insert(i);
    }

    for (int i = 1; i <= q; i++) {
        int t;
        cin >> t;

        if (t == 1) {
            int p, v;
            cin >> p >> v;
            el[a[p]].erase(p);
            el[v].insert(p);

            if (p < m) {
                adj[lca(a[p], a[p + 1])].erase(p);
                adj[lca(v, a[p + 1])].insert(p);
            }

            if (p > 1) {
                adj[lca(a[p - 1], a[p])].erase(p - 1);
                adj[lca(v, a[p - 1])].insert(p - 1);
            }
            a[p] = v;
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            auto it = el[v].lower_bound(l);
            if (it != el[v].end() && *it <= r) {
                cout << *it << " " << *it << '\n';
                continue;
            }

            it = adj[v].lower_bound(l);
            if (it != adj[v].end() && *it < r) {
                cout << *it << " " << *it + 1 << '\n';
                continue;
            }

            cout << -1 << " " << -1 << '\n';
        }
    }

    return 0;
}

/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3 
2 1 3 1
1 3 5 
2 3 4 5
2 1 3 1
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 78932 KB n=5
2 Correct 19 ms 79044 KB n=100
3 Correct 17 ms 79192 KB n=100
4 Correct 18 ms 79452 KB n=100
5 Correct 18 ms 78940 KB n=100
6 Correct 18 ms 78940 KB n=100
7 Correct 18 ms 78940 KB n=100
8 Correct 16 ms 78948 KB n=100
9 Correct 18 ms 78940 KB n=100
10 Correct 17 ms 79192 KB n=100
11 Correct 20 ms 78936 KB n=100
12 Correct 20 ms 78940 KB n=100
13 Correct 19 ms 78940 KB n=100
14 Correct 17 ms 79220 KB n=100
15 Correct 19 ms 78940 KB n=100
16 Correct 18 ms 78936 KB n=100
17 Correct 16 ms 78940 KB n=100
18 Correct 17 ms 78940 KB n=100
19 Correct 19 ms 79196 KB n=100
20 Correct 16 ms 78936 KB n=100
21 Correct 16 ms 78940 KB n=100
22 Correct 17 ms 79192 KB n=100
23 Correct 19 ms 78936 KB n=100
24 Correct 20 ms 78940 KB n=100
25 Correct 16 ms 78940 KB n=100
26 Correct 19 ms 78940 KB n=12
27 Correct 18 ms 78992 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 78932 KB n=5
2 Correct 19 ms 79044 KB n=100
3 Correct 17 ms 79192 KB n=100
4 Correct 18 ms 79452 KB n=100
5 Correct 18 ms 78940 KB n=100
6 Correct 18 ms 78940 KB n=100
7 Correct 18 ms 78940 KB n=100
8 Correct 16 ms 78948 KB n=100
9 Correct 18 ms 78940 KB n=100
10 Correct 17 ms 79192 KB n=100
11 Correct 20 ms 78936 KB n=100
12 Correct 20 ms 78940 KB n=100
13 Correct 19 ms 78940 KB n=100
14 Correct 17 ms 79220 KB n=100
15 Correct 19 ms 78940 KB n=100
16 Correct 18 ms 78936 KB n=100
17 Correct 16 ms 78940 KB n=100
18 Correct 17 ms 78940 KB n=100
19 Correct 19 ms 79196 KB n=100
20 Correct 16 ms 78936 KB n=100
21 Correct 16 ms 78940 KB n=100
22 Correct 17 ms 79192 KB n=100
23 Correct 19 ms 78936 KB n=100
24 Correct 20 ms 78940 KB n=100
25 Correct 16 ms 78940 KB n=100
26 Correct 19 ms 78940 KB n=12
27 Correct 18 ms 78992 KB n=100
28 Correct 18 ms 79004 KB n=500
29 Correct 19 ms 78996 KB n=500
30 Correct 17 ms 78940 KB n=500
31 Correct 21 ms 79104 KB n=500
32 Correct 18 ms 78940 KB n=500
33 Correct 17 ms 78940 KB n=500
34 Correct 20 ms 78936 KB n=500
35 Correct 18 ms 78940 KB n=500
36 Correct 17 ms 79004 KB n=500
37 Correct 17 ms 78936 KB n=500
38 Correct 17 ms 78936 KB n=500
39 Correct 18 ms 79196 KB n=500
40 Correct 21 ms 79004 KB n=500
41 Correct 17 ms 78936 KB n=500
42 Correct 18 ms 78984 KB n=500
43 Correct 17 ms 78940 KB n=500
44 Correct 19 ms 79196 KB n=500
45 Correct 18 ms 78940 KB n=500
46 Correct 17 ms 78940 KB n=500
47 Correct 18 ms 79100 KB n=500
48 Correct 17 ms 79008 KB n=500
49 Correct 20 ms 78936 KB n=500
50 Correct 18 ms 78940 KB n=500
51 Correct 17 ms 78940 KB n=500
52 Correct 17 ms 78936 KB n=500
53 Correct 17 ms 78984 KB n=500
54 Correct 17 ms 78936 KB n=500
55 Correct 17 ms 78940 KB n=278
56 Correct 17 ms 78940 KB n=500
57 Correct 18 ms 79076 KB n=500
58 Correct 20 ms 78936 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 78932 KB n=5
2 Correct 19 ms 79044 KB n=100
3 Correct 17 ms 79192 KB n=100
4 Correct 18 ms 79452 KB n=100
5 Correct 18 ms 78940 KB n=100
6 Correct 18 ms 78940 KB n=100
7 Correct 18 ms 78940 KB n=100
8 Correct 16 ms 78948 KB n=100
9 Correct 18 ms 78940 KB n=100
10 Correct 17 ms 79192 KB n=100
11 Correct 20 ms 78936 KB n=100
12 Correct 20 ms 78940 KB n=100
13 Correct 19 ms 78940 KB n=100
14 Correct 17 ms 79220 KB n=100
15 Correct 19 ms 78940 KB n=100
16 Correct 18 ms 78936 KB n=100
17 Correct 16 ms 78940 KB n=100
18 Correct 17 ms 78940 KB n=100
19 Correct 19 ms 79196 KB n=100
20 Correct 16 ms 78936 KB n=100
21 Correct 16 ms 78940 KB n=100
22 Correct 17 ms 79192 KB n=100
23 Correct 19 ms 78936 KB n=100
24 Correct 20 ms 78940 KB n=100
25 Correct 16 ms 78940 KB n=100
26 Correct 19 ms 78940 KB n=12
27 Correct 18 ms 78992 KB n=100
28 Correct 18 ms 79004 KB n=500
29 Correct 19 ms 78996 KB n=500
30 Correct 17 ms 78940 KB n=500
31 Correct 21 ms 79104 KB n=500
32 Correct 18 ms 78940 KB n=500
33 Correct 17 ms 78940 KB n=500
34 Correct 20 ms 78936 KB n=500
35 Correct 18 ms 78940 KB n=500
36 Correct 17 ms 79004 KB n=500
37 Correct 17 ms 78936 KB n=500
38 Correct 17 ms 78936 KB n=500
39 Correct 18 ms 79196 KB n=500
40 Correct 21 ms 79004 KB n=500
41 Correct 17 ms 78936 KB n=500
42 Correct 18 ms 78984 KB n=500
43 Correct 17 ms 78940 KB n=500
44 Correct 19 ms 79196 KB n=500
45 Correct 18 ms 78940 KB n=500
46 Correct 17 ms 78940 KB n=500
47 Correct 18 ms 79100 KB n=500
48 Correct 17 ms 79008 KB n=500
49 Correct 20 ms 78936 KB n=500
50 Correct 18 ms 78940 KB n=500
51 Correct 17 ms 78940 KB n=500
52 Correct 17 ms 78936 KB n=500
53 Correct 17 ms 78984 KB n=500
54 Correct 17 ms 78936 KB n=500
55 Correct 17 ms 78940 KB n=278
56 Correct 17 ms 78940 KB n=500
57 Correct 18 ms 79076 KB n=500
58 Correct 20 ms 78936 KB n=500
59 Correct 19 ms 79196 KB n=2000
60 Correct 22 ms 79192 KB n=2000
61 Correct 21 ms 79448 KB n=2000
62 Correct 20 ms 79192 KB n=2000
63 Correct 19 ms 79192 KB n=2000
64 Correct 19 ms 79192 KB n=2000
65 Correct 19 ms 79192 KB n=2000
66 Correct 18 ms 79196 KB n=2000
67 Correct 22 ms 79196 KB n=2000
68 Correct 19 ms 79192 KB n=2000
69 Correct 19 ms 79196 KB n=2000
70 Correct 19 ms 79196 KB n=2000
71 Correct 19 ms 79332 KB n=2000
72 Correct 18 ms 79196 KB n=2000
73 Correct 19 ms 79196 KB n=2000
74 Correct 19 ms 79192 KB n=1844
75 Correct 20 ms 79452 KB n=2000
76 Correct 19 ms 79196 KB n=2000
77 Correct 23 ms 79264 KB n=2000
78 Correct 22 ms 79192 KB n=2000
79 Correct 19 ms 79196 KB n=2000
80 Correct 19 ms 79192 KB n=2000
81 Correct 19 ms 79196 KB n=2000
82 Correct 20 ms 79196 KB n=2000
83 Correct 19 ms 79192 KB n=2000
84 Correct 22 ms 79200 KB n=2000
85 Correct 19 ms 79192 KB n=2000
86 Correct 20 ms 79344 KB n=2000
87 Correct 20 ms 79452 KB n=2000
88 Correct 19 ms 79448 KB n=2000
89 Correct 18 ms 79192 KB n=2000
90 Correct 18 ms 79264 KB n=2000
91 Correct 19 ms 79196 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 78932 KB n=5
2 Correct 19 ms 79044 KB n=100
3 Correct 17 ms 79192 KB n=100
4 Correct 18 ms 79452 KB n=100
5 Correct 18 ms 78940 KB n=100
6 Correct 18 ms 78940 KB n=100
7 Correct 18 ms 78940 KB n=100
8 Correct 16 ms 78948 KB n=100
9 Correct 18 ms 78940 KB n=100
10 Correct 17 ms 79192 KB n=100
11 Correct 20 ms 78936 KB n=100
12 Correct 20 ms 78940 KB n=100
13 Correct 19 ms 78940 KB n=100
14 Correct 17 ms 79220 KB n=100
15 Correct 19 ms 78940 KB n=100
16 Correct 18 ms 78936 KB n=100
17 Correct 16 ms 78940 KB n=100
18 Correct 17 ms 78940 KB n=100
19 Correct 19 ms 79196 KB n=100
20 Correct 16 ms 78936 KB n=100
21 Correct 16 ms 78940 KB n=100
22 Correct 17 ms 79192 KB n=100
23 Correct 19 ms 78936 KB n=100
24 Correct 20 ms 78940 KB n=100
25 Correct 16 ms 78940 KB n=100
26 Correct 19 ms 78940 KB n=12
27 Correct 18 ms 78992 KB n=100
28 Correct 18 ms 79004 KB n=500
29 Correct 19 ms 78996 KB n=500
30 Correct 17 ms 78940 KB n=500
31 Correct 21 ms 79104 KB n=500
32 Correct 18 ms 78940 KB n=500
33 Correct 17 ms 78940 KB n=500
34 Correct 20 ms 78936 KB n=500
35 Correct 18 ms 78940 KB n=500
36 Correct 17 ms 79004 KB n=500
37 Correct 17 ms 78936 KB n=500
38 Correct 17 ms 78936 KB n=500
39 Correct 18 ms 79196 KB n=500
40 Correct 21 ms 79004 KB n=500
41 Correct 17 ms 78936 KB n=500
42 Correct 18 ms 78984 KB n=500
43 Correct 17 ms 78940 KB n=500
44 Correct 19 ms 79196 KB n=500
45 Correct 18 ms 78940 KB n=500
46 Correct 17 ms 78940 KB n=500
47 Correct 18 ms 79100 KB n=500
48 Correct 17 ms 79008 KB n=500
49 Correct 20 ms 78936 KB n=500
50 Correct 18 ms 78940 KB n=500
51 Correct 17 ms 78940 KB n=500
52 Correct 17 ms 78936 KB n=500
53 Correct 17 ms 78984 KB n=500
54 Correct 17 ms 78936 KB n=500
55 Correct 17 ms 78940 KB n=278
56 Correct 17 ms 78940 KB n=500
57 Correct 18 ms 79076 KB n=500
58 Correct 20 ms 78936 KB n=500
59 Correct 19 ms 79196 KB n=2000
60 Correct 22 ms 79192 KB n=2000
61 Correct 21 ms 79448 KB n=2000
62 Correct 20 ms 79192 KB n=2000
63 Correct 19 ms 79192 KB n=2000
64 Correct 19 ms 79192 KB n=2000
65 Correct 19 ms 79192 KB n=2000
66 Correct 18 ms 79196 KB n=2000
67 Correct 22 ms 79196 KB n=2000
68 Correct 19 ms 79192 KB n=2000
69 Correct 19 ms 79196 KB n=2000
70 Correct 19 ms 79196 KB n=2000
71 Correct 19 ms 79332 KB n=2000
72 Correct 18 ms 79196 KB n=2000
73 Correct 19 ms 79196 KB n=2000
74 Correct 19 ms 79192 KB n=1844
75 Correct 20 ms 79452 KB n=2000
76 Correct 19 ms 79196 KB n=2000
77 Correct 23 ms 79264 KB n=2000
78 Correct 22 ms 79192 KB n=2000
79 Correct 19 ms 79196 KB n=2000
80 Correct 19 ms 79192 KB n=2000
81 Correct 19 ms 79196 KB n=2000
82 Correct 20 ms 79196 KB n=2000
83 Correct 19 ms 79192 KB n=2000
84 Correct 22 ms 79200 KB n=2000
85 Correct 19 ms 79192 KB n=2000
86 Correct 20 ms 79344 KB n=2000
87 Correct 20 ms 79452 KB n=2000
88 Correct 19 ms 79448 KB n=2000
89 Correct 18 ms 79192 KB n=2000
90 Correct 18 ms 79264 KB n=2000
91 Correct 19 ms 79196 KB n=2000
92 Correct 540 ms 128960 KB n=200000
93 Correct 622 ms 133688 KB n=200000
94 Correct 467 ms 137040 KB n=200000
95 Correct 521 ms 128712 KB n=200000
96 Correct 600 ms 129072 KB n=200000
97 Correct 715 ms 133124 KB n=200000
98 Correct 594 ms 128964 KB n=200000
99 Correct 598 ms 128848 KB n=200000
100 Correct 565 ms 129064 KB n=200000
101 Correct 439 ms 138412 KB n=200000
102 Correct 307 ms 129876 KB n=200000
103 Correct 293 ms 129872 KB n=200000
104 Correct 338 ms 129936 KB n=200000
105 Correct 325 ms 130380 KB n=200000
106 Correct 300 ms 130384 KB n=200000
107 Correct 305 ms 130384 KB n=200000
108 Correct 539 ms 128852 KB n=200000
109 Correct 576 ms 128656 KB n=200000
110 Correct 588 ms 128724 KB n=200000
111 Correct 556 ms 128076 KB n=200000
112 Correct 460 ms 137264 KB n=200000
113 Correct 734 ms 132912 KB n=200000
114 Correct 609 ms 128200 KB n=200000
115 Correct 920 ms 130804 KB n=200000
116 Correct 570 ms 128912 KB n=200000
117 Correct 449 ms 137848 KB n=200000
118 Correct 764 ms 131612 KB n=200000
119 Correct 550 ms 129164 KB n=200000
120 Correct 389 ms 137396 KB n=200000
121 Correct 357 ms 137268 KB n=200000
122 Correct 437 ms 137544 KB n=200000
123 Correct 341 ms 130144 KB n=200000
124 Correct 117 ms 93496 KB n=25264