Submission #986445

# Submission time Handle Problem Language Result Execution time Memory
986445 2024-05-20T14:54:47 Z tfgs Joker (BOI20_joker) C++17
49 / 100
766 ms 73580 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#endif

#define f first
#define s second
template<class T> using V = vector<T>; 
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) 
#define pb push_back
#define lb lower_bound
#define ub upper_bound
template<class T> int lwb(V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a=b, true : false; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a=b, true : false; }
#define ASSERT(exp) do { if (!(exp)) while (true); } while(0)

const int max_n = 2e5;
int K = 1.8e4;
struct query {
    int l, r, ind;
    bool operator <(const query& o) const {
        if (l/K == o.l/K) return r > o.r;
        return l/K < o.l/K;
    }
};
V<query> queries[(max_n-1)/200 + 1];
int n, m, q, ans[max_n];
array<int, 2> edges[max_n];

array<int, 2> par[max_n];
int rnk[max_n];
void make_set(int u) {
    par[u] = {u, 0};
    rnk[u] = 0;
}
array<int, 2> find(int u) {
    if (u == par[u][0]) return par[u];
    auto res = find(par[u][0]);
    res[1] ^= par[u][1];
    return res;
}
bool is_bipartite;
V<pair<array<int, 2>&, array<int, 2>>> undo_par;
V<pair<bool&, bool>> undo_bipartite;
V<pair<int&, int>> undo_rnk;
void unite(int u, int v) {
    auto U = find(u), V = find(v);
    if (rnk[U[0]] < rnk[V[0]]) swap(U, V);
    undo_bipartite.pb({is_bipartite, is_bipartite});
    undo_par.pb({par[V[0]], par[V[0]]});
    undo_rnk.pb({rnk[U[0]], rnk[U[0]]});

    if (U[0] == V[0]) {
        if (U[1] == V[1]) {
            is_bipartite = false;
        }
    } else {
        int t = U[1]^V[1]^1;
        par[V[0]] = {U[0], t};
        if (rnk[U[0]] == rnk[V[0]]) rnk[U[0]]++;
    }
}
int snapshot() { return undo_par.size(); }
void rollback(int until) {
    while (undo_par.size() > until) {
        undo_par.back().f = undo_par.back().s;
        undo_bipartite.back().f = undo_bipartite.back().s;
        undo_rnk.back().f = undo_rnk.back().s;
        undo_par.pop_back();
        undo_bipartite.pop_back();
        undo_rnk.pop_back();
    }
}
void reset() {
    is_bipartite = true;
    for (int i = 0; i < n; i++) make_set(i);
    undo_par.clear();
    undo_bipartite.clear();
    undo_rnk.clear();
}

void solve() {
    cin >> n >> m >> q;    
    if (q > 2000) K = 200;

    for (int i = 0; i < m; i++) {
        cin >> edges[i][0] >> edges[i][1];
        edges[i][0]--; edges[i][1]--;
    }

    for (int i = 0; i < q; i++) {
        int l, r; cin >> l >> r; l--;
        queries[l/K].pb({l, r, i});
    }
    for (int i = 0; i <= (m-1)/K; i++) sort(all(queries[i]));
    reset();
    for (int b = 0; b <= (m-1)/K; b++) {
        int l = b*K, r = m;
        for (int i = max((b-1)*K, 0); i < l; i++) unite(edges[i][0], edges[i][1]);
        for (auto [L, R, I] : queries[b]) {
            while (r > R) {
                r--;
                unite(edges[r][0], edges[r][1]);
            }
            int photo = snapshot();
            while (l < L) {
                unite(edges[l][0], edges[l][1]);
                l++;
            }
            ans[I] = !is_bipartite;
            rollback(photo);
            l = b*K;
        }
    }

    for (int i = 0; i < q; i++) cout << (ans[i] ? "YES" : "NO") << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    solve();
    return 0;
}

Compilation message

