답안 #768686

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
768686 2023-06-28T11:53:48 Z jakobrs Joker (BOI20_joker) C++17
100 / 100
1706 ms 13424 KB
#pragma GCC optimize("O3")

#include <algorithm>
#include <iostream>
#include <vector>

using i64 = int64_t;
using i32 = int32_t;

struct Operation {
    i32 l1, r1;
};

constexpr i64 root(i64 n) {
    i64 i = 0;
    for (; i * i <= n; i++)
        ;
    return i - 1;
}

class UnionFind {
    std::vector<i32> parent;
    std::vector<i32> sizes;
    std::vector<bool> edge_parity;

    std::vector<Operation> operations;

   public:
    UnionFind(size_t sz)
        : parent(sz, -1), sizes(sz, 1), edge_parity(sz, false), operations() {}

    template <bool PATH_COMPRESSION>
    i32 find(i32 idx) {
        if (parent[idx] < 0)
            return idx;
        else if constexpr (PATH_COMPRESSION) {
            i64 root = find<PATH_COMPRESSION>(parent[idx]);
            edge_parity[idx] = edge_parity[idx] ^ edge_parity[parent[idx]];
            return parent[idx] = root;
        } else
            return find<PATH_COMPRESSION>(parent[idx]);
    }

    bool parity(i32 idx) {
        if (parent[idx] < 0)
            return false;
        else
            return parity(parent[idx]) ^ edge_parity[idx];
    }

    template <bool PATH_COMPRESSION>
    std::pair<i32, bool> both(i32 idx) {
        if constexpr (PATH_COMPRESSION) {
            return {find<true>(idx), parity(idx)};
        } else {
            // in this case there's an easy iterative version
            bool parity = false;

            while (parent[idx] >= 0) {
                parity = parity ^ edge_parity[idx];
                idx = parent[idx];
            }

            return {idx, parity};
        }
    }

    template <bool PATH_COMPRESSION>
    bool lag_fagforening(i32 l, i32 r) {
        auto [l1, lp] = both<PATH_COMPRESSION>(l);
        auto [r1, rp] = both<PATH_COMPRESSION>(r);

        operations.push_back({
            .l1 = l1,
            .r1 = r1,
        });

        if (l1 == r1) {
            return lp != rp;
        } else {
            if (sizes[l1] < sizes[r1]) {
                std::swap(l1, r1);
                std::swap(lp, rp);
            }

            sizes[l1] += sizes[r1];
            parent[r1] = l1;

            edge_parity[r1] = 1 ^ lp ^ rp;

            return true;
        }
    }

    void tvungen_lønnsnemnd() {
        auto [l1, r1] = operations.back();
        operations.pop_back();

        if (parent[r1] == l1) {
            parent[r1] = -1;
            sizes[l1] -= sizes[r1];
        } else if (parent[l1] == r1) {
            parent[l1] = -1;
            sizes[r1] -= sizes[l1];
        }

        edge_parity[l1] = edge_parity[r1] = false;
    }
};

struct {
    template <typename T>
    operator T() const {
        T x;
        std::cin >> x;
        return x;
    }
} in;

#define entire(v) std::begin(v), std::end(v)

#pragma GCC diagnostic ignored "-Wreturn-type"

