Submission #727173

# Submission time Handle Problem Language Result Execution time Memory
727173 2023-04-20T06:52:03 Z marvinthang Game (APIO22_game) C++17
100 / 100
1344 ms 63960 KB
/*************************************
*    author: marvinthang             *
*    created: 20.04.2023 08:56:45    *
*************************************/

#include <bits/stdc++.h>
#include "game.h"

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

// end of template

const int MAX = 3e5 + 5;
vector <int> adj[MAX], radj[MAX];

int N, K, minFrom[MAX], maxTo[MAX], last_diff[MAX];

void init(int n, int k) {
    N = n; K = k;
    REP(i, N) {
        if (i < K) {
            minFrom[i] = i + 1;
            maxTo[i] = i;
        } else {
            minFrom[i] = K + 1;
            maxTo[i] = 0;
        }
    }
    REP(i, N) last_diff[i] = __lg(maxTo[i] ^ minFrom[i]);
}

bool res;

void dfs(int u) {
    if (res) return;
    if (minFrom[u] <= maxTo[u]) {
        res = true;
        return;
    }
    int nxt_diff = __lg(maxTo[u] ^ minFrom[u]);
    if (nxt_diff == last_diff[u]) return;
    last_diff[u] = nxt_diff;
    for (int v: adj[u]) if (maximize(maxTo[v], maxTo[u])) dfs(v);
    for (int v: radj[u]) if (minimize(minFrom[v], minFrom[u])) dfs(v);
}

int add_teleporter(int u, int v) {
    if (res) return true;
    adj[u].push_back(v);
    radj[v].push_back(u);
    if (maximize(maxTo[v], u < K ? max(u + 1, maxTo[u]) : maxTo[u])) dfs(v);
    if (minimize(minFrom[u], minFrom[v])) dfs(u);
    return res;
}

#ifdef LOCAL
#include <cstdio>
#include <cstdlib>
#include <vector>
#include "game.h"

namespace {

int read_int() {
    int x;
    if (scanf("%d", &x) != 1) {
        fprintf(stderr, "Error while reading input\n");
        exit(1);
    }
    return x;
}

}  // namespace

