Submission #597085

# Submission time Handle Problem Language Result Execution time Memory
597085 2022-07-15T13:23:36 Z KoD Game (APIO22_game) C++17
60 / 100
4000 ms 256340 KB
#include "game.h"
#include <bits/stdc++.h>

using std::pair;
using std::queue;
using std::set;
using std::vector;

bool contains(set<int>& set, const int x) {
    return set.find(x) != set.end();
}

struct Interval {
    int mid, par, lch, rch;
    set<int> from, to;
    explicit Interval(const int m) : mid(m), par(-1), lch(-1), rch(-1), from(), to() {}
};

int N, K;
vector<vector<int>> graph, revgraph;
vector<Interval> segtree;

int build(int l, int r) {
    const int u = (int)segtree.size();
    const int m = (l + r) / 2;
    segtree.emplace_back(m);
    if (l < m) {
        segtree[u].lch = build(l, m);
        segtree[segtree[u].lch].par = u;
    }
    if (m + 1 < r) {
        segtree[u].rch = build(m + 1, r);
        segtree[segtree[u].rch].par = u;
    }
    for (int i = l; i < m; ++i)
        segtree[u].from.insert(i);
    for (int i = m + 1; i < r; ++i)
        segtree[u].to.insert(i);
    return u;
}

bool check(int x, int u) {
    const int p = segtree[x].par;
    if (p == -1) {
        return true;
    } else {
        if (segtree[p].lch == x) {
            return contains(segtree[p].from, u);
        } else {
            return contains(segtree[p].to, u);
        }
    }
}

void init(int n, int k) {
    N = n;
    K = k;
    graph.resize(n);
    revgraph.resize(n);
    build(0, k);
}

bool insert_recursive(int x, int u);
bool insert_recursive_rev(int x, int u);
bool add_recursive(int x, int u, int v);

bool insert_recursive(int x, int u) {
    auto& data = segtree[x];
    if (!check(x, u))
        return false;
    if (data.mid == u)
        return true;
    if (data.to.insert(u).second) {
        if (contains(data.from, u))
            return true;
        for (const int v : graph[u])
            if (add_recursive(x, u, v))
                return true;
    }
    return false;
}

bool insert_recursive_rev(int x, int u) {
    auto& data = segtree[x];
    if (!check(x, u))
        return false;
    if (data.mid == u)
        return true;
    if (data.from.insert(u).second) {
        if (contains(data.to, u))
            return true;
        for (const int v : revgraph[u])
            if (add_recursive(x, v, u))
                return true;
    }
    return false;
}

bool add_recursive(int x, int u, int v) {
    auto& data = segtree[x];
    // std::cerr << data.mid << ' ' << u << ' ' << v << std::endl;
    if (contains(data.from, v) or v == data.mid) {
        if (insert_recursive_rev(x, u))
            return true;
    }
    if (contains(data.to, u) or u == data.mid) {
        if (insert_recursive(x, v))
            return true;
    }
    if (contains(data.from, u) and contains(data.from, v)) {
        if (data.lch != -1 and add_recursive(data.lch, u, v))
            return true;
    }
    if (contains(data.to, u) and contains(data.to, v)) {
        if (data.rch != -1 and add_recursive(data.rch, u, v))
            return true;
    }
    return false;
}

int add_teleporter(int u, int v) {
    graph[u].push_back(v);
    revgraph[v].push_back(u);
    static int counter = 0;
    counter++;
    const bool ret = add_recursive(0, u, v);
    // if (counter == 3) {
    //     for (const auto& x : segtree) {
    //         std::cerr << "\n[mid = " << x.mid << "]\n";
    //         std::cerr << "from:";
    //         for (const int t : x.from) std::cerr << ' ' << t;
    //         std::cerr << std::endl;
    //         std::cerr << "to:";
    //         for (const int t : x.to) std::cerr << ' ' << t;
    //         std::cerr << std::endl;
    //     }
    // }
    return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 0 ms 208 KB Output is correct
11 Correct 0 ms 208 KB Output is correct
12 Correct 1 ms 208 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 0 ms 336 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 208 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 0 ms 208 KB Output is correct
11 Correct 0 ms 208 KB Output is correct
12 Correct 1 ms 208 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 0 ms 336 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 208 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 388 KB Output is correct
22 Correct 2 ms 464 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 6 ms 976 KB Output is correct
25 Correct 11 ms 848 KB Output is correct
26 Correct 14 ms 624 KB Output is correct
27 Correct 9 ms 848 KB Output is correct
28 Correct 4 ms 848 KB Output is correct
29 Correct 6 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 0 ms 208 KB Output is correct
11 Correct 0 ms 208 KB Output is correct
12 Correct 1 ms 208 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 0 ms 336 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 208 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 388 KB Output is correct
22 Correct 2 ms 464 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 6 ms 976 KB Output is correct
25 Correct 11 ms 848 KB Output is correct
26 Correct 14 ms 624 KB Output is correct
27 Correct 9 ms 848 KB Output is correct
28 Correct 4 ms 848 KB Output is correct
29 Correct 6 ms 848 KB Output is correct
30 Correct 20 ms 3128 KB Output is correct
31 Correct 9 ms 2592 KB Output is correct
32 Correct 50 ms 10520 KB Output is correct
33 Correct 26 ms 4136 KB Output is correct
34 Correct 440 ms 26824 KB Output is correct
35 Correct 368 ms 19864 KB Output is correct
36 Correct 314 ms 11344 KB Output is correct
37 Correct 486 ms 17488 KB Output is correct
38 Correct 203 ms 8480 KB Output is correct
39 Correct 289 ms 11020 KB Output is correct
40 Correct 519 ms 24352 KB Output is correct
41 Correct 314 ms 16948 KB Output is correct
42 Correct 186 ms 12068 KB Output is correct
43 Correct 502 ms 20060 KB Output is correct
44 Correct 336 ms 18724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 0 ms 208 KB Output is correct
11 Correct 0 ms 208 KB Output is correct
12 Correct 1 ms 208 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 0 ms 336 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 208 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 388 KB Output is correct
22 Correct 2 ms 464 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 6 ms 976 KB Output is correct
25 Correct 11 ms 848 KB Output is correct
26 Correct 14 ms 624 KB Output is correct
27 Correct 9 ms 848 KB Output is correct
28 Correct 4 ms 848 KB Output is correct
29 Correct 6 ms 848 KB Output is correct
30 Correct 20 ms 3128 KB Output is correct
31 Correct 9 ms 2592 KB Output is correct
32 Correct 50 ms 10520 KB Output is correct
33 Correct 26 ms 4136 KB Output is correct
34 Correct 440 ms 26824 KB Output is correct
35 Correct 368 ms 19864 KB Output is correct
36 Correct 314 ms 11344 KB Output is correct
37 Correct 486 ms 17488 KB Output is correct
38 Correct 203 ms 8480 KB Output is correct
39 Correct 289 ms 11020 KB Output is correct
40 Correct 519 ms 24352 KB Output is correct
41 Correct 314 ms 16948 KB Output is correct
42 Correct 186 ms 12068 KB Output is correct
43 Correct 502 ms 20060 KB Output is correct
44 Correct 336 ms 18724 KB Output is correct
45 Correct 261 ms 25916 KB Output is correct
46 Correct 13 ms 15312 KB Output is correct
47 Correct 18 ms 15812 KB Output is correct
48 Correct 618 ms 67476 KB Output is correct
49 Correct 285 ms 34620 KB Output is correct
50 Execution timed out 4083 ms 256340 KB Time limit exceeded
51 Halted 0 ms 0 KB -