답안 #1049683

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1049683 2024-08-09T03:56:52 Z shiomusubi496 Joker (BOI20_joker) C++17
71 / 100
2000 ms 20156 KB
#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)

using namespace std;

using ll = long long;

template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }

class UndoableUF {
    int n;
    bool flag = true;
    vector<int> par;
    vector<tuple<int, int, bool>> history;

public:
    UndoableUF(int n_) : n(n_ / 2), par(n_, -1) {}
    int find(int x) { return par[x] < 0 ? x : find(par[x]); }
    bool same(int x, int y) { return find(x) == find(y); }
    bool merge(int x, int y) {
        x = find(x), y = find(y);
        history.emplace_back(x, par[x], flag);
        history.emplace_back(y, par[y], flag);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        if (same(x, (x + n) % (2 * n))) flag = false;
        return true;
    }
    int find2(int x) {
        if (par[x] < 0) return x;
        history.emplace_back(x, par[x], flag);
        return par[x] = find2(par[x]);
    }
    bool same2(int x, int y) { return find2(x) == find2(y); }
    bool merge2(int x, int y) {
        x = find2(x), y = find2(y);
        history.emplace_back(x, par[x], flag);
        history.emplace_back(y, par[y], flag);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        if (same2(x, (x + n) % (2 * n))) flag = false;
        return true;
    }
    void undo() {
        auto [x, px, _] = history.back(); history.pop_back();
        auto [y, py, f] = history.back(); history.pop_back();
        par[x] = px;
        par[y] = py;
        flag = f;
    }
    void snapshot() { history.clear(); }
    void rollback() {
        while (!history.empty()) {
            auto [x, px, f] = history.back(); history.pop_back();
            par[x] = px;
            flag = f;
        }
    }
    bool is_bip() { return flag; }
};

