답안 #394735

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
394735 2021-04-27T08:55:43 Z KoD Joker (BOI20_joker) C++17
100 / 100
1064 ms 27380 KB
#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept {
            return itr != other.itr;
        }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept
        : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class F> struct RecLambda : private F {
    template <class G>
    explicit constexpr RecLambda(G&& g) : F(std::forward<G>(g)) {}
    template <class... Args>
    constexpr decltype(auto) operator()(Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class G> explicit RecLambda(G&&) -> RecLambda<std::decay_t<G>>;

class UnionFindRollback {
    std::vector<usize> data;
    std::stack<std::pair<usize, usize>> history;

  public:
    explicit UnionFindRollback(const usize size = 0)
        : data(size, -1), history() {}

    usize size() const { return data.size(); }

    usize leader(usize u) const {
        assert(u < size());
        while (data[u] < size()) u = data[u];
        return u;
    }

    usize size(const usize u) const {
        assert(u < size());
        return -data[leader(u)];
    }

    std::pair<usize, bool> merge(usize u, usize v) {
        assert(u < size());
        assert(v < size());
        u = leader(u);
        v = leader(v);
        if (u == v) return std::make_pair(u, false);
        if (data[u] > data[v]) std::swap(u, v);
        history.emplace(u, data[u]);
        history.emplace(v, data[v]);
        data[u] += data[v];
        data[v] = u;
        return std::make_pair(u, true);
    }

    bool same(const usize u, const usize v) const {
        assert(u < size());
        assert(v < size());
        return leader(u) == leader(v);
    }

    void rollback(const usize steps) {
        assert(2 * steps <= history.size());
        for (usize i = 2 * steps; i > 0; --i) {
            const auto [k, x] = history.top();
            history.pop();
            data[k] = x;
        }
    }
};

template <class T> using Vec = std::vector<T>;

void BOI20_joker_main() {
    usize N, M, Q;
    std::cin >> N >> M >> Q;
    Vec<usize> A(M), B(M);
    for (const auto i : rep(0, M)) {
        std::cin >> A[i] >> B[i];
        A[i] -= 1;
        B[i] -= 1;
    }
    Vec<usize> last(M);
    UnionFindRollback dsu(2 * N);
    Vec<bool> bip;
    bip.push_back(true);
    const auto add_edge = [&](const usize e) {
        if (dsu.merge(A[e], B[e] + N).second) {
            bip.push_back(bip.back() and !dsu.same(A[e], A[e] + N) and
                          !dsu.same(B[e], B[e] + N));
        }
        if (dsu.merge(A[e] + N, B[e]).second) {
            bip.push_back(bip.back() and !dsu.same(A[e], A[e] + N) and
                          !dsu.same(B[e], B[e] + N));
        }
    };
    const auto back_until = [&](const usize time) {
        dsu.rollback(bip.size() - time);
        bip.resize(time);
    };
    RecLambda([&](auto&& dfs, const usize il, const usize ir, const usize jl,
                  const usize jr) -> void {
        if (!bip.back()) {
            std::fill(last.begin() + il, last.begin() + ir + 1, jr);
            return;
        }
        const auto im = (il + ir) / 2;
        {
            const auto time = bip.size();
            for (const auto i : rep(il, im)) {
                add_edge(i);
            }
            last[im] = jr;
            while (bip.back() and last[im] > im) {
                last[im] -= 1;
                add_edge(last[im]);
            }
            back_until(time);
        }
        if (il < im) {
            const auto time = bip.size();
            for (const auto i : rep(last[im], jr)) {
                add_edge(i);
            }
            dfs(il, im - 1, jl, last[im]);
            back_until(time);
        }
        if (im < ir) {
            const auto time = bip.size();
            for (const auto i : rep(il, im + 1)) {
                add_edge(i);
            }
            dfs(im + 1, ir, last[im], jr);
            back_until(time);
        }
    })(0, M - 1, 0, M);
    while (Q--) {
        usize l, r;
        std::cin >> l >> r;
        l -= 1;
        std::cout << (r <= last[l] ? "YES" : "NO") << '\n';
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    BOI20_joker_main();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 3 ms 460 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 3 ms 460 KB Output is correct
32 Correct 3 ms 460 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 3 ms 460 KB Output is correct
35 Correct 4 ms 460 KB Output is correct
36 Correct 4 ms 460 KB Output is correct
37 Correct 5 ms 460 KB Output is correct
38 Correct 6 ms 460 KB Output is correct
39 Correct 3 ms 460 KB Output is correct
40 Correct 3 ms 460 KB Output is correct
41 Correct 3 ms 460 KB Output is correct
42 Correct 3 ms 460 KB Output is correct
43 Correct 3 ms 332 KB Output is correct
44 Correct 3 ms 460 KB Output is correct
45 Correct 3 ms 460 KB Output is correct
46 Correct 4 ms 460 KB Output is correct
47 Correct 3 ms 460 KB Output is correct
48 Correct 3 ms 460 KB Output is correct
49 Correct 4 ms 460 KB Output is correct
50 Correct 4 ms 460 KB Output is correct
51 Correct 3 ms 460 KB Output is correct
52 Correct 3 ms 460 KB Output is correct
53 Correct 4 ms 512 KB Output is correct
54 Correct 4 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 498 ms 17860 KB Output is correct
4 Correct 936 ms 25536 KB Output is correct
5 Correct 254 ms 24556 KB Output is correct
6 Correct 286 ms 17800 KB Output is correct
7 Correct 270 ms 17796 KB Output is correct
8 Correct 283 ms 13948 KB Output is correct
9 Correct 363 ms 16932 KB Output is correct
10 Correct 567 ms 21812 KB Output is correct
11 Correct 433 ms 17316 KB Output is correct
12 Correct 483 ms 21224 KB Output is correct
13 Correct 264 ms 11108 KB Output is correct
14 Correct 308 ms 14496 KB Output is correct
15 Correct 551 ms 19740 KB Output is correct
16 Correct 579 ms 22204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 498 ms 17860 KB Output is correct
30 Correct 936 ms 25536 KB Output is correct
31 Correct 254 ms 24556 KB Output is correct
32 Correct 286 ms 17800 KB Output is correct
33 Correct 270 ms 17796 KB Output is correct
34 Correct 283 ms 13948 KB Output is correct
35 Correct 363 ms 16932 KB Output is correct
36 Correct 567 ms 21812 KB Output is correct
37 Correct 433 ms 17316 KB Output is correct
38 Correct 483 ms 21224 KB Output is correct
39 Correct 264 ms 11108 KB Output is correct
40 Correct 308 ms 14496 KB Output is correct
41 Correct 551 ms 19740 KB Output is correct
42 Correct 579 ms 22204 KB Output is correct
43 Correct 513 ms 16940 KB Output is correct
44 Correct 1064 ms 25916 KB Output is correct
45 Correct 629 ms 26300 KB Output is correct
46 Correct 263 ms 18028 KB Output is correct
47 Correct 300 ms 18140 KB Output is correct
48 Correct 458 ms 17416 KB Output is correct
49 Correct 605 ms 22296 KB Output is correct
50 Correct 434 ms 16224 KB Output is correct
51 Correct 457 ms 19988 KB Output is correct
52 Correct 522 ms 22304 KB Output is correct
53 Correct 254 ms 11420 KB Output is correct
54 Correct 402 ms 16348 KB Output is correct
55 Correct 506 ms 20168 KB Output is correct
56 Correct 615 ms 22552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 3 ms 460 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 3 ms 460 KB Output is correct
32 Correct 3 ms 460 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 3 ms 460 KB Output is correct
35 Correct 4 ms 460 KB Output is correct
36 Correct 4 ms 460 KB Output is correct
37 Correct 5 ms 460 KB Output is correct
38 Correct 6 ms 460 KB Output is correct
39 Correct 3 ms 460 KB Output is correct
40 Correct 3 ms 460 KB Output is correct
41 Correct 3 ms 460 KB Output is correct
42 Correct 3 ms 460 KB Output is correct
43 Correct 3 ms 332 KB Output is correct
44 Correct 3 ms 460 KB Output is correct
45 Correct 3 ms 460 KB Output is correct
46 Correct 4 ms 460 KB Output is correct
47 Correct 3 ms 460 KB Output is correct
48 Correct 3 ms 460 KB Output is correct
49 Correct 4 ms 460 KB Output is correct
50 Correct 4 ms 460 KB Output is correct
51 Correct 3 ms 460 KB Output is correct
52 Correct 3 ms 460 KB Output is correct
53 Correct 4 ms 512 KB Output is correct
54 Correct 4 ms 460 KB Output is correct
55 Correct 459 ms 15492 KB Output is correct
56 Correct 900 ms 23980 KB Output is correct
57 Correct 192 ms 22168 KB Output is correct
58 Correct 201 ms 15508 KB Output is correct
59 Correct 339 ms 15172 KB Output is correct
60 Correct 500 ms 18560 KB Output is correct
61 Correct 401 ms 15148 KB Output is correct
62 Correct 416 ms 19016 KB Output is correct
63 Correct 207 ms 10544 KB Output is correct
64 Correct 397 ms 16472 KB Output is correct
65 Correct 499 ms 19384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 3 ms 460 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 3 ms 460 KB Output is correct
32 Correct 3 ms 460 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 3 ms 460 KB Output is correct
35 Correct 4 ms 460 KB Output is correct
36 Correct 4 ms 460 KB Output is correct
37 Correct 5 ms 460 KB Output is correct
38 Correct 6 ms 460 KB Output is correct
39 Correct 3 ms 460 KB Output is correct
40 Correct 3 ms 460 KB Output is correct
41 Correct 3 ms 460 KB Output is correct
42 Correct 3 ms 460 KB Output is correct
43 Correct 3 ms 332 KB Output is correct
44 Correct 3 ms 460 KB Output is correct
45 Correct 3 ms 460 KB Output is correct
46 Correct 4 ms 460 KB Output is correct
47 Correct 3 ms 460 KB Output is correct
48 Correct 3 ms 460 KB Output is correct
49 Correct 4 ms 460 KB Output is correct
50 Correct 4 ms 460 KB Output is correct
51 Correct 3 ms 460 KB Output is correct
52 Correct 3 ms 460 KB Output is correct
53 Correct 4 ms 512 KB Output is correct
54 Correct 4 ms 460 KB Output is correct
55 Correct 498 ms 17860 KB Output is correct
56 Correct 936 ms 25536 KB Output is correct
57 Correct 254 ms 24556 KB Output is correct
58 Correct 286 ms 17800 KB Output is correct
59 Correct 270 ms 17796 KB Output is correct
60 Correct 283 ms 13948 KB Output is correct
61 Correct 363 ms 16932 KB Output is correct
62 Correct 567 ms 21812 KB Output is correct
63 Correct 433 ms 17316 KB Output is correct
64 Correct 483 ms 21224 KB Output is correct
65 Correct 264 ms 11108 KB Output is correct
66 Correct 308 ms 14496 KB Output is correct
67 Correct 551 ms 19740 KB Output is correct
68 Correct 579 ms 22204 KB Output is correct
69 Correct 513 ms 16940 KB Output is correct
70 Correct 1064 ms 25916 KB Output is correct
71 Correct 629 ms 26300 KB Output is correct
72 Correct 263 ms 18028 KB Output is correct
73 Correct 300 ms 18140 KB Output is correct
74 Correct 458 ms 17416 KB Output is correct
75 Correct 605 ms 22296 KB Output is correct
76 Correct 434 ms 16224 KB Output is correct
77 Correct 457 ms 19988 KB Output is correct
78 Correct 522 ms 22304 KB Output is correct
79 Correct 254 ms 11420 KB Output is correct
80 Correct 402 ms 16348 KB Output is correct
81 Correct 506 ms 20168 KB Output is correct
82 Correct 615 ms 22552 KB Output is correct
83 Correct 459 ms 15492 KB Output is correct
84 Correct 900 ms 23980 KB Output is correct
85 Correct 192 ms 22168 KB Output is correct
86 Correct 201 ms 15508 KB Output is correct
87 Correct 339 ms 15172 KB Output is correct
88 Correct 500 ms 18560 KB Output is correct
89 Correct 401 ms 15148 KB Output is correct
90 Correct 416 ms 19016 KB Output is correct
91 Correct 207 ms 10544 KB Output is correct
92 Correct 397 ms 16472 KB Output is correct
93 Correct 499 ms 19384 KB Output is correct
94 Correct 502 ms 18784 KB Output is correct
95 Correct 552 ms 26988 KB Output is correct
96 Correct 255 ms 25480 KB Output is correct
97 Correct 236 ms 18776 KB Output is correct
98 Correct 282 ms 18792 KB Output is correct
99 Correct 311 ms 15508 KB Output is correct
100 Correct 448 ms 23436 KB Output is correct
101 Correct 346 ms 17040 KB Output is correct
102 Correct 501 ms 20808 KB Output is correct
103 Correct 523 ms 23124 KB Output is correct
104 Correct 286 ms 13848 KB Output is correct
105 Correct 453 ms 19760 KB Output is correct
106 Correct 569 ms 22756 KB Output is correct
107 Correct 199 ms 27380 KB Output is correct
108 Correct 502 ms 18592 KB Output is correct
109 Correct 497 ms 18508 KB Output is correct
110 Correct 506 ms 18656 KB Output is correct
111 Correct 502 ms 18596 KB Output is correct
112 Correct 513 ms 18512 KB Output is correct
113 Correct 503 ms 18712 KB Output is correct
114 Correct 502 ms 18584 KB Output is correct
115 Correct 548 ms 18548 KB Output is correct
116 Correct 503 ms 18552 KB Output is correct