Submission #879286

# Submission time Handle Problem Language Result Execution time Memory
879286 2023-11-27T05:33:47 Z The_Samurai Birthday gift (IZhO18_treearray) C++17
100 / 100
1187 ms 84848 KB
// I stand with PALESTINE




//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include "bits/stdc++.h"

using namespace std;
using ll = long long;
const int lg = 18;

vector<vector<int>> g, jump;
vector<int> tin, tout;
int z;

void dfs(int u) {
    if (u != 1) {
        for (int i = 1; i < lg; i++) {
            jump[i][u] = jump[i - 1][jump[i - 1][u]];
            if (!jump[i][u]) break;
        }
    }
    tin[u] = z++;
    for (int v: g[u]) {
        if (jump[0][u] == v) continue;
        jump[0][v] = u;
        dfs(v);
    }
    tout[u] = z++;
}

bool isPar(int u, int v) {
    return tin[u] <= tin[v] and tout[v] <= tout[u];
}

int lca(int u, int v) {
    if (v == 0 or isPar(u, v)) return u;
    if (u == 0 or isPar(v, u)) return v;
    for (int i = lg - 1; i >= 0; i--) {
        if (!jump[i][u] or isPar(jump[i][u], v)) continue;
        u = jump[i][u];
    }
    return jump[0][u];
}

struct SegTree {
    vector<set<pair<int, int>>> tree;
    int size;

    void init(int n) {
        size = 1;
        while (size < n) size *= 2;
        tree.assign(2 * size - 1, set<pair<int, int>>());
        build(0, 0, size);
    }

    void build(int x, int lx, int rx) {
        for (int i = 0; i < rx - lx; i++) tree[x].emplace(0, i + lx);
        if (rx - lx == 1) return;
        int m = (lx + rx) >> 1;
        build(2 * x + 1, lx, m);
        build(2 * x + 2, m, rx);
    }

    int upd(int i, int v, int x, int lx, int rx) {
        if (rx - lx == 1) {
            int vv = tree[x].begin()->first;
            tree[x] = set<pair<int, int>>{make_pair(v, i)};
            return vv;
        }
        int vv, m = (lx + rx) >> 1;
        if (i < m) vv = upd(i, v, 2 * x + 1, lx, m);
        else vv = upd(i, v, 2 * x + 2, m, rx);
        tree[x].erase(tree[x].find(make_pair(vv, i)));
        tree[x].emplace(v, i);
        return vv;
    }

    void upd(int i, int v) {
        upd(i, v, 0, 0, size);
    }

    int get(int l, int r, int v, int x, int lx, int rx) {
        if (l >= rx or lx >= r) return -1;
        if (l <= lx and rx <= r) {
            auto it = tree[x].lower_bound(make_pair(v, 0));
            if (it != tree[x].end() and it->first == v) return it->second;
            return -1;
        }
        int m = (lx + rx) >> 1;
        int ans = get(l, r, v, 2 * x + 1, lx, m);
        if (ans != -1) return ans;
        ans = get(l, r, v, 2 * x + 2, m, rx);
        return ans;
    }

    int get(int l, int r, int v) {
        return get(l, r + 1, v, 0, 0, size);
    }
};