int main() {
    file("game");
    int N = read_int();
    int M = read_int();
    int K = read_int();
    std::vector<int> u(M), v(M);
    for (int i = 0; i < M; ++i) {
        u[i] = read_int();
        v[i] = read_int();
    }

    init(N, K);
    int i;
    for (i = 0; i < M; ++i) {
        int answer = add_teleporter(u[i], v[i]);
        if (answer != 0 && answer != 1) {
            i = -1;
            break;
        } else if (answer == 1) {
            break;
        }
    }
    printf("%d\n", i);
    return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14288 KB Output is correct
2 Correct 8 ms 14288 KB Output is correct
3 Correct 7 ms 14288 KB Output is correct
4 Correct 7 ms 14416 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14288 KB Output is correct
7 Correct 7 ms 14288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14288 KB Output is correct
2 Correct 8 ms 14288 KB Output is correct
3 Correct 7 ms 14288 KB Output is correct
4 Correct 7 ms 14416 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14288 KB Output is correct
7 Correct 7 ms 14288 KB Output is correct
8 Correct 9 ms 14288 KB Output is correct
9 Correct 8 ms 14288 KB Output is correct
10 Correct 9 ms 14284 KB Output is correct
11 Correct 9 ms 14356 KB Output is correct
12 Correct 8 ms 14288 KB Output is correct
13 Correct 8 ms 14288 KB Output is correct
14 Correct 9 ms 14364 KB Output is correct
15 Correct 8 ms 14416 KB Output is correct
16 Correct 8 ms 14416 KB Output is correct
17 Correct 8 ms 14288 KB Output is correct
18 Correct 8 ms 14288 KB Output is correct
19 Correct 7 ms 14288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14288 KB Output is correct
2 Correct 8 ms 14288 KB Output is correct
3 Correct 7 ms 14288 KB Output is correct
4 Correct 7 ms 14416 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14288 KB Output is correct
7 Correct 7 ms 14288 KB Output is correct
8 Correct 9 ms 14288 KB Output is correct
9 Correct 8 ms 14288 KB Output is correct
10 Correct 9 ms 14284 KB Output is correct
11 Correct 9 ms 14356 KB Output is correct
12 Correct 8 ms 14288 KB Output is correct
13 Correct 8 ms 14288 KB Output is correct
14 Correct 9 ms 14364 KB Output is correct
15 Correct 8 ms 14416 KB Output is correct
16 Correct 8 ms 14416 KB Output is correct
17 Correct 8 ms 14288 KB Output is correct
18 Correct 8 ms 14288 KB Output is correct
19 Correct 7 ms 14288 KB Output is correct
20 Correct 10 ms 14416 KB Output is correct
21 Correct 9 ms 14428 KB Output is correct
22 Correct 8 ms 14416 KB Output is correct
23 Correct 9 ms 14408 KB Output is correct
24 Correct 9 ms 14472 KB Output is correct
25 Correct 13 ms 14544 KB Output is correct
26 Correct 10 ms 14480 KB Output is correct
27 Correct 10 ms 14448 KB Output is correct
28 Correct 9 ms 14456 KB Output is correct
29 Correct 10 ms 14476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14288 KB Output is correct
2 Correct 8 ms 14288 KB Output is correct
3 Correct 7 ms 14288 KB Output is correct
4 Correct 7 ms 14416 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14288 KB Output is correct
7 Correct 7 ms 14288 KB Output is correct
8 Correct 9 ms 14288 KB Output is correct
9 Correct 8 ms 14288 KB Output is correct
10 Correct 9 ms 14284 KB Output is correct
11 Correct 9 ms 14356 KB Output is correct
12 Correct 8 ms 14288 KB Output is correct
13 Correct 8 ms 14288 KB Output is correct
14 Correct 9 ms 14364 KB Output is correct
15 Correct 8 ms 14416 KB Output is correct
16 Correct 8 ms 14416 KB Output is correct
17 Correct 8 ms 14288 KB Output is correct
18 Correct 8 ms 14288 KB Output is correct
19 Correct 7 ms 14288 KB Output is correct
20 Correct 10 ms 14416 KB Output is correct
21 Correct 9 ms 14428 KB Output is correct
22 Correct 8 ms 14416 KB Output is correct
23 Correct 9 ms 14408 KB Output is correct
24 Correct 9 ms 14472 KB Output is correct
25 Correct 13 ms 14544 KB Output is correct
26 Correct 10 ms 14480 KB Output is correct
27 Correct 10 ms 14448 KB Output is correct
28 Correct 9 ms 14456 KB Output is correct
29 Correct 10 ms 14476 KB Output is correct
30 Correct 26 ms 15788 KB Output is correct
31 Correct 16 ms 15152 KB Output is correct
32 Correct 29 ms 17416 KB Output is correct
33 Correct 31 ms 16548 KB Output is correct
34 Correct 31 ms 20168 KB Output is correct
35 Correct 39 ms 17888 KB Output is correct
36 Correct 44 ms 16516 KB Output is correct
37 Correct 42 ms 16556 KB Output is correct
38 Correct 37 ms 16360 KB Output is correct
39 Correct 46 ms 16328 KB Output is correct
40 Correct 43 ms 19984 KB Output is correct
41 Correct 40 ms 17324 KB Output is correct
42 Correct 49 ms 16968 KB Output is correct
43 Correct 41 ms 17608 KB Output is correct
44 Correct 47 ms 17292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14288 KB Output is correct
2 Correct 8 ms 14288 KB Output is correct
3 Correct 7 ms 14288 KB Output is correct
4 Correct 7 ms 14416 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 7 ms 14288 KB Output is correct
7 Correct 7 ms 14288 KB Output is correct
8 Correct 9 ms 14288 KB Output is correct
9 Correct 8 ms 14288 KB Output is correct
10 Correct 9 ms 14284 KB Output is correct
11 Correct 9 ms 14356 KB Output is correct
12 Correct 8 ms 14288 KB Output is correct
13 Correct 8 ms 14288 KB Output is correct
14 Correct 9 ms 14364 KB Output is correct
15 Correct 8 ms 14416 KB Output is correct
16 Correct 8 ms 14416 KB Output is correct
17 Correct 8 ms 14288 KB Output is correct
18 Correct 8 ms 14288 KB Output is correct
19 Correct 7 ms 14288 KB Output is correct
20 Correct 10 ms 14416 KB Output is correct
21 Correct 9 ms 14428 KB Output is correct
22 Correct 8 ms 14416 KB Output is correct
23 Correct 9 ms 14408 KB Output is correct
24 Correct 9 ms 14472 KB Output is correct
25 Correct 13 ms 14544 KB Output is correct
26 Correct 10 ms 14480 KB Output is correct
27 Correct 10 ms 14448 KB Output is correct
28 Correct 9 ms 14456 KB Output is correct
29 Correct 10 ms 14476 KB Output is correct
30 Correct 26 ms 15788 KB Output is correct
31 Correct 16 ms 15152 KB Output is correct
32 Correct 29 ms 17416 KB Output is correct
33 Correct 31 ms 16548 KB Output is correct
34 Correct 31 ms 20168 KB Output is correct
35 Correct 39 ms 17888 KB Output is correct
36 Correct 44 ms 16516 KB Output is correct
37 Correct 42 ms 16556 KB Output is correct
38 Correct 37 ms 16360 KB Output is correct
39 Correct 46 ms 16328 KB Output is correct
40 Correct 43 ms 19984 KB Output is correct
41 Correct 40 ms 17324 KB Output is correct
42 Correct 49 ms 16968 KB Output is correct
43 Correct 41 ms 17608 KB Output is correct
44 Correct 47 ms 17292 KB Output is correct
45 Correct 291 ms 29216 KB Output is correct
46 Correct 16 ms 18256 KB Output is correct
47 Correct 15 ms 18264 KB Output is correct
48 Correct 309 ms 46140 KB Output is correct
49 Correct 291 ms 36660 KB Output is correct
50 Correct 609 ms 51448 KB Output is correct
51 Correct 836 ms 45536 KB Output is correct
52 Correct 594 ms 36572 KB Output is correct
53 Correct 650 ms 36872 KB Output is correct
54 Correct 674 ms 36596 KB Output is correct
55 Correct 937 ms 36948 KB Output is correct
56 Correct 924 ms 36656 KB Output is correct
57 Correct 460 ms 30568 KB Output is correct
58 Correct 457 ms 30552 KB Output is correct
59 Correct 464 ms 30656 KB Output is correct
60 Correct 455 ms 30692 KB Output is correct
61 Correct 452 ms 30252 KB Output is correct
62 Correct 472 ms 40728 KB Output is correct
63 Correct 550 ms 37284 KB Output is correct
64 Correct 1344 ms 63960 KB Output is correct
65 Correct 574 ms 37352 KB Output is correct
66 Correct 407 ms 33276 KB Output is correct
67 Correct 807 ms 42584 KB Output is correct
68 Correct 221 ms 27416 KB Output is correct
69 Correct 57 ms 21248 KB Output is correct
70 Correct 710 ms 41692 KB Output is correct
71 Correct 346 ms 30264 KB Output is correct
72 Correct 261 ms 25932 KB Output is correct
73 Correct 678 ms 39232 KB Output is correct
74 Correct 953 ms 36240 KB Output is correct
75 Correct 1030 ms 46292 KB Output is correct
76 Correct 909 ms 45436 KB Output is correct