int main() {
    int N, M, Q; cin >> N >> M >> Q;
    vector<pair<int, int>> A(M);
    rep (i, M) {
        cin >> A[i].first >> A[i].second;
        --A[i].first, --A[i].second;
    }
    vector<array<int, 3>> B(Q);
    rep (i, Q) {
        cin >> B[i][0] >> B[i][1];
        --B[i][0];
        B[i][2] = i;
    }
    int block = M / sqrt(Q);
    sort(all(B), [&](auto a, auto b) {
        if (a[0] / block != b[0] / block) return a[0] < b[0];
        return a[1] > b[1];
    });
    vector<bool> ans(Q);
    int curl = block, curr = M;
    UndoableUF uf(2 * N);
    bool f = false;
    for (auto [l, r, k] : B) {
        if (f) {
            ans[k] = false;
            continue;
        }
        while (curl <= l) {
            uf.rollback();
            rep2 (i, curl - block, curl) {
                if (i >= M) break;
                uf.merge2(A[i].first, A[i].second + N);
                uf.merge2(A[i].first + N, A[i].second);
            }
            uf.snapshot();
            if (!uf.is_bip()) {
                ans[k] = false;
                f = true;
                break;
            }
            curl += block;
            curr = M;
        }
        if (f) continue;
        assert(curr >= r);
        rrep2 (i, r, curr) {
            uf.merge(A[i].first, A[i].second + N);
            uf.merge(A[i].first + N, A[i].second);
        }
        curr = r;
        rep2 (i, curl - block, l) {
            uf.merge(A[i].first, A[i].second + N);
            uf.merge(A[i].first + N, A[i].second);
        }
        ans[k] = uf.is_bip();
        rep2 (i, curl - block, l) uf.undo(), uf.undo();
    }
    rep (i, Q) puts(ans[i] ? "NO" : "YES");
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 3 ms 668 KB Output is correct
31 Correct 3 ms 604 KB Output is correct
32 Correct 3 ms 604 KB Output is correct
33 Correct 2 ms 600 KB Output is correct
34 Correct 3 ms 604 KB Output is correct
35 Correct 4 ms 676 KB Output is correct
36 Correct 3 ms 600 KB Output is correct
37 Correct 5 ms 444 KB Output is correct
38 Correct 4 ms 604 KB Output is correct
39 Correct 4 ms 444 KB Output is correct
40 Correct 4 ms 672 KB Output is correct
41 Correct 3 ms 612 KB Output is correct
42 Correct 3 ms 604 KB Output is correct
43 Correct 3 ms 668 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 4 ms 604 KB Output is correct
46 Correct 4 ms 604 KB Output is correct
47 Correct 4 ms 604 KB Output is correct
48 Correct 4 ms 604 KB Output is correct
49 Correct 4 ms 604 KB Output is correct
50 Correct 4 ms 604 KB Output is correct
51 Correct 4 ms 604 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 3 ms 604 KB Output is correct
54 Correct 3 ms 680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 158 ms 19392 KB Output is correct
4 Correct 158 ms 19364 KB Output is correct
5 Correct 181 ms 19500 KB Output is correct
6 Correct 152 ms 19392 KB Output is correct
7 Correct 160 ms 18368 KB Output is correct
8 Correct 143 ms 17216 KB Output is correct
9 Correct 147 ms 19136 KB Output is correct
10 Correct 155 ms 18484 KB Output is correct
11 Correct 165 ms 18988 KB Output is correct
12 Correct 161 ms 18240 KB Output is correct
13 Correct 165 ms 18620 KB Output is correct
14 Correct 183 ms 18372 KB Output is correct
15 Correct 160 ms 18368 KB Output is correct
16 Correct 156 ms 19900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 158 ms 19392 KB Output is correct
30 Correct 158 ms 19364 KB Output is correct
31 Correct 181 ms 19500 KB Output is correct
32 Correct 152 ms 19392 KB Output is correct
33 Correct 160 ms 18368 KB Output is correct
34 Correct 143 ms 17216 KB Output is correct
35 Correct 147 ms 19136 KB Output is correct
36 Correct 155 ms 18484 KB Output is correct
37 Correct 165 ms 18988 KB Output is correct
38 Correct 161 ms 18240 KB Output is correct
39 Correct 165 ms 18620 KB Output is correct
40 Correct 183 ms 18372 KB Output is correct
41 Correct 160 ms 18368 KB Output is correct
42 Correct 156 ms 19900 KB Output is correct
43 Correct 657 ms 19108 KB Output is correct
44 Correct 689 ms 18360 KB Output is correct
45 Correct 664 ms 18264 KB Output is correct
46 Correct 640 ms 17596 KB Output is correct
47 Correct 690 ms 17720 KB Output is correct
48 Correct 659 ms 19540 KB Output is correct
49 Correct 743 ms 18492 KB Output is correct
50 Correct 583 ms 17592 KB Output is correct
51 Correct 635 ms 18104 KB Output is correct
52 Correct 634 ms 19380 KB Output is correct
53 Correct 562 ms 17080 KB Output is correct
54 Correct 708 ms 17708 KB Output is correct
55 Correct 726 ms 19384 KB Output is correct
56 Correct 713 ms 19792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 3 ms 668 KB Output is correct
31 Correct 3 ms 604 KB Output is correct
32 Correct 3 ms 604 KB Output is correct
33 Correct 2 ms 600 KB Output is correct
34 Correct 3 ms 604 KB Output is correct
35 Correct 4 ms 676 KB Output is correct
36 Correct 3 ms 600 KB Output is correct
37 Correct 5 ms 444 KB Output is correct
38 Correct 4 ms 604 KB Output is correct
39 Correct 4 ms 444 KB Output is correct
40 Correct 4 ms 672 KB Output is correct
41 Correct 3 ms 612 KB Output is correct
42 Correct 3 ms 604 KB Output is correct
43 Correct 3 ms 668 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 4 ms 604 KB Output is correct
46 Correct 4 ms 604 KB Output is correct
47 Correct 4 ms 604 KB Output is correct
48 Correct 4 ms 604 KB Output is correct
49 Correct 4 ms 604 KB Output is correct
50 Correct 4 ms 604 KB Output is correct
51 Correct 4 ms 604 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 3 ms 604 KB Output is correct
54 Correct 3 ms 680 KB Output is correct
55 Correct 229 ms 16320 KB Output is correct
56 Correct 401 ms 17600 KB Output is correct
57 Correct 400 ms 16828 KB Output is correct
58 Correct 277 ms 16316 KB Output is correct
59 Correct 373 ms 16316 KB Output is correct
60 Correct 396 ms 15808 KB Output is correct
61 Correct 287 ms 15552 KB Output is correct
62 Correct 327 ms 16832 KB Output is correct
63 Correct 277 ms 15684 KB Output is correct
64 Correct 375 ms 17204 KB Output is correct
65 Correct 391 ms 16320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 3 ms 668 KB Output is correct
31 Correct 3 ms 604 KB Output is correct
32 Correct 3 ms 604 KB Output is correct
33 Correct 2 ms 600 KB Output is correct
34 Correct 3 ms 604 KB Output is correct
35 Correct 4 ms 676 KB Output is correct
36 Correct 3 ms 600 KB Output is correct
37 Correct 5 ms 444 KB Output is correct
38 Correct 4 ms 604 KB Output is correct
39 Correct 4 ms 444 KB Output is correct
40 Correct 4 ms 672 KB Output is correct
41 Correct 3 ms 612 KB Output is correct
42 Correct 3 ms 604 KB Output is correct
43 Correct 3 ms 668 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 4 ms 604 KB Output is correct
46 Correct 4 ms 604 KB Output is correct
47 Correct 4 ms 604 KB Output is correct
48 Correct 4 ms 604 KB Output is correct
49 Correct 4 ms 604 KB Output is correct
50 Correct 4 ms 604 KB Output is correct
51 Correct 4 ms 604 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 3 ms 604 KB Output is correct
54 Correct 3 ms 680 KB Output is correct
55 Correct 158 ms 19392 KB Output is correct
56 Correct 158 ms 19364 KB Output is correct
57 Correct 181 ms 19500 KB Output is correct
58 Correct 152 ms 19392 KB Output is correct
59 Correct 160 ms 18368 KB Output is correct
60 Correct 143 ms 17216 KB Output is correct
61 Correct 147 ms 19136 KB Output is correct
62 Correct 155 ms 18484 KB Output is correct
63 Correct 165 ms 18988 KB Output is correct
64 Correct 161 ms 18240 KB Output is correct
65 Correct 165 ms 18620 KB Output is correct
66 Correct 183 ms 18372 KB Output is correct
67 Correct 160 ms 18368 KB Output is correct
68 Correct 156 ms 19900 KB Output is correct
69 Correct 657 ms 19108 KB Output is correct
70 Correct 689 ms 18360 KB Output is correct
71 Correct 664 ms 18264 KB Output is correct
72 Correct 640 ms 17596 KB Output is correct
73 Correct 690 ms 17720 KB Output is correct
74 Correct 659 ms 19540 KB Output is correct
75 Correct 743 ms 18492 KB Output is correct
76 Correct 583 ms 17592 KB Output is correct
77 Correct 635 ms 18104 KB Output is correct
78 Correct 634 ms 19380 KB Output is correct
79 Correct 562 ms 17080 KB Output is correct
80 Correct 708 ms 17708 KB Output is correct
81 Correct 726 ms 19384 KB Output is correct
82 Correct 713 ms 19792 KB Output is correct
83 Correct 229 ms 16320 KB Output is correct
84 Correct 401 ms 17600 KB Output is correct
85 Correct 400 ms 16828 KB Output is correct
86 Correct 277 ms 16316 KB Output is correct
87 Correct 373 ms 16316 KB Output is correct
88 Correct 396 ms 15808 KB Output is correct
89 Correct 287 ms 15552 KB Output is correct
90 Correct 327 ms 16832 KB Output is correct
91 Correct 277 ms 15684 KB Output is correct
92 Correct 375 ms 17204 KB Output is correct
93 Correct 391 ms 16320 KB Output is correct
94 Correct 1816 ms 18620 KB Output is correct
95 Execution timed out 2047 ms 20156 KB Time limit exceeded
96 Halted 0 ms 0 KB -