void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    g.assign(n + 1, {});
    jump.assign(lg, vector<int>(n + 1));
    tin.assign(n + 1, 0); tout.assign(n + 1, 0);
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    vector<int> a(m + 1);
    for (int i = 1; i <= m; i++) cin >> a[i];
    dfs(1);
    vector<set<int>> v1(n + 1), v2(n + 1);
    for (int i = 1; i <= m; i++) v1[a[i]].insert(i);
    for (int i = 1; i < m; i++) v2[lca(a[i], a[i + 1])].insert(i);
    while (q--) {
        int op;
        cin >> op;
        if (op == 1) {
            int i;
            cin >> i;
            v1[a[i]].erase(i);
            if (i > 1) v2[lca(a[i - 1], a[i])].erase(i - 1);
            if (i < m) v2[lca(a[i], a[i + 1])].erase(i);
            cin >> a[i];
            v1[a[i]].insert(i);
            if (i > 1) v2[lca(a[i - 1], a[i])].insert(i - 1);
            if (i < m) v2[lca(a[i], a[i + 1])].insert(i);
        } else {
            int l, r, root;
            cin >> l >> r >> root;
            auto it1 = v1[root].lower_bound(l);
            if (it1 != v1[root].end() and *it1 <= r) {
                cout << *it1 << ' ' << *it1 << '\n';
                continue;
            }
            auto it2 = v2[root].lower_bound(l);
            if (it2 != v2[root].end() and *it2 < r) cout << *it2 << ' ' << *it2 + 1 << '\n';
            else cout << "-1 -1\n";
        }
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int q = 1;
//    cin >> q;
    while (q--) {
        solve();
        cout << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 344 KB n=100
3 Correct 0 ms 348 KB n=100
4 Correct 0 ms 436 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 0 ms 348 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 424 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 0 ms 348 KB n=100
19 Correct 0 ms 344 KB n=100
20 Correct 1 ms 344 KB n=100
21 Correct 0 ms 344 KB n=100
22 Correct 1 ms 348 KB n=100
23 Correct 1 ms 608 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 600 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 0 ms 348 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 344 KB n=100
3 Correct 0 ms 348 KB n=100
4 Correct 0 ms 436 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 0 ms 348 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 424 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 0 ms 348 KB n=100
19 Correct 0 ms 344 KB n=100
20 Correct 1 ms 344 KB n=100
21 Correct 0 ms 344 KB n=100
22 Correct 1 ms 348 KB n=100
23 Correct 1 ms 608 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 600 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 0 ms 348 KB n=100
28 Correct 1 ms 604 KB n=500
29 Correct 1 ms 604 KB n=500
30 Correct 1 ms 600 KB n=500
31 Correct 1 ms 600 KB n=500
32 Correct 1 ms 604 KB n=500
33 Correct 1 ms 604 KB n=500
34 Correct 1 ms 604 KB n=500
35 Correct 1 ms 600 KB n=500
36 Correct 1 ms 604 KB n=500
37 Correct 1 ms 604 KB n=500
38 Correct 1 ms 604 KB n=500
39 Correct 1 ms 604 KB n=500
40 Correct 1 ms 604 KB n=500
41 Correct 1 ms 604 KB n=500
42 Correct 1 ms 604 KB n=500
43 Correct 1 ms 604 KB n=500
44 Correct 1 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 1 ms 604 KB n=500
47 Correct 1 ms 604 KB n=500
48 Correct 1 ms 604 KB n=500
49 Correct 1 ms 600 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 604 KB n=500
52 Correct 1 ms 600 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 1 ms 604 KB n=500
57 Correct 1 ms 604 KB n=500
58 Correct 1 ms 604 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 344 KB n=100
3 Correct 0 ms 348 KB n=100
4 Correct 0 ms 436 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 0 ms 348 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 424 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 0 ms 348 KB n=100
19 Correct 0 ms 344 KB n=100
20 Correct 1 ms 344 KB n=100
21 Correct 0 ms 344 KB n=100
22 Correct 1 ms 348 KB n=100
23 Correct 1 ms 608 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 600 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 0 ms 348 KB n=100
28 Correct 1 ms 604 KB n=500
29 Correct 1 ms 604 KB n=500
30 Correct 1 ms 600 KB n=500
31 Correct 1 ms 600 KB n=500
32 Correct 1 ms 604 KB n=500
33 Correct 1 ms 604 KB n=500
34 Correct 1 ms 604 KB n=500
35 Correct 1 ms 600 KB n=500
36 Correct 1 ms 604 KB n=500
37 Correct 1 ms 604 KB n=500
38 Correct 1 ms 604 KB n=500
39 Correct 1 ms 604 KB n=500
40 Correct 1 ms 604 KB n=500
41 Correct 1 ms 604 KB n=500
42 Correct 1 ms 604 KB n=500
43 Correct 1 ms 604 KB n=500
44 Correct 1 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 1 ms 604 KB n=500
47 Correct 1 ms 604 KB n=500
48 Correct 1 ms 604 KB n=500
49 Correct 1 ms 600 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 604 KB n=500
52 Correct 1 ms 600 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 1 ms 604 KB n=500
57 Correct 1 ms 604 KB n=500
58 Correct 1 ms 604 KB n=500
59 Correct 2 ms 1116 KB n=2000
60 Correct 2 ms 1116 KB n=2000
61 Correct 2 ms 1116 KB n=2000
62 Correct 3 ms 1116 KB n=2000
63 Correct 3 ms 1116 KB n=2000
64 Correct 3 ms 1116 KB n=2000
65 Correct 3 ms 1116 KB n=2000
66 Correct 2 ms 1368 KB n=2000
67 Correct 3 ms 1116 KB n=2000
68 Correct 2 ms 1116 KB n=2000
69 Correct 2 ms 992 KB n=2000
70 Correct 2 ms 1116 KB n=2000
71 Correct 2 ms 1116 KB n=2000
72 Correct 3 ms 1116 KB n=2000
73 Correct 2 ms 1112 KB n=2000
74 Correct 2 ms 856 KB n=1844
75 Correct 2 ms 1116 KB n=2000
76 Correct 3 ms 1116 KB n=2000
77 Correct 2 ms 1116 KB n=2000
78 Correct 2 ms 1116 KB n=2000
79 Correct 2 ms 1116 KB n=2000
80 Correct 2 ms 1368 KB n=2000
81 Correct 3 ms 1112 KB n=2000
82 Correct 2 ms 1116 KB n=2000
83 Correct 2 ms 1116 KB n=2000
84 Correct 2 ms 1116 KB n=2000
85 Correct 2 ms 1116 KB n=2000
86 Correct 3 ms 1116 KB n=2000
87 Correct 2 ms 1116 KB n=2000
88 Correct 2 ms 1116 KB n=2000
89 Correct 2 ms 1240 KB n=2000
90 Correct 2 ms 1112 KB n=2000
91 Correct 3 ms 1116 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 344 KB n=100
3 Correct 0 ms 348 KB n=100
4 Correct 0 ms 436 KB n=100
5 Correct 0 ms 348 KB n=100
6 Correct 0 ms 348 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 344 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 344 KB n=100
14 Correct 0 ms 424 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 0 ms 348 KB n=100
19 Correct 0 ms 344 KB n=100
20 Correct 1 ms 344 KB n=100
21 Correct 0 ms 344 KB n=100
22 Correct 1 ms 348 KB n=100
23 Correct 1 ms 608 KB n=100
24 Correct 1 ms 348 KB n=100
25 Correct 0 ms 600 KB n=100
26 Correct 0 ms 348 KB n=12
27 Correct 0 ms 348 KB n=100
28 Correct 1 ms 604 KB n=500
29 Correct 1 ms 604 KB n=500
30 Correct 1 ms 600 KB n=500
31 Correct 1 ms 600 KB n=500
32 Correct 1 ms 604 KB n=500
33 Correct 1 ms 604 KB n=500
34 Correct 1 ms 604 KB n=500
35 Correct 1 ms 600 KB n=500
36 Correct 1 ms 604 KB n=500
37 Correct 1 ms 604 KB n=500
38 Correct 1 ms 604 KB n=500
39 Correct 1 ms 604 KB n=500
40 Correct 1 ms 604 KB n=500
41 Correct 1 ms 604 KB n=500
42 Correct 1 ms 604 KB n=500
43 Correct 1 ms 604 KB n=500
44 Correct 1 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 1 ms 604 KB n=500
47 Correct 1 ms 604 KB n=500
48 Correct 1 ms 604 KB n=500
49 Correct 1 ms 600 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 604 KB n=500
52 Correct 1 ms 600 KB n=500
53 Correct 1 ms 604 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 1 ms 604 KB n=500
57 Correct 1 ms 604 KB n=500
58 Correct 1 ms 604 KB n=500
59 Correct 2 ms 1116 KB n=2000
60 Correct 2 ms 1116 KB n=2000
61 Correct 2 ms 1116 KB n=2000
62 Correct 3 ms 1116 KB n=2000
63 Correct 3 ms 1116 KB n=2000
64 Correct 3 ms 1116 KB n=2000
65 Correct 3 ms 1116 KB n=2000
66 Correct 2 ms 1368 KB n=2000
67 Correct 3 ms 1116 KB n=2000
68 Correct 2 ms 1116 KB n=2000
69 Correct 2 ms 992 KB n=2000
70 Correct 2 ms 1116 KB n=2000
71 Correct 2 ms 1116 KB n=2000
72 Correct 3 ms 1116 KB n=2000
73 Correct 2 ms 1112 KB n=2000
74 Correct 2 ms 856 KB n=1844
75 Correct 2 ms 1116 KB n=2000
76 Correct 3 ms 1116 KB n=2000
77 Correct 2 ms 1116 KB n=2000
78 Correct 2 ms 1116 KB n=2000
79 Correct 2 ms 1116 KB n=2000
80 Correct 2 ms 1368 KB n=2000
81 Correct 3 ms 1112 KB n=2000
82 Correct 2 ms 1116 KB n=2000
83 Correct 2 ms 1116 KB n=2000
84 Correct 2 ms 1116 KB n=2000
85 Correct 2 ms 1116 KB n=2000
86 Correct 3 ms 1116 KB n=2000
87 Correct 2 ms 1116 KB n=2000
88 Correct 2 ms 1116 KB n=2000
89 Correct 2 ms 1240 KB n=2000
90 Correct 2 ms 1112 KB n=2000
91 Correct 3 ms 1116 KB n=2000
92 Correct 614 ms 67332 KB n=200000
93 Correct 866 ms 74840 KB n=200000
94 Correct 561 ms 83504 KB n=200000
95 Correct 566 ms 72688 KB n=200000
96 Correct 520 ms 72748 KB n=200000
97 Correct 914 ms 77888 KB n=200000
98 Correct 548 ms 72768 KB n=200000
99 Correct 664 ms 72764 KB n=200000
100 Correct 603 ms 72956 KB n=200000
101 Correct 439 ms 84848 KB n=200000
102 Correct 268 ms 73540 KB n=200000
103 Correct 266 ms 73452 KB n=200000
104 Correct 274 ms 73708 KB n=200000
105 Correct 281 ms 73776 KB n=200000
106 Correct 271 ms 73796 KB n=200000
107 Correct 277 ms 73716 KB n=200000
108 Correct 652 ms 72144 KB n=200000
109 Correct 604 ms 72188 KB n=200000
110 Correct 583 ms 72260 KB n=200000
111 Correct 513 ms 72000 KB n=200000
112 Correct 509 ms 83548 KB n=200000
113 Correct 932 ms 77580 KB n=200000
114 Correct 532 ms 72224 KB n=200000
115 Correct 1187 ms 75032 KB n=200000
116 Correct 586 ms 72380 KB n=200000
117 Correct 466 ms 84420 KB n=200000
118 Correct 1004 ms 76156 KB n=200000
119 Correct 525 ms 72384 KB n=200000
120 Correct 388 ms 84032 KB n=200000
121 Correct 394 ms 83980 KB n=200000
122 Correct 374 ms 84252 KB n=200000
123 Correct 280 ms 73792 KB n=200000
124 Correct 94 ms 18308 KB n=25264