답안 #894687

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894687 2023-12-28T17:24:30 Z Pekiban Birthday gift (IZhO18_treearray) C++17
100 / 100
815 ms 94260 KB
#include <bits/stdc++.h>

using namespace std;

const int mxN = 2e5, LOG = 18;
vector<int> g[mxN];
int up[LOG][mxN], dep[mxN], _m;
void dfs(int s, int e) {
    for (auto u : g[s]) {
        if (u == e) continue;
        up[0][u] = s;
        dep[u] = dep[s] + 1;
        dfs(u, s);
    }
}
void jump(int &s, int k) {
    for (int i = 0; i < LOG; ++i) {
        if ((1 << i) & k) {
            s = up[i][s];
        }
    }
}
int lca(int u, int v) {
    if (dep[u] < dep[v])    swap(u, v);
    jump(u, dep[u] - dep[v]);
    if (u == v) return u;
    for (int i = LOG-1; i >= 0; --i) {
        if (up[i][u] != up[i][v]) {
            u = up[i][u], v = up[i][v];
        }
    }
    return up[0][u];
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q; _m = m-1;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v), g[v].push_back(u);
    }
    dfs(0, -1);
    for (int i = 1; i < LOG; ++i) {
        for (int j = 0; j < n; ++j) {
            up[i][j] = up[i-1][up[i-1][j]];
        }
    }
    int a[m];
    multiset<int> ms[n], mms[n];
    vector<int> val(m);
    for (int i = 0; i < n; ++i) {
        ms[i].insert(1e9);
        mms[i].insert(1e9);
    } 
    for (int i = 0; i < m; ++i) {
        cin >> a[i];
        a[i]--;
        if (i) {
            val[i-1] = lca(a[i-1], a[i]);
            mms[val[i-1]].insert(i-1);
        }
        ms[a[i]].insert(i);
    }
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            int l, u;
            cin >> l >> u;
            l--, u--;
            if (l != m-1) {
                int x = lca(u, a[l+1]);
                mms[val[l]].erase(l);
                mms[x].insert(l);
                val[l] = x;
            }
            if (l) {
                int x = lca(u, a[l-1]);
                mms[val[l-1]].erase(l-1);
                mms[x].insert(l-1);
                val[l-1] = x;
            }
            ms[a[l]].erase(ms[a[l]].find(l));
            ms[u].insert(l);
            a[l] = u;
        }
        if (t == 2) {
            int l, r, v;
            cin >> l >> r >> v;
            l--, r--, v--;
            if (*ms[v].lower_bound(l) <= r) {
                cout << 1+*ms[v].lower_bound(l) << ' ' << 1+*ms[v].lower_bound(l) << '\n';
            }
            else if (*mms[v].lower_bound(l) <= r-1) {
                cout << 1 + *mms[v].lower_bound(l) << ' ' << 2+*mms[v].lower_bound(l) << '\n';
            }
            else {
                cout << -1 << ' ' << -1 << '\n';;
            }
        }
    }
    
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19804 KB n=5
2 Correct 3 ms 19804 KB n=100
3 Correct 4 ms 19824 KB n=100
4 Correct 4 ms 19804 KB n=100
5 Correct 3 ms 19800 KB n=100
6 Correct 3 ms 19804 KB n=100
7 Correct 4 ms 19804 KB n=100
8 Correct 3 ms 19804 KB n=100
9 Correct 3 ms 19872 KB n=100
10 Correct 3 ms 19804 KB n=100
11 Correct 3 ms 19800 KB n=100
12 Correct 3 ms 19804 KB n=100
13 Correct 3 ms 19804 KB n=100
14 Correct 3 ms 19804 KB n=100
15 Correct 3 ms 19800 KB n=100
16 Correct 3 ms 19804 KB n=100
17 Correct 3 ms 19800 KB n=100
18 Correct 3 ms 19804 KB n=100
19 Correct 3 ms 19800 KB n=100
20 Correct 3 ms 19804 KB n=100
21 Correct 3 ms 19804 KB n=100
22 Correct 3 ms 19804 KB n=100
23 Correct 3 ms 19804 KB n=100
24 Correct 3 ms 19804 KB n=100
25 Correct 3 ms 19804 KB n=100
26 Correct 3 ms 19804 KB n=12
27 Correct 3 ms 19804 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19804 KB n=5
2 Correct 3 ms 19804 KB n=100
3 Correct 4 ms 19824 KB n=100
4 Correct 4 ms 19804 KB n=100
5 Correct 3 ms 19800 KB n=100
6 Correct 3 ms 19804 KB n=100
7 Correct 4 ms 19804 KB n=100
8 Correct 3 ms 19804 KB n=100
9 Correct 3 ms 19872 KB n=100
10 Correct 3 ms 19804 KB n=100
11 Correct 3 ms 19800 KB n=100
12 Correct 3 ms 19804 KB n=100
13 Correct 3 ms 19804 KB n=100
14 Correct 3 ms 19804 KB n=100
15 Correct 3 ms 19800 KB n=100
16 Correct 3 ms 19804 KB n=100
17 Correct 3 ms 19800 KB n=100
18 Correct 3 ms 19804 KB n=100
19 Correct 3 ms 19800 KB n=100
20 Correct 3 ms 19804 KB n=100
21 Correct 3 ms 19804 KB n=100
22 Correct 3 ms 19804 KB n=100
23 Correct 3 ms 19804 KB n=100
24 Correct 3 ms 19804 KB n=100
25 Correct 3 ms 19804 KB n=100
26 Correct 3 ms 19804 KB n=12
27 Correct 3 ms 19804 KB n=100
28 Correct 4 ms 19804 KB n=500
29 Correct 4 ms 19896 KB n=500
30 Correct 4 ms 19804 KB n=500
31 Correct 4 ms 19804 KB n=500
32 Correct 4 ms 19804 KB n=500
33 Correct 4 ms 19804 KB n=500
34 Correct 4 ms 19804 KB n=500
35 Correct 4 ms 19804 KB n=500
36 Correct 4 ms 19884 KB n=500
37 Correct 4 ms 19804 KB n=500
38 Correct 4 ms 19804 KB n=500
39 Correct 4 ms 19800 KB n=500
40 Correct 3 ms 19800 KB n=500
41 Correct 4 ms 19800 KB n=500
42 Correct 4 ms 19804 KB n=500
43 Correct 3 ms 19804 KB n=500
44 Correct 4 ms 19804 KB n=500
45 Correct 4 ms 19804 KB n=500
46 Correct 4 ms 19804 KB n=500
47 Correct 4 ms 19804 KB n=500
48 Correct 4 ms 19800 KB n=500
49 Correct 4 ms 19804 KB n=500
50 Correct 4 ms 19804 KB n=500
51 Correct 4 ms 19804 KB n=500
52 Correct 4 ms 19804 KB n=500
53 Correct 4 ms 19804 KB n=500
54 Correct 4 ms 19804 KB n=500
55 Correct 3 ms 19804 KB n=278
56 Correct 4 ms 19804 KB n=500
57 Correct 3 ms 19800 KB n=500
58 Correct 4 ms 19804 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19804 KB n=5
2 Correct 3 ms 19804 KB n=100
3 Correct 4 ms 19824 KB n=100
4 Correct 4 ms 19804 KB n=100
5 Correct 3 ms 19800 KB n=100
6 Correct 3 ms 19804 KB n=100
7 Correct 4 ms 19804 KB n=100
8 Correct 3 ms 19804 KB n=100
9 Correct 3 ms 19872 KB n=100
10 Correct 3 ms 19804 KB n=100
11 Correct 3 ms 19800 KB n=100
12 Correct 3 ms 19804 KB n=100
13 Correct 3 ms 19804 KB n=100
14 Correct 3 ms 19804 KB n=100
15 Correct 3 ms 19800 KB n=100
16 Correct 3 ms 19804 KB n=100
17 Correct 3 ms 19800 KB n=100
18 Correct 3 ms 19804 KB n=100
19 Correct 3 ms 19800 KB n=100
20 Correct 3 ms 19804 KB n=100
21 Correct 3 ms 19804 KB n=100
22 Correct 3 ms 19804 KB n=100
23 Correct 3 ms 19804 KB n=100
24 Correct 3 ms 19804 KB n=100
25 Correct 3 ms 19804 KB n=100
26 Correct 3 ms 19804 KB n=12
27 Correct 3 ms 19804 KB n=100
28 Correct 4 ms 19804 KB n=500
29 Correct 4 ms 19896 KB n=500
30 Correct 4 ms 19804 KB n=500
31 Correct 4 ms 19804 KB n=500
32 Correct 4 ms 19804 KB n=500
33 Correct 4 ms 19804 KB n=500
34 Correct 4 ms 19804 KB n=500
35 Correct 4 ms 19804 KB n=500
36 Correct 4 ms 19884 KB n=500
37 Correct 4 ms 19804 KB n=500
38 Correct 4 ms 19804 KB n=500
39 Correct 4 ms 19800 KB n=500
40 Correct 3 ms 19800 KB n=500
41 Correct 4 ms 19800 KB n=500
42 Correct 4 ms 19804 KB n=500
43 Correct 3 ms 19804 KB n=500
44 Correct 4 ms 19804 KB n=500
45 Correct 4 ms 19804 KB n=500
46 Correct 4 ms 19804 KB n=500
47 Correct 4 ms 19804 KB n=500
48 Correct 4 ms 19800 KB n=500
49 Correct 4 ms 19804 KB n=500
50 Correct 4 ms 19804 KB n=500
51 Correct 4 ms 19804 KB n=500
52 Correct 4 ms 19804 KB n=500
53 Correct 4 ms 19804 KB n=500
54 Correct 4 ms 19804 KB n=500
55 Correct 3 ms 19804 KB n=278
56 Correct 4 ms 19804 KB n=500
57 Correct 3 ms 19800 KB n=500
58 Correct 4 ms 19804 KB n=500
59 Correct 6 ms 20572 KB n=2000
60 Correct 5 ms 20316 KB n=2000
61 Correct 6 ms 20316 KB n=2000
62 Correct 5 ms 20364 KB n=2000
63 Correct 5 ms 20572 KB n=2000
64 Correct 6 ms 20312 KB n=2000
65 Correct 6 ms 20316 KB n=2000
66 Correct 5 ms 20316 KB n=2000
67 Correct 6 ms 20316 KB n=2000
68 Correct 5 ms 20316 KB n=2000
69 Correct 5 ms 20568 KB n=2000
70 Correct 5 ms 20568 KB n=2000
71 Correct 5 ms 20572 KB n=2000
72 Correct 6 ms 20352 KB n=2000
73 Correct 5 ms 20572 KB n=2000
74 Correct 5 ms 20568 KB n=1844
75 Correct 5 ms 20568 KB n=2000
76 Correct 6 ms 20316 KB n=2000
77 Correct 6 ms 20572 KB n=2000
78 Correct 6 ms 20572 KB n=2000
79 Correct 5 ms 20484 KB n=2000
80 Correct 5 ms 20316 KB n=2000
81 Correct 5 ms 20316 KB n=2000
82 Correct 5 ms 20316 KB n=2000
83 Correct 5 ms 20360 KB n=2000
84 Correct 6 ms 20572 KB n=2000
85 Correct 5 ms 20316 KB n=2000
86 Correct 5 ms 20316 KB n=2000
87 Correct 5 ms 20316 KB n=2000
88 Correct 5 ms 20316 KB n=2000
89 Correct 5 ms 20316 KB n=2000
90 Correct 7 ms 20316 KB n=2000
91 Correct 5 ms 20572 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19804 KB n=5
2 Correct 3 ms 19804 KB n=100
3 Correct 4 ms 19824 KB n=100
4 Correct 4 ms 19804 KB n=100
5 Correct 3 ms 19800 KB n=100
6 Correct 3 ms 19804 KB n=100
7 Correct 4 ms 19804 KB n=100
8 Correct 3 ms 19804 KB n=100
9 Correct 3 ms 19872 KB n=100
10 Correct 3 ms 19804 KB n=100
11 Correct 3 ms 19800 KB n=100
12 Correct 3 ms 19804 KB n=100
13 Correct 3 ms 19804 KB n=100
14 Correct 3 ms 19804 KB n=100
15 Correct 3 ms 19800 KB n=100
16 Correct 3 ms 19804 KB n=100
17 Correct 3 ms 19800 KB n=100
18 Correct 3 ms 19804 KB n=100
19 Correct 3 ms 19800 KB n=100
20 Correct 3 ms 19804 KB n=100
21 Correct 3 ms 19804 KB n=100
22 Correct 3 ms 19804 KB n=100
23 Correct 3 ms 19804 KB n=100
24 Correct 3 ms 19804 KB n=100
25 Correct 3 ms 19804 KB n=100
26 Correct 3 ms 19804 KB n=12
27 Correct 3 ms 19804 KB n=100
28 Correct 4 ms 19804 KB n=500
29 Correct 4 ms 19896 KB n=500
30 Correct 4 ms 19804 KB n=500
31 Correct 4 ms 19804 KB n=500
32 Correct 4 ms 19804 KB n=500
33 Correct 4 ms 19804 KB n=500
34 Correct 4 ms 19804 KB n=500
35 Correct 4 ms 19804 KB n=500
36 Correct 4 ms 19884 KB n=500
37 Correct 4 ms 19804 KB n=500
38 Correct 4 ms 19804 KB n=500
39 Correct 4 ms 19800 KB n=500
40 Correct 3 ms 19800 KB n=500
41 Correct 4 ms 19800 KB n=500
42 Correct 4 ms 19804 KB n=500
43 Correct 3 ms 19804 KB n=500
44 Correct 4 ms 19804 KB n=500
45 Correct 4 ms 19804 KB n=500
46 Correct 4 ms 19804 KB n=500
47 Correct 4 ms 19804 KB n=500
48 Correct 4 ms 19800 KB n=500
49 Correct 4 ms 19804 KB n=500
50 Correct 4 ms 19804 KB n=500
51 Correct 4 ms 19804 KB n=500
52 Correct 4 ms 19804 KB n=500
53 Correct 4 ms 19804 KB n=500
54 Correct 4 ms 19804 KB n=500
55 Correct 3 ms 19804 KB n=278
56 Correct 4 ms 19804 KB n=500
57 Correct 3 ms 19800 KB n=500
58 Correct 4 ms 19804 KB n=500
59 Correct 6 ms 20572 KB n=2000
60 Correct 5 ms 20316 KB n=2000
61 Correct 6 ms 20316 KB n=2000
62 Correct 5 ms 20364 KB n=2000
63 Correct 5 ms 20572 KB n=2000
64 Correct 6 ms 20312 KB n=2000
65 Correct 6 ms 20316 KB n=2000
66 Correct 5 ms 20316 KB n=2000
67 Correct 6 ms 20316 KB n=2000
68 Correct 5 ms 20316 KB n=2000
69 Correct 5 ms 20568 KB n=2000
70 Correct 5 ms 20568 KB n=2000
71 Correct 5 ms 20572 KB n=2000
72 Correct 6 ms 20352 KB n=2000
73 Correct 5 ms 20572 KB n=2000
74 Correct 5 ms 20568 KB n=1844
75 Correct 5 ms 20568 KB n=2000
76 Correct 6 ms 20316 KB n=2000
77 Correct 6 ms 20572 KB n=2000
78 Correct 6 ms 20572 KB n=2000
79 Correct 5 ms 20484 KB n=2000
80 Correct 5 ms 20316 KB n=2000
81 Correct 5 ms 20316 KB n=2000
82 Correct 5 ms 20316 KB n=2000
83 Correct 5 ms 20360 KB n=2000
84 Correct 6 ms 20572 KB n=2000
85 Correct 5 ms 20316 KB n=2000
86 Correct 5 ms 20316 KB n=2000
87 Correct 5 ms 20316 KB n=2000
88 Correct 5 ms 20316 KB n=2000
89 Correct 5 ms 20316 KB n=2000
90 Correct 7 ms 20316 KB n=2000
91 Correct 5 ms 20572 KB n=2000
92 Correct 673 ms 92756 KB n=200000
93 Correct 710 ms 93312 KB n=200000
94 Correct 662 ms 92808 KB n=200000
95 Correct 659 ms 92388 KB n=200000
96 Correct 620 ms 92344 KB n=200000
97 Correct 767 ms 92752 KB n=200000
98 Correct 664 ms 92356 KB n=200000
99 Correct 758 ms 92920 KB n=200000
100 Correct 655 ms 92352 KB n=200000
101 Correct 636 ms 92752 KB n=200000
102 Correct 359 ms 93488 KB n=200000
103 Correct 357 ms 93524 KB n=200000
104 Correct 418 ms 93524 KB n=200000
105 Correct 365 ms 94036 KB n=200000
106 Correct 394 ms 93992 KB n=200000
107 Correct 360 ms 94260 KB n=200000
108 Correct 697 ms 92584 KB n=200000
109 Correct 655 ms 92640 KB n=200000
110 Correct 669 ms 92352 KB n=200000
111 Correct 622 ms 91904 KB n=200000
112 Correct 652 ms 92664 KB n=200000
113 Correct 719 ms 93008 KB n=200000
114 Correct 638 ms 92068 KB n=200000
115 Correct 815 ms 92948 KB n=200000
116 Correct 631 ms 92664 KB n=200000
117 Correct 630 ms 92496 KB n=200000
118 Correct 783 ms 92604 KB n=200000
119 Correct 615 ms 92612 KB n=200000
120 Correct 570 ms 91728 KB n=200000
121 Correct 579 ms 91732 KB n=200000
122 Correct 571 ms 91988 KB n=200000
123 Correct 361 ms 93780 KB n=200000
124 Correct 110 ms 36816 KB n=25264