답안 #379385

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379385 2021-03-18T05:38:25 Z abc864197532 Birthday gift (IZhO18_treearray) C++17
100 / 100
1331 ms 74604 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define pii pair<int, int>
#define pll pair<lli, lli>
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define test(x) cout << #x << ' ' << x << endl
#define printv(x) {\
    for (auto a : x) cout << a << ' ';\
    cout << endl;\
}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int N = 200000, logN = 17;

vector <int> adj[N];
int in[N], out[N], jump[N][logN], _t;

void dfs(int v, int pa) {
    in[v] = _t++;
    jump[v][0] = pa;
    for (int u : adj[v]) if (u != pa) {
        dfs(u, v);
    }
    out[v] = _t++;
}

bool anc(int u, int v) {
    return in[u] <= in[v] && out[u] >= out[v];
}

int lca(int u, int v) {
    if (anc(u, v)) return u;
    if (anc(v, u)) return v;
    for (int i = logN - 1; ~i; --i) {
        int k = jump[u][i];
        if (~k && !anc(k, v)) u = k;
    }
    return jump[u][0];
}


int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0, u, v; i < n - 1; ++i) {
        cin >> u >> v, --u, --v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for (int i = 0; i < logN; ++i) for (int j = 0; j < n; ++j) jump[j][i] = -1;
    dfs(0, -1);
    for (int j = 1; j < logN; ++j) {
        for (int i = 0; i < n; ++i) {
            int k = jump[i][j - 1];
            if (~k) jump[i][j] = jump[k][j - 1];
        }
    }
    set <int> adjpair[n], vertex[n];
    vector <int> a(m);
    for (int i = 0; i < m; ++i) {
        cin >> a[i], --a[i];
        vertex[a[i]].insert(i);
        if (i) adjpair[lca(a[i - 1], a[i])].insert(i - 1);
    }
    while (q--) {
        int t, l, r, v;
        cin >> t >> l >> r, --l;
        if (t == 1) {
            vertex[a[l]].erase(l);
            if (l) adjpair[lca(a[l - 1], a[l])].erase(l - 1);
            if (l + 1 < m) adjpair[lca(a[l], a[l + 1])].erase(l);
            a[l] = --r;
            vertex[a[l]].insert(l);
            if (l) adjpair[lca(a[l - 1], a[l])].insert(l - 1);
            if (l + 1 < m) adjpair[lca(a[l], a[l + 1])].insert(l);
        } else {
            cin >> v, --v;
            pii ans = mp(-2, -2);
            auto it = vertex[v].lower_bound(l);
            if (it != vertex[v].end() && *it < r) {
                ans = mp(*it, *it);
            }
            auto it2 = adjpair[v].lower_bound(l);
            if (it2 != adjpair[v].end() && *it2 < r - 1) {
                ans = mp(*it2, *it2 + 1);
            }
            cout << ans.X + 1 << ' ' << ans.Y + 1 << '\n';
        }
    }
}
/*
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 4 ms 5228 KB n=5
2 Correct 5 ms 5100 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5100 KB n=100
5 Correct 4 ms 5100 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5100 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB n=5
2 Correct 5 ms 5100 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5100 KB n=100
5 Correct 4 ms 5100 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5100 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
28 Correct 4 ms 5228 KB n=500
29 Correct 4 ms 5228 KB n=500
30 Correct 5 ms 5356 KB n=500
31 Correct 6 ms 5356 KB n=500
32 Correct 4 ms 5228 KB n=500
33 Correct 4 ms 5228 KB n=500
34 Correct 4 ms 5228 KB n=500
35 Correct 4 ms 5228 KB n=500
36 Correct 5 ms 5228 KB n=500
37 Correct 6 ms 5228 KB n=500
38 Correct 5 ms 5228 KB n=500
39 Correct 4 ms 5228 KB n=500
40 Correct 4 ms 5228 KB n=500
41 Correct 4 ms 5228 KB n=500
42 Correct 4 ms 5228 KB n=500
43 Correct 4 ms 5228 KB n=500
44 Correct 4 ms 5228 KB n=500
45 Correct 5 ms 5228 KB n=500
46 Correct 4 ms 5228 KB n=500
47 Correct 5 ms 5228 KB n=500
48 Correct 6 ms 5228 KB n=500
49 Correct 4 ms 5228 KB n=500
50 Correct 4 ms 5228 KB n=500
51 Correct 5 ms 5228 KB n=500
52 Correct 4 ms 5228 KB n=500
53 Correct 4 ms 5228 KB n=500
54 Correct 5 ms 5228 KB n=500
55 Correct 4 ms 5100 KB n=278
56 Correct 4 ms 5228 KB n=500
57 Correct 4 ms 5228 KB n=500
58 Correct 5 ms 5228 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB n=5
2 Correct 5 ms 5100 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5100 KB n=100
5 Correct 4 ms 5100 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5100 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
28 Correct 4 ms 5228 KB n=500
29 Correct 4 ms 5228 KB n=500
30 Correct 5 ms 5356 KB n=500
31 Correct 6 ms 5356 KB n=500
32 Correct 4 ms 5228 KB n=500
33 Correct 4 ms 5228 KB n=500
34 Correct 4 ms 5228 KB n=500
35 Correct 4 ms 5228 KB n=500
36 Correct 5 ms 5228 KB n=500
37 Correct 6 ms 5228 KB n=500
38 Correct 5 ms 5228 KB n=500
39 Correct 4 ms 5228 KB n=500
40 Correct 4 ms 5228 KB n=500
41 Correct 4 ms 5228 KB n=500
42 Correct 4 ms 5228 KB n=500
43 Correct 4 ms 5228 KB n=500
44 Correct 4 ms 5228 KB n=500
45 Correct 5 ms 5228 KB n=500
46 Correct 4 ms 5228 KB n=500
47 Correct 5 ms 5228 KB n=500
48 Correct 6 ms 5228 KB n=500
49 Correct 4 ms 5228 KB n=500
50 Correct 4 ms 5228 KB n=500
51 Correct 5 ms 5228 KB n=500
52 Correct 4 ms 5228 KB n=500
53 Correct 4 ms 5228 KB n=500
54 Correct 5 ms 5228 KB n=500
55 Correct 4 ms 5100 KB n=278
56 Correct 4 ms 5228 KB n=500
57 Correct 4 ms 5228 KB n=500
58 Correct 5 ms 5228 KB n=500
59 Correct 7 ms 5740 KB n=2000
60 Correct 7 ms 5740 KB n=2000
61 Correct 7 ms 5740 KB n=2000
62 Correct 7 ms 5740 KB n=2000
63 Correct 7 ms 5740 KB n=2000
64 Correct 7 ms 5740 KB n=2000
65 Correct 7 ms 5740 KB n=2000
66 Correct 7 ms 5740 KB n=2000
67 Correct 7 ms 5740 KB n=2000
68 Correct 7 ms 5740 KB n=2000
69 Correct 7 ms 5888 KB n=2000
70 Correct 7 ms 5760 KB n=2000
71 Correct 7 ms 5740 KB n=2000
72 Correct 7 ms 5740 KB n=2000
73 Correct 7 ms 5740 KB n=2000
74 Correct 7 ms 5740 KB n=1844
75 Correct 7 ms 5740 KB n=2000
76 Correct 8 ms 5868 KB n=2000
77 Correct 8 ms 5740 KB n=2000
78 Correct 7 ms 5740 KB n=2000
79 Correct 8 ms 5740 KB n=2000
80 Correct 7 ms 5740 KB n=2000
81 Correct 7 ms 5740 KB n=2000
82 Correct 7 ms 5868 KB n=2000
83 Correct 7 ms 5740 KB n=2000
84 Correct 8 ms 5740 KB n=2000
85 Correct 7 ms 5740 KB n=2000
86 Correct 7 ms 5740 KB n=2000
87 Correct 7 ms 5740 KB n=2000
88 Correct 6 ms 5740 KB n=2000
89 Correct 7 ms 5740 KB n=2000
90 Correct 8 ms 5740 KB n=2000
91 Correct 8 ms 5740 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB n=5
2 Correct 5 ms 5100 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5100 KB n=100
5 Correct 4 ms 5100 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5100 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
28 Correct 4 ms 5228 KB n=500
29 Correct 4 ms 5228 KB n=500
30 Correct 5 ms 5356 KB n=500
31 Correct 6 ms 5356 KB n=500
32 Correct 4 ms 5228 KB n=500
33 Correct 4 ms 5228 KB n=500
34 Correct 4 ms 5228 KB n=500
35 Correct 4 ms 5228 KB n=500
36 Correct 5 ms 5228 KB n=500
37 Correct 6 ms 5228 KB n=500
38 Correct 5 ms 5228 KB n=500
39 Correct 4 ms 5228 KB n=500
40 Correct 4 ms 5228 KB n=500
41 Correct 4 ms 5228 KB n=500
42 Correct 4 ms 5228 KB n=500
43 Correct 4 ms 5228 KB n=500
44 Correct 4 ms 5228 KB n=500
45 Correct 5 ms 5228 KB n=500
46 Correct 4 ms 5228 KB n=500
47 Correct 5 ms 5228 KB n=500
48 Correct 6 ms 5228 KB n=500
49 Correct 4 ms 5228 KB n=500
50 Correct 4 ms 5228 KB n=500
51 Correct 5 ms 5228 KB n=500
52 Correct 4 ms 5228 KB n=500
53 Correct 4 ms 5228 KB n=500
54 Correct 5 ms 5228 KB n=500
55 Correct 4 ms 5100 KB n=278
56 Correct 4 ms 5228 KB n=500
57 Correct 4 ms 5228 KB n=500
58 Correct 5 ms 5228 KB n=500
59 Correct 7 ms 5740 KB n=2000
60 Correct 7 ms 5740 KB n=2000
61 Correct 7 ms 5740 KB n=2000
62 Correct 7 ms 5740 KB n=2000
63 Correct 7 ms 5740 KB n=2000
64 Correct 7 ms 5740 KB n=2000
65 Correct 7 ms 5740 KB n=2000
66 Correct 7 ms 5740 KB n=2000
67 Correct 7 ms 5740 KB n=2000
68 Correct 7 ms 5740 KB n=2000
69 Correct 7 ms 5888 KB n=2000
70 Correct 7 ms 5760 KB n=2000
71 Correct 7 ms 5740 KB n=2000
72 Correct 7 ms 5740 KB n=2000
73 Correct 7 ms 5740 KB n=2000
74 Correct 7 ms 5740 KB n=1844
75 Correct 7 ms 5740 KB n=2000
76 Correct 8 ms 5868 KB n=2000
77 Correct 8 ms 5740 KB n=2000
78 Correct 7 ms 5740 KB n=2000
79 Correct 8 ms 5740 KB n=2000
80 Correct 7 ms 5740 KB n=2000
81 Correct 7 ms 5740 KB n=2000
82 Correct 7 ms 5868 KB n=2000
83 Correct 7 ms 5740 KB n=2000
84 Correct 8 ms 5740 KB n=2000
85 Correct 7 ms 5740 KB n=2000
86 Correct 7 ms 5740 KB n=2000
87 Correct 7 ms 5740 KB n=2000
88 Correct 6 ms 5740 KB n=2000
89 Correct 7 ms 5740 KB n=2000
90 Correct 8 ms 5740 KB n=2000
91 Correct 8 ms 5740 KB n=2000
92 Correct 859 ms 72940 KB n=200000
93 Correct 955 ms 73324 KB n=200000
94 Correct 752 ms 73324 KB n=200000
95 Correct 842 ms 72800 KB n=200000
96 Correct 822 ms 73056 KB n=200000
97 Correct 1025 ms 73544 KB n=200000
98 Correct 846 ms 72880 KB n=200000
99 Correct 1018 ms 73240 KB n=200000
100 Correct 856 ms 73188 KB n=200000
101 Correct 672 ms 73324 KB n=200000
102 Correct 534 ms 74220 KB n=200000
103 Correct 510 ms 74092 KB n=200000
104 Correct 508 ms 74084 KB n=200000
105 Correct 528 ms 74536 KB n=200000
106 Correct 530 ms 74604 KB n=200000
107 Correct 522 ms 74476 KB n=200000
108 Correct 934 ms 73004 KB n=200000
109 Correct 1027 ms 72964 KB n=200000
110 Correct 905 ms 72960 KB n=200000
111 Correct 902 ms 72736 KB n=200000
112 Correct 720 ms 73324 KB n=200000
113 Correct 1091 ms 73452 KB n=200000
114 Correct 884 ms 72672 KB n=200000
115 Correct 1331 ms 73316 KB n=200000
116 Correct 826 ms 72928 KB n=200000
117 Correct 696 ms 73196 KB n=200000
118 Correct 1155 ms 73148 KB n=200000
119 Correct 775 ms 73184 KB n=200000
120 Correct 625 ms 72172 KB n=200000
121 Correct 623 ms 72100 KB n=200000
122 Correct 650 ms 72456 KB n=200000
123 Correct 550 ms 74220 KB n=200000
124 Correct 209 ms 21100 KB n=25264