Joker.cpp: In function 'void rollback(int)':
Joker.cpp:74:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<std::array<int, 2>&, std::array<int, 2> >, std::allocator<std::pair<std::array<int, 2>&, std::array<int, 2> > > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   74 |     while (undo_par.size() > until) {
      |            ~~~~~~~~~~~~~~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 2 ms 4440 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 2 ms 756 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 2 ms 4440 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 2 ms 756 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 30 ms 4444 KB Output is correct
30 Correct 54 ms 4696 KB Output is correct
31 Correct 63 ms 4728 KB Output is correct
32 Correct 61 ms 2648 KB Output is correct
33 Correct 61 ms 604 KB Output is correct
34 Correct 61 ms 604 KB Output is correct
35 Correct 60 ms 2648 KB Output is correct
36 Correct 62 ms 2652 KB Output is correct
37 Correct 60 ms 704 KB Output is correct
38 Correct 59 ms 688 KB Output is correct
39 Correct 58 ms 2652 KB Output is correct
40 Correct 61 ms 4736 KB Output is correct
41 Correct 62 ms 4700 KB Output is correct
42 Correct 59 ms 2648 KB Output is correct
43 Correct 61 ms 604 KB Output is correct
44 Correct 61 ms 600 KB Output is correct
45 Correct 62 ms 684 KB Output is correct
46 Correct 62 ms 600 KB Output is correct
47 Correct 59 ms 604 KB Output is correct
48 Correct 61 ms 2652 KB Output is correct
49 Correct 62 ms 4948 KB Output is correct
50 Correct 61 ms 2648 KB Output is correct
51 Correct 62 ms 2648 KB Output is correct
52 Correct 66 ms 600 KB Output is correct
53 Correct 60 ms 600 KB Output is correct
54 Correct 62 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 106 ms 26920 KB Output is correct
4 Correct 108 ms 30668 KB Output is correct
5 Correct 107 ms 34688 KB Output is correct
6 Correct 112 ms 31016 KB Output is correct
7 Correct 113 ms 31004 KB Output is correct
8 Correct 107 ms 30936 KB Output is correct
9 Correct 108 ms 30748 KB Output is correct
10 Correct 121 ms 30812 KB Output is correct
11 Correct 111 ms 31508 KB Output is correct
12 Correct 109 ms 31268 KB Output is correct
13 Correct 107 ms 31292 KB Output is correct
14 Correct 106 ms 30972 KB Output is correct
15 Correct 116 ms 31416 KB Output is correct
16 Correct 115 ms 31004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 2 ms 4440 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 2 ms 756 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 106 ms 26920 KB Output is correct
30 Correct 108 ms 30668 KB Output is correct
31 Correct 107 ms 34688 KB Output is correct
32 Correct 112 ms 31016 KB Output is correct
33 Correct 113 ms 31004 KB Output is correct
34 Correct 107 ms 30936 KB Output is correct
35 Correct 108 ms 30748 KB Output is correct
36 Correct 121 ms 30812 KB Output is correct
37 Correct 111 ms 31508 KB Output is correct
38 Correct 109 ms 31268 KB Output is correct
39 Correct 107 ms 31292 KB Output is correct
40 Correct 106 ms 30972 KB Output is correct
41 Correct 116 ms 31416 KB Output is correct
42 Correct 115 ms 31004 KB Output is correct
43 Correct 697 ms 34476 KB Output is correct
44 Correct 701 ms 31116 KB Output is correct
45 Correct 698 ms 33308 KB Output is correct
46 Correct 717 ms 31392 KB Output is correct
47 Correct 726 ms 34188 KB Output is correct
48 Correct 727 ms 31688 KB Output is correct
49 Correct 737 ms 31252 KB Output is correct
50 Correct 716 ms 31820 KB Output is correct
51 Correct 713 ms 31508 KB Output is correct
52 Correct 710 ms 31252 KB Output is correct
53 Correct 719 ms 31004 KB Output is correct
54 Correct 766 ms 31248 KB Output is correct
55 Correct 716 ms 31408 KB Output is correct
56 Correct 722 ms 31464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 2 ms 4440 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 2 ms 756 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 30 ms 4444 KB Output is correct
30 Correct 54 ms 4696 KB Output is correct
31 Correct 63 ms 4728 KB Output is correct
32 Correct 61 ms 2648 KB Output is correct
33 Correct 61 ms 604 KB Output is correct
34 Correct 61 ms 604 KB Output is correct
35 Correct 60 ms 2648 KB Output is correct
36 Correct 62 ms 2652 KB Output is correct
37 Correct 60 ms 704 KB Output is correct
38 Correct 59 ms 688 KB Output is correct
39 Correct 58 ms 2652 KB Output is correct
40 Correct 61 ms 4736 KB Output is correct
41 Correct 62 ms 4700 KB Output is correct
42 Correct 59 ms 2648 KB Output is correct
43 Correct 61 ms 604 KB Output is correct
44 Correct 61 ms 600 KB Output is correct
45 Correct 62 ms 684 KB Output is correct
46 Correct 62 ms 600 KB Output is correct
47 Correct 59 ms 604 KB Output is correct
48 Correct 61 ms 2652 KB Output is correct
49 Correct 62 ms 4948 KB Output is correct
50 Correct 61 ms 2648 KB Output is correct
51 Correct 62 ms 2648 KB Output is correct
52 Correct 66 ms 600 KB Output is correct
53 Correct 60 ms 600 KB Output is correct
54 Correct 62 ms 4700 KB Output is correct
55 Incorrect 636 ms 73580 KB Output isn't correct
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 2 ms 4440 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 2 ms 756 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 30 ms 4444 KB Output is correct
30 Correct 54 ms 4696 KB Output is correct
31 Correct 63 ms 4728 KB Output is correct
32 Correct 61 ms 2648 KB Output is correct
33 Correct 61 ms 604 KB Output is correct
34 Correct 61 ms 604 KB Output is correct
35 Correct 60 ms 2648 KB Output is correct
36 Correct 62 ms 2652 KB Output is correct
37 Correct 60 ms 704 KB Output is correct
38 Correct 59 ms 688 KB Output is correct
39 Correct 58 ms 2652 KB Output is correct
40 Correct 61 ms 4736 KB Output is correct
41 Correct 62 ms 4700 KB Output is correct
42 Correct 59 ms 2648 KB Output is correct
43 Correct 61 ms 604 KB Output is correct
44 Correct 61 ms 600 KB Output is correct
45 Correct 62 ms 684 KB Output is correct
46 Correct 62 ms 600 KB Output is correct
47 Correct 59 ms 604 KB Output is correct
48 Correct 61 ms 2652 KB Output is correct
49 Correct 62 ms 4948 KB Output is correct
50 Correct 61 ms 2648 KB Output is correct
51 Correct 62 ms 2648 KB Output is correct
52 Correct 66 ms 600 KB Output is correct
53 Correct 60 ms 600 KB Output is correct
54 Correct 62 ms 4700 KB Output is correct
55 Correct 106 ms 26920 KB Output is correct
56 Correct 108 ms 30668 KB Output is correct
57 Correct 107 ms 34688 KB Output is correct
58 Correct 112 ms 31016 KB Output is correct
59 Correct 113 ms 31004 KB Output is correct
60 Correct 107 ms 30936 KB Output is correct
61 Correct 108 ms 30748 KB Output is correct
62 Correct 121 ms 30812 KB Output is correct
63 Correct 111 ms 31508 KB Output is correct
64 Correct 109 ms 31268 KB Output is correct
65 Correct 107 ms 31292 KB Output is correct
66 Correct 106 ms 30972 KB Output is correct
67 Correct 116 ms 31416 KB Output is correct
68 Correct 115 ms 31004 KB Output is correct
69 Correct 697 ms 34476 KB Output is correct
70 Correct 701 ms 31116 KB Output is correct
71 Correct 698 ms 33308 KB Output is correct
72 Correct 717 ms 31392 KB Output is correct
73 Correct 726 ms 34188 KB Output is correct
74 Correct 727 ms 31688 KB Output is correct
75 Correct 737 ms 31252 KB Output is correct
76 Correct 716 ms 31820 KB Output is correct
77 Correct 713 ms 31508 KB Output is correct
78 Correct 710 ms 31252 KB Output is correct
79 Correct 719 ms 31004 KB Output is correct
80 Correct 766 ms 31248 KB Output is correct
81 Correct 716 ms 31408 KB Output is correct
82 Correct 722 ms 31464 KB Output is correct
83 Incorrect 636 ms 73580 KB Output isn't correct
84 Halted 0 ms 0 KB -