Submission #1114034

# Submission time Handle Problem Language Result Execution time Memory
1114034 2024-11-18T06:40:00 Z vjudge1 Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 45256 KB
#include <bits/stdc++.h>

using i64 = long long;

#ifdef DEBUG 
    #include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
    #define debug(...) void(23)
#endif

constexpr int max_N = int(2E5) + 5;
constexpr int LG = 20;

int N, M, Q;
std::vector<int> adj[max_N];
int par[LG][max_N], tin[max_N], tout[max_N], dep[max_N], timer;

void dfs(int v) {
    tin[v] = timer++;
    for (auto u : adj[v]) {
        adj[u].erase(std::find(adj[u].begin(), adj[u].end(), v));
        par[0][u] = v;
        dep[u] = dep[v] + 1;
        dfs(u);
    }
    tout[v] = timer;
}

bool is_in_subtree_of(int u, int v) {
    return tin[v] <= tin[u] && tout[u] <= tout[v];
}
int lca(int a, int b) {
    if (is_in_subtree_of(a, b)) {
        return b;
    } else if (is_in_subtree_of(b, a)) {
        return a;
    }
    for (int lg = LG - 1; lg >= 0; --lg) {
        if (!is_in_subtree_of(b, par[lg][a])) {
            a = par[lg][a];
        }
    }
    return par[0][a];
}

int A[max_N];

