답안 #992539

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
992539 2024-06-04T15:59:54 Z VMaksimoski008 Birthday gift (IZhO18_treearray) C++17
100 / 100
967 ms 82772 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
 
const int LOG = 20;
const int maxn = 2e5 + 5;
 
int n, m, q, D[maxn], up[maxn][LOG];
vector<int> G[maxn], V(maxn);
set<int> S[maxn], M[maxn];
 
void dfs(int u, int p) {
    for(int i=1; i<LOG; i++) up[u][i] = up[up[u][i-1]][i-1];
 
    for(int &v : G[u]) {
        if(v == p) continue;
        up[v][0] = u;
        D[v] = D[u] + 1;
        dfs(v, u);
    }
}
 
int jmp(int x, int d) {
    for(int j=LOG-1; j>=0; j--)
        if(d & (1 << j)) x = up[x][j];
    return x;
}
 
int get_lca(int a, int b) {
    if(D[a] < D[b]) swap(a, b);
 
    a = jmp(a, D[a] - D[b]);
    if(a == b) return a;
    for(int j=LOG-1; j>=0; j--)
        if(up[a][j] != up[b][j])
            a = up[a][j], b = up[b][j];
 
    return up[a][0];
}
 
int main() {
    cin >> n >> m >> q;
 
    for(int i=0; i<n-1; i++) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
 
    dfs(1, 0);
 
    for(int i=1; i<=m; i++) {
        cin >> V[i];
    }

    for(int i=1; i<=m; i++) S[V[i]].insert(i);
    for(int i=1; i+1<=m; i++) M[get_lca(V[i], V[i+1])].insert(i);
 
    while(q--) {
        int t;
        cin >> t;
 
        if(t == 1) {
            int p, v;
            cin >> p >> v;
            S[V[p]].erase(p);
            if(p < m) M[get_lca(V[p], V[p+1])].erase(p);
            if(p) M[get_lca(V[p], V[p-1])].erase(p-1);
            V[p] = v;
            S[V[p]].insert(p);
            if(p < m) M[get_lca(V[p], V[p+1])].insert(p);
            if(p) M[get_lca(V[p], V[p-1])].insert(p-1);
        } else {
            int l, r, v;
            cin >> l >> r >> v;
 
            auto it = S[v].lower_bound(l);
            if(it != S[v].end() && *it <= r && *it >= l) {
                cout << *it << " " << *it << '\n';
                continue;
            } 
 
            auto it2 = M[v].lower_bound(l);
            if(it2 != M[v].end() && *it2 < r && *it2 >= l) {
                cout << *it2 << " " << *it2 + 1 << '\n';
                continue;
            }

            cout << "-1 -1" << '\n';
        }
    }
 
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 25948 KB n=5
2 Correct 4 ms 25948 KB n=100
3 Correct 4 ms 25948 KB n=100
4 Correct 4 ms 25948 KB n=100
5 Correct 4 ms 25948 KB n=100
6 Correct 4 ms 25836 KB n=100
7 Correct 4 ms 25948 KB n=100
8 Correct 4 ms 26016 KB n=100
9 Correct 4 ms 25944 KB n=100
10 Correct 4 ms 25948 KB n=100
11 Correct 4 ms 25948 KB n=100
12 Correct 4 ms 26040 KB n=100
13 Correct 4 ms 25944 KB n=100
14 Correct 4 ms 25896 KB n=100
15 Correct 4 ms 25948 KB n=100
16 Correct 4 ms 25948 KB n=100
17 Correct 4 ms 25944 KB n=100
18 Correct 5 ms 25948 KB n=100
19 Correct 4 ms 25824 KB n=100
20 Correct 5 ms 25948 KB n=100
21 Correct 5 ms 25948 KB n=100
22 Correct 4 ms 25948 KB n=100
23 Correct 4 ms 25948 KB n=100
24 Correct 5 ms 26036 KB n=100
25 Correct 4 ms 25948 KB n=100
26 Correct 4 ms 25948 KB n=12
27 Correct 5 ms 25948 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 25948 KB n=5
2 Correct 4 ms 25948 KB n=100
3 Correct 4 ms 25948 KB n=100
4 Correct 4 ms 25948 KB n=100
5 Correct 4 ms 25948 KB n=100
6 Correct 4 ms 25836 KB n=100
7 Correct 4 ms 25948 KB n=100
8 Correct 4 ms 26016 KB n=100
9 Correct 4 ms 25944 KB n=100
10 Correct 4 ms 25948 KB n=100
11 Correct 4 ms 25948 KB n=100
12 Correct 4 ms 26040 KB n=100
13 Correct 4 ms 25944 KB n=100
14 Correct 4 ms 25896 KB n=100
15 Correct 4 ms 25948 KB n=100
16 Correct 4 ms 25948 KB n=100
17 Correct 4 ms 25944 KB n=100
18 Correct 5 ms 25948 KB n=100
19 Correct 4 ms 25824 KB n=100
20 Correct 5 ms 25948 KB n=100
21 Correct 5 ms 25948 KB n=100
22 Correct 4 ms 25948 KB n=100
23 Correct 4 ms 25948 KB n=100
24 Correct 5 ms 26036 KB n=100
25 Correct 4 ms 25948 KB n=100
26 Correct 4 ms 25948 KB n=12
27 Correct 5 ms 25948 KB n=100
28 Correct 5 ms 25948 KB n=500
29 Correct 7 ms 26012 KB n=500
30 Correct 4 ms 26088 KB n=500
31 Correct 5 ms 25948 KB n=500
32 Correct 5 ms 25948 KB n=500
33 Correct 5 ms 25948 KB n=500
34 Correct 5 ms 25944 KB n=500
35 Correct 5 ms 25948 KB n=500
36 Correct 5 ms 26044 KB n=500
37 Correct 5 ms 25948 KB n=500
38 Correct 5 ms 25944 KB n=500
39 Correct 5 ms 25948 KB n=500
40 Correct 7 ms 25948 KB n=500
41 Correct 6 ms 25944 KB n=500
42 Correct 5 ms 25944 KB n=500
43 Correct 5 ms 25948 KB n=500
44 Correct 5 ms 25944 KB n=500
45 Correct 5 ms 25944 KB n=500
46 Correct 6 ms 25944 KB n=500
47 Correct 5 ms 25944 KB n=500
48 Correct 5 ms 25944 KB n=500
49 Correct 5 ms 25944 KB n=500
50 Correct 5 ms 25948 KB n=500
51 Correct 5 ms 25948 KB n=500
52 Correct 5 ms 25948 KB n=500
53 Correct 5 ms 25948 KB n=500
54 Correct 5 ms 26200 KB n=500
55 Correct 4 ms 26036 KB n=278
56 Correct 5 ms 25948 KB n=500
57 Correct 5 ms 25948 KB n=500
58 Correct 5 ms 25948 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 25948 KB n=5
2 Correct 4 ms 25948 KB n=100
3 Correct 4 ms 25948 KB n=100
4 Correct 4 ms 25948 KB n=100
5 Correct 4 ms 25948 KB n=100
6 Correct 4 ms 25836 KB n=100
7 Correct 4 ms 25948 KB n=100
8 Correct 4 ms 26016 KB n=100
9 Correct 4 ms 25944 KB n=100
10 Correct 4 ms 25948 KB n=100
11 Correct 4 ms 25948 KB n=100
12 Correct 4 ms 26040 KB n=100
13 Correct 4 ms 25944 KB n=100
14 Correct 4 ms 25896 KB n=100
15 Correct 4 ms 25948 KB n=100
16 Correct 4 ms 25948 KB n=100
17 Correct 4 ms 25944 KB n=100
18 Correct 5 ms 25948 KB n=100
19 Correct 4 ms 25824 KB n=100
20 Correct 5 ms 25948 KB n=100
21 Correct 5 ms 25948 KB n=100
22 Correct 4 ms 25948 KB n=100
23 Correct 4 ms 25948 KB n=100
24 Correct 5 ms 26036 KB n=100
25 Correct 4 ms 25948 KB n=100
26 Correct 4 ms 25948 KB n=12
27 Correct 5 ms 25948 KB n=100
28 Correct 5 ms 25948 KB n=500
29 Correct 7 ms 26012 KB n=500
30 Correct 4 ms 26088 KB n=500
31 Correct 5 ms 25948 KB n=500
32 Correct 5 ms 25948 KB n=500
33 Correct 5 ms 25948 KB n=500
34 Correct 5 ms 25944 KB n=500
35 Correct 5 ms 25948 KB n=500
36 Correct 5 ms 26044 KB n=500
37 Correct 5 ms 25948 KB n=500
38 Correct 5 ms 25944 KB n=500
39 Correct 5 ms 25948 KB n=500
40 Correct 7 ms 25948 KB n=500
41 Correct 6 ms 25944 KB n=500
42 Correct 5 ms 25944 KB n=500
43 Correct 5 ms 25948 KB n=500
44 Correct 5 ms 25944 KB n=500
45 Correct 5 ms 25944 KB n=500
46 Correct 6 ms 25944 KB n=500
47 Correct 5 ms 25944 KB n=500
48 Correct 5 ms 25944 KB n=500
49 Correct 5 ms 25944 KB n=500
50 Correct 5 ms 25948 KB n=500
51 Correct 5 ms 25948 KB n=500
52 Correct 5 ms 25948 KB n=500
53 Correct 5 ms 25948 KB n=500
54 Correct 5 ms 26200 KB n=500
55 Correct 4 ms 26036 KB n=278
56 Correct 5 ms 25948 KB n=500
57 Correct 5 ms 25948 KB n=500
58 Correct 5 ms 25948 KB n=500
59 Correct 7 ms 26204 KB n=2000
60 Correct 8 ms 26204 KB n=2000
61 Correct 8 ms 26200 KB n=2000
62 Correct 8 ms 26200 KB n=2000
63 Correct 8 ms 26204 KB n=2000
64 Correct 8 ms 26204 KB n=2000
65 Correct 7 ms 26204 KB n=2000
66 Correct 8 ms 26204 KB n=2000
67 Correct 8 ms 26204 KB n=2000
68 Correct 8 ms 26204 KB n=2000
69 Correct 9 ms 26204 KB n=2000
70 Correct 10 ms 26204 KB n=2000
71 Correct 8 ms 26296 KB n=2000
72 Correct 9 ms 26204 KB n=2000
73 Correct 8 ms 26200 KB n=2000
74 Correct 8 ms 26204 KB n=1844
75 Correct 8 ms 26204 KB n=2000
76 Correct 8 ms 26204 KB n=2000
77 Correct 8 ms 26204 KB n=2000
78 Correct 8 ms 26204 KB n=2000
79 Correct 8 ms 26204 KB n=2000
80 Correct 8 ms 26204 KB n=2000
81 Correct 8 ms 26204 KB n=2000
82 Correct 8 ms 26204 KB n=2000
83 Correct 10 ms 26204 KB n=2000
84 Correct 7 ms 26200 KB n=2000
85 Correct 9 ms 26204 KB n=2000
86 Correct 8 ms 26204 KB n=2000
87 Correct 8 ms 26204 KB n=2000
88 Correct 8 ms 26204 KB n=2000
89 Correct 9 ms 26200 KB n=2000
90 Correct 9 ms 26460 KB n=2000
91 Correct 8 ms 26204 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 25948 KB n=5
2 Correct 4 ms 25948 KB n=100
3 Correct 4 ms 25948 KB n=100
4 Correct 4 ms 25948 KB n=100
5 Correct 4 ms 25948 KB n=100
6 Correct 4 ms 25836 KB n=100
7 Correct 4 ms 25948 KB n=100
8 Correct 4 ms 26016 KB n=100
9 Correct 4 ms 25944 KB n=100
10 Correct 4 ms 25948 KB n=100
11 Correct 4 ms 25948 KB n=100
12 Correct 4 ms 26040 KB n=100
13 Correct 4 ms 25944 KB n=100
14 Correct 4 ms 25896 KB n=100
15 Correct 4 ms 25948 KB n=100
16 Correct 4 ms 25948 KB n=100
17 Correct 4 ms 25944 KB n=100
18 Correct 5 ms 25948 KB n=100
19 Correct 4 ms 25824 KB n=100
20 Correct 5 ms 25948 KB n=100
21 Correct 5 ms 25948 KB n=100
22 Correct 4 ms 25948 KB n=100
23 Correct 4 ms 25948 KB n=100
24 Correct 5 ms 26036 KB n=100
25 Correct 4 ms 25948 KB n=100
26 Correct 4 ms 25948 KB n=12
27 Correct 5 ms 25948 KB n=100
28 Correct 5 ms 25948 KB n=500
29 Correct 7 ms 26012 KB n=500
30 Correct 4 ms 26088 KB n=500
31 Correct 5 ms 25948 KB n=500
32 Correct 5 ms 25948 KB n=500
33 Correct 5 ms 25948 KB n=500
34 Correct 5 ms 25944 KB n=500
35 Correct 5 ms 25948 KB n=500
36 Correct 5 ms 26044 KB n=500
37 Correct 5 ms 25948 KB n=500
38 Correct 5 ms 25944 KB n=500
39 Correct 5 ms 25948 KB n=500
40 Correct 7 ms 25948 KB n=500
41 Correct 6 ms 25944 KB n=500
42 Correct 5 ms 25944 KB n=500
43 Correct 5 ms 25948 KB n=500
44 Correct 5 ms 25944 KB n=500
45 Correct 5 ms 25944 KB n=500
46 Correct 6 ms 25944 KB n=500
47 Correct 5 ms 25944 KB n=500
48 Correct 5 ms 25944 KB n=500
49 Correct 5 ms 25944 KB n=500
50 Correct 5 ms 25948 KB n=500
51 Correct 5 ms 25948 KB n=500
52 Correct 5 ms 25948 KB n=500
53 Correct 5 ms 25948 KB n=500
54 Correct 5 ms 26200 KB n=500
55 Correct 4 ms 26036 KB n=278
56 Correct 5 ms 25948 KB n=500
57 Correct 5 ms 25948 KB n=500
58 Correct 5 ms 25948 KB n=500
59 Correct 7 ms 26204 KB n=2000
60 Correct 8 ms 26204 KB n=2000
61 Correct 8 ms 26200 KB n=2000
62 Correct 8 ms 26200 KB n=2000
63 Correct 8 ms 26204 KB n=2000
64 Correct 8 ms 26204 KB n=2000
65 Correct 7 ms 26204 KB n=2000
66 Correct 8 ms 26204 KB n=2000
67 Correct 8 ms 26204 KB n=2000
68 Correct 8 ms 26204 KB n=2000
69 Correct 9 ms 26204 KB n=2000
70 Correct 10 ms 26204 KB n=2000
71 Correct 8 ms 26296 KB n=2000
72 Correct 9 ms 26204 KB n=2000
73 Correct 8 ms 26200 KB n=2000
74 Correct 8 ms 26204 KB n=1844
75 Correct 8 ms 26204 KB n=2000
76 Correct 8 ms 26204 KB n=2000
77 Correct 8 ms 26204 KB n=2000
78 Correct 8 ms 26204 KB n=2000
79 Correct 8 ms 26204 KB n=2000
80 Correct 8 ms 26204 KB n=2000
81 Correct 8 ms 26204 KB n=2000
82 Correct 8 ms 26204 KB n=2000
83 Correct 10 ms 26204 KB n=2000
84 Correct 7 ms 26200 KB n=2000
85 Correct 9 ms 26204 KB n=2000
86 Correct 8 ms 26204 KB n=2000
87 Correct 8 ms 26204 KB n=2000
88 Correct 8 ms 26204 KB n=2000
89 Correct 9 ms 26200 KB n=2000
90 Correct 9 ms 26460 KB n=2000
91 Correct 8 ms 26204 KB n=2000
92 Correct 640 ms 67924 KB n=200000
93 Correct 723 ms 74936 KB n=200000
94 Correct 748 ms 80464 KB n=200000
95 Correct 670 ms 67844 KB n=200000
96 Correct 596 ms 68040 KB n=200000
97 Correct 967 ms 73300 KB n=200000
98 Correct 630 ms 68112 KB n=200000
99 Correct 680 ms 67436 KB n=200000
100 Correct 600 ms 68124 KB n=200000
101 Correct 690 ms 82516 KB n=200000
102 Correct 569 ms 68940 KB n=200000
103 Correct 596 ms 68840 KB n=200000
104 Correct 574 ms 68944 KB n=200000
105 Correct 590 ms 68692 KB n=200000
106 Correct 617 ms 68632 KB n=200000
107 Correct 602 ms 68756 KB n=200000
108 Correct 688 ms 67672 KB n=200000
109 Correct 687 ms 67580 KB n=200000
110 Correct 696 ms 67640 KB n=200000
111 Correct 639 ms 67780 KB n=200000
112 Correct 761 ms 80768 KB n=200000
113 Correct 758 ms 73296 KB n=200000
114 Correct 645 ms 67780 KB n=200000
115 Correct 783 ms 69716 KB n=200000
116 Correct 665 ms 67784 KB n=200000
117 Correct 752 ms 81488 KB n=200000
118 Correct 767 ms 71508 KB n=200000
119 Correct 672 ms 67780 KB n=200000
120 Correct 738 ms 82244 KB n=200000
121 Correct 660 ms 82348 KB n=200000
122 Correct 882 ms 82772 KB n=200000
123 Correct 613 ms 68180 KB n=200000
124 Correct 191 ms 39336 KB n=25264