main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    i32 n, m, q;
    std::cin >> n >> m >> q;

    std::vector<std::pair<i32, i32>> edges(m);
    for (i32 i = 0; i < m; i++) {
        edges[i] = {(i32)in - 1, (i32)in - 1};
    }

    UnionFind dsu(n);

    static constexpr i32 root_m = 1024;

    struct Query {
        i32 l, r, i;
    };

    std::vector<Query> queries(q);
    for (i32 i = 0; i < q; i++) queries[i] = {(i32)in - 1, (i32)in, i};

    std::sort(entire(queries), [](auto &&lhs, auto &&rhs) {
        return std::pair<i32, i32>{lhs.l / root_m, -lhs.r} <
               std::pair<i32, i32>{rhs.l / root_m, -rhs.r};
    });

    std::vector<bool> answers(q);

    i32 l = 0;
    i32 r = 0;

    bool is_bipartite_left = true;
    for (i32 start = 0; start < m && is_bipartite_left; start += root_m) {
        l = r;
        while (r < q && queries[r].l < start + root_m) r++;

        bool is_bipartite_right = true;
        i32 w = m;
        for (; l < r && is_bipartite_right; l++) {
            for (; w > queries[l].r && is_bipartite_right; w--)
                is_bipartite_right &= dsu.lag_fagforening<false>(edges[w - 1].first, edges[w - 1].second);

            if (!is_bipartite_right) break;

            bool is_bipartite = true;
            i32 g = start;
            for (; g < queries[l].l && is_bipartite; g++)
                is_bipartite &= dsu.lag_fagforening<false>(edges[g].first, edges[g].second);

            answers[queries[l].i] = is_bipartite;

            for (i32 u = start; u < g; u++) dsu.tvungen_lønnsnemnd();
        }

        if (!is_bipartite_right)
            for (; l < r; l++)
              answers[queries[l].i] = false;

        for (i32 x = w; x < m; x++) dsu.tvungen_lønnsnemnd();

        for (i32 y = start; y < start + root_m && y < m; y++)
            is_bipartite_left &= dsu.lag_fagforening<true>(edges[y].first, edges[y].second);
    }

    if (!is_bipartite_left)
        for (; l < q; l++)
            answers[queries[l].i] = false;

    for (auto x : answers) {
        std::cout << (x ? "NO\n" : "YES\n");
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 10 ms 384 KB Output is correct
30 Correct 7 ms 400 KB Output is correct
31 Correct 5 ms 340 KB Output is correct
32 Correct 4 ms 340 KB Output is correct
33 Correct 3 ms 340 KB Output is correct
34 Correct 6 ms 340 KB Output is correct
35 Correct 8 ms 340 KB Output is correct
36 Correct 5 ms 352 KB Output is correct
37 Correct 9 ms 408 KB Output is correct
38 Correct 14 ms 404 KB Output is correct
39 Correct 8 ms 340 KB Output is correct
40 Correct 5 ms 340 KB Output is correct
41 Correct 5 ms 384 KB Output is correct
42 Correct 6 ms 468 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 5 ms 340 KB Output is correct
48 Correct 6 ms 340 KB Output is correct
49 Correct 6 ms 340 KB Output is correct
50 Correct 7 ms 404 KB Output is correct
51 Correct 7 ms 380 KB Output is correct
52 Correct 7 ms 340 KB Output is correct
53 Correct 5 ms 340 KB Output is correct
54 Correct 6 ms 400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 65 ms 6560 KB Output is correct
4 Correct 72 ms 8116 KB Output is correct
5 Correct 84 ms 7808 KB Output is correct
6 Correct 65 ms 6544 KB Output is correct
7 Correct 73 ms 6720 KB Output is correct
8 Correct 63 ms 6480 KB Output is correct
9 Correct 72 ms 6808 KB Output is correct
10 Correct 78 ms 7628 KB Output is correct
11 Correct 68 ms 6612 KB Output is correct
12 Correct 65 ms 7252 KB Output is correct
13 Correct 63 ms 6044 KB Output is correct
14 Correct 64 ms 6332 KB Output is correct
15 Correct 66 ms 6992 KB Output is correct
16 Correct 75 ms 7540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 65 ms 6560 KB Output is correct
30 Correct 72 ms 8116 KB Output is correct
31 Correct 84 ms 7808 KB Output is correct
32 Correct 65 ms 6544 KB Output is correct
33 Correct 73 ms 6720 KB Output is correct
34 Correct 63 ms 6480 KB Output is correct
35 Correct 72 ms 6808 KB Output is correct
36 Correct 78 ms 7628 KB Output is correct
37 Correct 68 ms 6612 KB Output is correct
38 Correct 65 ms 7252 KB Output is correct
39 Correct 63 ms 6044 KB Output is correct
40 Correct 64 ms 6332 KB Output is correct
41 Correct 66 ms 6992 KB Output is correct
42 Correct 75 ms 7540 KB Output is correct
43 Correct 272 ms 6720 KB Output is correct
44 Correct 311 ms 8148 KB Output is correct
45 Correct 299 ms 8284 KB Output is correct
46 Correct 204 ms 6732 KB Output is correct
47 Correct 258 ms 6860 KB Output is correct
48 Correct 87 ms 6936 KB Output is correct
49 Correct 103 ms 7732 KB Output is correct
50 Correct 285 ms 6576 KB Output is correct
51 Correct 271 ms 7124 KB Output is correct
52 Correct 249 ms 7640 KB Output is correct
53 Correct 296 ms 6268 KB Output is correct
54 Correct 312 ms 6592 KB Output is correct
55 Correct 326 ms 7168 KB Output is correct
56 Correct 309 ms 7564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 10 ms 384 KB Output is correct
30 Correct 7 ms 400 KB Output is correct
31 Correct 5 ms 340 KB Output is correct
32 Correct 4 ms 340 KB Output is correct
33 Correct 3 ms 340 KB Output is correct
34 Correct 6 ms 340 KB Output is correct
35 Correct 8 ms 340 KB Output is correct
36 Correct 5 ms 352 KB Output is correct
37 Correct 9 ms 408 KB Output is correct
38 Correct 14 ms 404 KB Output is correct
39 Correct 8 ms 340 KB Output is correct
40 Correct 5 ms 340 KB Output is correct
41 Correct 5 ms 384 KB Output is correct
42 Correct 6 ms 468 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 5 ms 340 KB Output is correct
48 Correct 6 ms 340 KB Output is correct
49 Correct 6 ms 340 KB Output is correct
50 Correct 7 ms 404 KB Output is correct
51 Correct 7 ms 380 KB Output is correct
52 Correct 7 ms 340 KB Output is correct
53 Correct 5 ms 340 KB Output is correct
54 Correct 6 ms 400 KB Output is correct
55 Correct 188 ms 3936 KB Output is correct
56 Correct 690 ms 5696 KB Output is correct
57 Correct 47 ms 5608 KB Output is correct
58 Correct 40 ms 3920 KB Output is correct
59 Correct 69 ms 4060 KB Output is correct
60 Correct 140 ms 4572 KB Output is correct
61 Correct 99 ms 4056 KB Output is correct
62 Correct 189 ms 4700 KB Output is correct
63 Correct 49 ms 3432 KB Output is correct
64 Correct 111 ms 4300 KB Output is correct
65 Correct 194 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 10 ms 384 KB Output is correct
30 Correct 7 ms 400 KB Output is correct
31 Correct 5 ms 340 KB Output is correct
32 Correct 4 ms 340 KB Output is correct
33 Correct 3 ms 340 KB Output is correct
34 Correct 6 ms 340 KB Output is correct
35 Correct 8 ms 340 KB Output is correct
36 Correct 5 ms 352 KB Output is correct
37 Correct 9 ms 408 KB Output is correct
38 Correct 14 ms 404 KB Output is correct
39 Correct 8 ms 340 KB Output is correct
40 Correct 5 ms 340 KB Output is correct
41 Correct 5 ms 384 KB Output is correct
42 Correct 6 ms 468 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 5 ms 340 KB Output is correct
48 Correct 6 ms 340 KB Output is correct
49 Correct 6 ms 340 KB Output is correct
50 Correct 7 ms 404 KB Output is correct
51 Correct 7 ms 380 KB Output is correct
52 Correct 7 ms 340 KB Output is correct
53 Correct 5 ms 340 KB Output is correct
54 Correct 6 ms 400 KB Output is correct
55 Correct 65 ms 6560 KB Output is correct
56 Correct 72 ms 8116 KB Output is correct
57 Correct 84 ms 7808 KB Output is correct
58 Correct 65 ms 6544 KB Output is correct
59 Correct 73 ms 6720 KB Output is correct
60 Correct 63 ms 6480 KB Output is correct
61 Correct 72 ms 6808 KB Output is correct
62 Correct 78 ms 7628 KB Output is correct
63 Correct 68 ms 6612 KB Output is correct
64 Correct 65 ms 7252 KB Output is correct
65 Correct 63 ms 6044 KB Output is correct
66 Correct 64 ms 6332 KB Output is correct
67 Correct 66 ms 6992 KB Output is correct
68 Correct 75 ms 7540 KB Output is correct
69 Correct 272 ms 6720 KB Output is correct
70 Correct 311 ms 8148 KB Output is correct
71 Correct 299 ms 8284 KB Output is correct
72 Correct 204 ms 6732 KB Output is correct
73 Correct 258 ms 6860 KB Output is correct
74 Correct 87 ms 6936 KB Output is correct
75 Correct 103 ms 7732 KB Output is correct
76 Correct 285 ms 6576 KB Output is correct
77 Correct 271 ms 7124 KB Output is correct
78 Correct 249 ms 7640 KB Output is correct
79 Correct 296 ms 6268 KB Output is correct
80 Correct 312 ms 6592 KB Output is correct
81 Correct 326 ms 7168 KB Output is correct
82 Correct 309 ms 7564 KB Output is correct
83 Correct 188 ms 3936 KB Output is correct
84 Correct 690 ms 5696 KB Output is correct
85 Correct 47 ms 5608 KB Output is correct
86 Correct 40 ms 3920 KB Output is correct
87 Correct 69 ms 4060 KB Output is correct
88 Correct 140 ms 4572 KB Output is correct
89 Correct 99 ms 4056 KB Output is correct
90 Correct 189 ms 4700 KB Output is correct
91 Correct 49 ms 3432 KB Output is correct
92 Correct 111 ms 4300 KB Output is correct
93 Correct 194 ms 4700 KB Output is correct
94 Correct 662 ms 6868 KB Output is correct
95 Correct 1706 ms 9400 KB Output is correct
96 Correct 841 ms 13032 KB Output is correct
97 Correct 558 ms 11584 KB Output is correct
98 Correct 518 ms 11524 KB Output is correct
99 Correct 110 ms 11336 KB Output is correct
100 Correct 166 ms 12576 KB Output is correct
101 Correct 589 ms 11360 KB Output is correct
102 Correct 704 ms 11964 KB Output is correct
103 Correct 853 ms 12628 KB Output is correct
104 Correct 549 ms 10988 KB Output is correct
105 Correct 714 ms 11760 KB Output is correct
106 Correct 947 ms 12352 KB Output is correct
107 Correct 605 ms 13424 KB Output is correct
108 Correct 1242 ms 11392 KB Output is correct
109 Correct 1327 ms 11340 KB Output is correct
110 Correct 1402 ms 11296 KB Output is correct
111 Correct 1350 ms 11288 KB Output is correct
112 Correct 1373 ms 11300 KB Output is correct
113 Correct 1575 ms 11288 KB Output is correct
114 Correct 1316 ms 11332 KB Output is correct
115 Correct 1451 ms 11288 KB Output is correct
116 Correct 1443 ms 11280 KB Output is correct