#define def int mid = (l + r) >> 1, lv = v << 1, rv = v << 1 | 1;
struct node {
    int v;
} tree[max_N << 2];
node unite(const node& lhs, const node& rhs) {
    return {lca(lhs.v, rhs.v)};
}
void build(int v, int l, int r) {
    if (l == r) {
        tree[v] = {A[l]};
        return;
    }
    def;
    build(lv, l, mid);
    build(rv, mid + 1, r);
    tree[v] = unite(tree[lv], tree[rv]);
}
void build() {
    build(1, 0, M - 1);
}
void modify(int v, int l, int r, int p, int x) {
    if (l == r) {
        tree[v] = {x};
        return;
    }
    def;
    if (p <= mid) {
        modify(lv, l, mid, p, x);
    } else {
        modify(rv, mid + 1, r, p, x);
    }
    tree[v] = unite(tree[lv], tree[rv]);
}
void modify(int p, int x) {
    modify(1, 0, M - 1, p, x);
}
node query(int v, int l, int r, int ql, int qr) {
    if (l == ql && qr == r) {
        return tree[v];
    }
    def;
    if (qr <= mid) {
        return query(lv, l, mid, ql, qr);
    } else if (mid + 1 <= ql) {
        return query(rv, mid + 1, r, ql, qr);
    } else {
        return unite(query(lv, l, mid, ql, mid), query(rv, mid + 1, r, mid + 1, qr));
    }
}
node query(int l, int r) {
    return query(1, 0, M - 1, l, r);
}
std::pair<int, int> find_first(int v, int l, int r, int p, int now, int x) {
    debug("finding first", v, l, r, p, now, x);
    if (r < p) {
        return {-1, -1};
    } else if (l == r) {
        int tv = lca(now, tree[v].v);
        return {tv, l};
    } else if (p <= l) {
        def;
        int tv = tree[lv].v;
        int lc = lca(tv, now);
        if (dep[lc] > dep[x]) {
            return find_first(rv, mid + 1, r, p, lc, x);
        } else {
            return find_first(lv, l, mid, p, now, x);
        }
    }
    def;
    auto gl = find_first(lv, l, mid, p, now, x);
    if (gl.first != -1) {
        if (dep[gl.first] <= dep[x]) {
            return gl;
        }
        now = lca(now, gl.first);
    }
    auto gr = find_first(rv, mid + 1, r, p, now, x);
    return gr;
}
std::pair<int, int> find_first(int p, int x) {
    return find_first(1, 0, M - 1, p, A[p], x);
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> N >> M >> Q;
    for (int i = 1; i < N; ++i) {
        int X, Y;
        std::cin >> X >> Y;
        adj[X].emplace_back(Y);
        adj[Y].emplace_back(X);
    }
    for (int i = 0; i < M; ++i) {
        std::cin >> A[i];
    }

    par[0][1] = 1;
    dfs(1);

    #ifdef DEBUG
        for (int i = 1; i <= N; ++i) {
            std::cerr << par[0][i] << " \n"[i == N];
        }
        for (int i = 1; i <= N; ++i) {
            std::cerr << dep[i] << " \n"[i == N];
        }
        for (int i = 1; i <= N; ++i) {
            std::cerr << tin[i] << " \n"[i == N];
        }
        for (int i = 1; i <= N; ++i) {
            std::cerr << tout[i] << " \n"[i == N];
        }
    #endif

    for (int lg = 0; lg + 1 < LG; ++lg) {
        for (int i = 1; i <= N; ++i) {
            par[lg + 1][i] = par[lg][par[lg][i]];
        }
    }

    build();

    while (Q--) {
        int TP;
        std::cin >> TP;
        if (TP == 1) {
            int P, X;
            std::cin >> P >> X;
            --P;
            A[P] = X;
            modify(P, X);
        } else {
            int L, R, X; 
            std::cin >> L >> R >> X;
            --L, --R;
            bool found = false;
            for (int l = L; l <= R; ++l) {
                auto[x, r] = find_first(l, X);
                debug(l, x, r);
                if (x == X && r <= R) {
                    std::cout << l + 1 << ' ' << r + 1 << '\n';
                    found = true;
                    break;
                }
            }
            if (!found) {
                std::cout << "-1 -1\n";
            }
        }
        debug();
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24656 KB n=5
2 Correct 4 ms 24656 KB n=100
3 Correct 5 ms 24656 KB n=100
4 Correct 5 ms 24656 KB n=100
5 Correct 5 ms 24656 KB n=100
6 Correct 5 ms 24656 KB n=100
7 Correct 6 ms 24656 KB n=100
8 Correct 6 ms 24656 KB n=100
9 Correct 5 ms 24824 KB n=100
10 Correct 5 ms 24820 KB n=100
11 Correct 4 ms 24812 KB n=100
12 Correct 5 ms 24656 KB n=100
13 Correct 5 ms 24656 KB n=100
14 Correct 5 ms 24656 KB n=100
15 Correct 6 ms 24828 KB n=100
16 Correct 5 ms 22608 KB n=100
17 Correct 5 ms 22608 KB n=100
18 Correct 5 ms 22608 KB n=100
19 Correct 4 ms 18672 KB n=100
20 Correct 4 ms 22608 KB n=100
21 Correct 5 ms 24656 KB n=100
22 Correct 5 ms 24656 KB n=100
23 Correct 5 ms 24656 KB n=100
24 Correct 5 ms 24824 KB n=100
25 Correct 5 ms 24656 KB n=100
26 Correct 5 ms 24844 KB n=12
27 Correct 5 ms 22608 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24656 KB n=5
2 Correct 4 ms 24656 KB n=100
3 Correct 5 ms 24656 KB n=100
4 Correct 5 ms 24656 KB n=100
5 Correct 5 ms 24656 KB n=100
6 Correct 5 ms 24656 KB n=100
7 Correct 6 ms 24656 KB n=100
8 Correct 6 ms 24656 KB n=100
9 Correct 5 ms 24824 KB n=100
10 Correct 5 ms 24820 KB n=100
11 Correct 4 ms 24812 KB n=100
12 Correct 5 ms 24656 KB n=100
13 Correct 5 ms 24656 KB n=100
14 Correct 5 ms 24656 KB n=100
15 Correct 6 ms 24828 KB n=100
16 Correct 5 ms 22608 KB n=100
17 Correct 5 ms 22608 KB n=100
18 Correct 5 ms 22608 KB n=100
19 Correct 4 ms 18672 KB n=100
20 Correct 4 ms 22608 KB n=100
21 Correct 5 ms 24656 KB n=100
22 Correct 5 ms 24656 KB n=100
23 Correct 5 ms 24656 KB n=100
24 Correct 5 ms 24824 KB n=100
25 Correct 5 ms 24656 KB n=100
26 Correct 5 ms 24844 KB n=12
27 Correct 5 ms 22608 KB n=100
28 Correct 5 ms 24656 KB n=500
29 Correct 7 ms 24912 KB n=500
30 Correct 7 ms 24656 KB n=500
31 Correct 7 ms 24912 KB n=500
32 Correct 6 ms 22776 KB n=500
33 Correct 7 ms 24656 KB n=500
34 Correct 5 ms 22608 KB n=500
35 Correct 6 ms 24912 KB n=500
36 Correct 26 ms 24824 KB n=500
37 Correct 24 ms 24656 KB n=500
38 Correct 25 ms 24872 KB n=500
39 Correct 15 ms 22864 KB n=500
40 Correct 15 ms 24848 KB n=500
41 Correct 15 ms 24656 KB n=500
42 Correct 14 ms 24656 KB n=500
43 Correct 14 ms 24656 KB n=500
44 Correct 15 ms 24884 KB n=500
45 Correct 4 ms 18768 KB n=500
46 Correct 5 ms 12792 KB n=500
47 Correct 5 ms 18768 KB n=500
48 Correct 4 ms 18768 KB n=500
49 Correct 4 ms 12624 KB n=500
50 Correct 4 ms 14672 KB n=500
51 Correct 7 ms 24656 KB n=500
52 Correct 6 ms 24912 KB n=500
53 Correct 8 ms 24840 KB n=500
54 Correct 8 ms 22864 KB n=500
55 Correct 4 ms 24824 KB n=278
56 Correct 15 ms 22864 KB n=500
57 Correct 22 ms 24912 KB n=500
58 Correct 12 ms 24656 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24656 KB n=5
2 Correct 4 ms 24656 KB n=100
3 Correct 5 ms 24656 KB n=100
4 Correct 5 ms 24656 KB n=100
5 Correct 5 ms 24656 KB n=100
6 Correct 5 ms 24656 KB n=100
7 Correct 6 ms 24656 KB n=100
8 Correct 6 ms 24656 KB n=100
9 Correct 5 ms 24824 KB n=100
10 Correct 5 ms 24820 KB n=100
11 Correct 4 ms 24812 KB n=100
12 Correct 5 ms 24656 KB n=100
13 Correct 5 ms 24656 KB n=100
14 Correct 5 ms 24656 KB n=100
15 Correct 6 ms 24828 KB n=100
16 Correct 5 ms 22608 KB n=100
17 Correct 5 ms 22608 KB n=100
18 Correct 5 ms 22608 KB n=100
19 Correct 4 ms 18672 KB n=100
20 Correct 4 ms 22608 KB n=100
21 Correct 5 ms 24656 KB n=100
22 Correct 5 ms 24656 KB n=100
23 Correct 5 ms 24656 KB n=100
24 Correct 5 ms 24824 KB n=100
25 Correct 5 ms 24656 KB n=100
26 Correct 5 ms 24844 KB n=12
27 Correct 5 ms 22608 KB n=100
28 Correct 5 ms 24656 KB n=500
29 Correct 7 ms 24912 KB n=500
30 Correct 7 ms 24656 KB n=500
31 Correct 7 ms 24912 KB n=500
32 Correct 6 ms 22776 KB n=500
33 Correct 7 ms 24656 KB n=500
34 Correct 5 ms 22608 KB n=500
35 Correct 6 ms 24912 KB n=500
36 Correct 26 ms 24824 KB n=500
37 Correct 24 ms 24656 KB n=500
38 Correct 25 ms 24872 KB n=500
39 Correct 15 ms 22864 KB n=500
40 Correct 15 ms 24848 KB n=500
41 Correct 15 ms 24656 KB n=500
42 Correct 14 ms 24656 KB n=500
43 Correct 14 ms 24656 KB n=500
44 Correct 15 ms 24884 KB n=500
45 Correct 4 ms 18768 KB n=500
46 Correct 5 ms 12792 KB n=500
47 Correct 5 ms 18768 KB n=500
48 Correct 4 ms 18768 KB n=500
49 Correct 4 ms 12624 KB n=500
50 Correct 4 ms 14672 KB n=500
51 Correct 7 ms 24656 KB n=500
52 Correct 6 ms 24912 KB n=500
53 Correct 8 ms 24840 KB n=500
54 Correct 8 ms 22864 KB n=500
55 Correct 4 ms 24824 KB n=278
56 Correct 15 ms 22864 KB n=500
57 Correct 22 ms 24912 KB n=500
58 Correct 12 ms 24656 KB n=500
59 Correct 6 ms 25080 KB n=2000
60 Correct 33 ms 25084 KB n=2000
61 Correct 38 ms 19024 KB n=2000
62 Correct 35 ms 22864 KB n=2000
63 Correct 6 ms 22864 KB n=2000
64 Correct 41 ms 25048 KB n=2000
65 Correct 6 ms 24912 KB n=2000
66 Correct 39 ms 19040 KB n=2000
67 Correct 9 ms 22864 KB n=2000
68 Correct 40 ms 24912 KB n=2000
69 Correct 381 ms 24984 KB n=2000
70 Correct 382 ms 25160 KB n=2000
71 Correct 377 ms 24912 KB n=2000
72 Correct 212 ms 24980 KB n=2000
73 Correct 201 ms 24912 KB n=2000
74 Correct 6 ms 24912 KB n=1844
75 Correct 199 ms 24912 KB n=2000
76 Correct 214 ms 24984 KB n=2000
77 Correct 219 ms 22864 KB n=2000
78 Correct 222 ms 24912 KB n=2000
79 Correct 8 ms 22864 KB n=2000
80 Correct 31 ms 19036 KB n=2000
81 Correct 37 ms 18988 KB n=2000
82 Correct 6 ms 18768 KB n=2000
83 Correct 33 ms 19024 KB n=2000
84 Correct 51 ms 12888 KB n=2000
85 Correct 70 ms 18980 KB n=2000
86 Correct 74 ms 25016 KB n=2000
87 Correct 58 ms 24912 KB n=2000
88 Correct 327 ms 25084 KB n=2000
89 Correct 332 ms 21092 KB n=2000
90 Correct 79 ms 24912 KB n=2000
91 Correct 226 ms 24912 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24656 KB n=5
2 Correct 4 ms 24656 KB n=100
3 Correct 5 ms 24656 KB n=100
4 Correct 5 ms 24656 KB n=100
5 Correct 5 ms 24656 KB n=100
6 Correct 5 ms 24656 KB n=100
7 Correct 6 ms 24656 KB n=100
8 Correct 6 ms 24656 KB n=100
9 Correct 5 ms 24824 KB n=100
10 Correct 5 ms 24820 KB n=100
11 Correct 4 ms 24812 KB n=100
12 Correct 5 ms 24656 KB n=100
13 Correct 5 ms 24656 KB n=100
14 Correct 5 ms 24656 KB n=100
15 Correct 6 ms 24828 KB n=100
16 Correct 5 ms 22608 KB n=100
17 Correct 5 ms 22608 KB n=100
18 Correct 5 ms 22608 KB n=100
19 Correct 4 ms 18672 KB n=100
20 Correct 4 ms 22608 KB n=100
21 Correct 5 ms 24656 KB n=100
22 Correct 5 ms 24656 KB n=100
23 Correct 5 ms 24656 KB n=100
24 Correct 5 ms 24824 KB n=100
25 Correct 5 ms 24656 KB n=100
26 Correct 5 ms 24844 KB n=12
27 Correct 5 ms 22608 KB n=100
28 Correct 5 ms 24656 KB n=500
29 Correct 7 ms 24912 KB n=500
30 Correct 7 ms 24656 KB n=500
31 Correct 7 ms 24912 KB n=500
32 Correct 6 ms 22776 KB n=500
33 Correct 7 ms 24656 KB n=500
34 Correct 5 ms 22608 KB n=500
35 Correct 6 ms 24912 KB n=500
36 Correct 26 ms 24824 KB n=500
37 Correct 24 ms 24656 KB n=500
38 Correct 25 ms 24872 KB n=500
39 Correct 15 ms 22864 KB n=500
40 Correct 15 ms 24848 KB n=500
41 Correct 15 ms 24656 KB n=500
42 Correct 14 ms 24656 KB n=500
43 Correct 14 ms 24656 KB n=500
44 Correct 15 ms 24884 KB n=500
45 Correct 4 ms 18768 KB n=500
46 Correct 5 ms 12792 KB n=500
47 Correct 5 ms 18768 KB n=500
48 Correct 4 ms 18768 KB n=500
49 Correct 4 ms 12624 KB n=500
50 Correct 4 ms 14672 KB n=500
51 Correct 7 ms 24656 KB n=500
52 Correct 6 ms 24912 KB n=500
53 Correct 8 ms 24840 KB n=500
54 Correct 8 ms 22864 KB n=500
55 Correct 4 ms 24824 KB n=278
56 Correct 15 ms 22864 KB n=500
57 Correct 22 ms 24912 KB n=500
58 Correct 12 ms 24656 KB n=500
59 Correct 6 ms 25080 KB n=2000
60 Correct 33 ms 25084 KB n=2000
61 Correct 38 ms 19024 KB n=2000
62 Correct 35 ms 22864 KB n=2000
63 Correct 6 ms 22864 KB n=2000
64 Correct 41 ms 25048 KB n=2000
65 Correct 6 ms 24912 KB n=2000
66 Correct 39 ms 19040 KB n=2000
67 Correct 9 ms 22864 KB n=2000
68 Correct 40 ms 24912 KB n=2000
69 Correct 381 ms 24984 KB n=2000
70 Correct 382 ms 25160 KB n=2000
71 Correct 377 ms 24912 KB n=2000
72 Correct 212 ms 24980 KB n=2000
73 Correct 201 ms 24912 KB n=2000
74 Correct 6 ms 24912 KB n=1844
75 Correct 199 ms 24912 KB n=2000
76 Correct 214 ms 24984 KB n=2000
77 Correct 219 ms 22864 KB n=2000
78 Correct 222 ms 24912 KB n=2000
79 Correct 8 ms 22864 KB n=2000
80 Correct 31 ms 19036 KB n=2000
81 Correct 37 ms 18988 KB n=2000
82 Correct 6 ms 18768 KB n=2000
83 Correct 33 ms 19024 KB n=2000
84 Correct 51 ms 12888 KB n=2000
85 Correct 70 ms 18980 KB n=2000
86 Correct 74 ms 25016 KB n=2000
87 Correct 58 ms 24912 KB n=2000
88 Correct 327 ms 25084 KB n=2000
89 Correct 332 ms 21092 KB n=2000
90 Correct 79 ms 24912 KB n=2000
91 Correct 226 ms 24912 KB n=2000
92 Correct 1046 ms 41616 KB n=200000
93 Execution timed out 4059 ms 45256 KB Time limit exceeded
94 Halted 0 ms 0 KB -