Submission #676808

# Submission time Handle Problem Language Result Execution time Memory
676808 2023-01-01T08:37:55 Z dooompy Joker (BOI20_joker) C++17
100 / 100
186 ms 14224 KB
#include "bits/stdc++.h"

using namespace std;

void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}

#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif

using ll = long long;

pair<int, int> roads[200005];


struct dsu {
    vector<pair<int, int>> par;
    vector<int> rnk;
    vector<array<int, 4>> history;

    void init(int n) {
        par.resize(n + 1); rnk.resize(n + 1);
        for (int i = 0; i <= n; i++) {
            par[i] = {i, 0}, rnk[i] = 1;
        }
    }

    pair<int, int> findset(int i) {
        if (i == par[i].first) return par[i];
        int parity = par[i].second;
        pair<int, int> temp =  findset(par[i].first);
        temp.second ^= parity;

        return temp;
    }

    bool onion(int a, int b) {
        auto tempa = findset(a), tempb = findset(b);

        if (tempa.first == tempb.first) {
            if (tempa.second == tempb.second) return false;

            return true;
        }

        a = tempa.first, b = tempb.first;

        if (rnk[a] > rnk[b]) swap(a, b);
        history.push_back({a, rnk[a], b, rnk[b]});

        par[a] = {b, tempa.second^tempb.second^1};

        if (rnk[a] == rnk[b]) rnk[b]++;
        return true;
    }

    void rollback(int idx) {
        while (history.size() > idx) {
            auto [a, rnka, b, rnkb] = history.back();
            history.pop_back();

            rnk[a] = rnka, rnk[b] = rnkb;

            par[a] = {a, 0};
        }
    }
};

dsu ds;
int n, m, q;
int lst[200005];

void dnc(int l, int r, int ql, int qr) {
    if (r < l) return;

    int mid = (l + r) / 2;

    int res = -1;

    int b1 = ds.history.size();
    for (int i = l; i < mid; i++) {
        int id = ds.onion(roads[i].first, roads[i].second);

        if (!id) {
            res = qr;
            break;
        }
    }

    int b2 = ds.history.size();
    if (res == -1) {
        for (int i = qr; i > ql; i--) {
            if (i == m + 1) continue;
            if (!ds.onion(roads[i].first, roads[i].second)) {
                res = i; break;
            }
        }

        if (res == -1) res = ql;
    }

    lst[mid] = res;

    if (res == m + 1) {
        for (int i = mid + 1; i <= r; i++) lst[i] = m + 1;
    } else {
        ds.rollback(b2);
        if (!ds.onion(roads[mid].first, roads[mid].second)) {
//            cout << "REACHED" << endl;
            for (int i = mid + 1; i <= r; i++) lst[i] = m + 1;
        } else {
            dnc(mid + 1, r, lst[mid], qr);
        }
    }

    ds.rollback(b1);
    for (int i = lst[mid] + 1; i <= qr; i++) {
        ds.onion(roads[i].first, roads[i].second);
    }

    dnc(l, mid - 1, ql, lst[mid]);
    ds.rollback(b1);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("", "r", stdin);
//    freopen("", "w", stdout);
    cin >> n >> m >> q;
    for (int i = 1; i <= m; i++) {
        int a, b; cin >> a >> b;
        roads[i] = {a, b};
    }

    ds.init(n);

    dnc(1, m, 1, m + 1);

    for (int i = 0; i < q; i++) {
        int l, r; cin >> l >> r;

        if (lst[l] <= r) cout << "NO\n";
        else cout << "YES\n";
    }
}

Compilation message

Joker.cpp: In member function 'void dsu::rollback(int)':
Joker.cpp:77:31: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   77 |         while (history.size() > idx) {
      |                ~~~~~~~~~~~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 336 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 336 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 1 ms 372 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 472 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 476 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 2 ms 372 KB Output is correct
41 Correct 2 ms 348 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 352 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 2 ms 340 KB Output is correct
48 Correct 2 ms 348 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 1 ms 376 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 122 ms 6128 KB Output is correct
4 Correct 167 ms 8844 KB Output is correct
5 Correct 116 ms 8280 KB Output is correct
6 Correct 100 ms 6140 KB Output is correct
7 Correct 104 ms 6132 KB Output is correct
8 Correct 113 ms 5192 KB Output is correct
9 Correct 128 ms 6000 KB Output is correct
10 Correct 167 ms 7548 KB Output is correct
11 Correct 141 ms 5844 KB Output is correct
12 Correct 155 ms 7108 KB Output is correct
13 Correct 116 ms 3984 KB Output is correct
14 Correct 117 ms 5060 KB Output is correct
15 Correct 164 ms 6556 KB Output is correct
16 Correct 176 ms 7280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 336 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 122 ms 6128 KB Output is correct
30 Correct 167 ms 8844 KB Output is correct
31 Correct 116 ms 8280 KB Output is correct
32 Correct 100 ms 6140 KB Output is correct
33 Correct 104 ms 6132 KB Output is correct
34 Correct 113 ms 5192 KB Output is correct
35 Correct 128 ms 6000 KB Output is correct
36 Correct 167 ms 7548 KB Output is correct
37 Correct 141 ms 5844 KB Output is correct
38 Correct 155 ms 7108 KB Output is correct
39 Correct 116 ms 3984 KB Output is correct
40 Correct 117 ms 5060 KB Output is correct
41 Correct 164 ms 6556 KB Output is correct
42 Correct 176 ms 7280 KB Output is correct
43 Correct 126 ms 10428 KB Output is correct
44 Correct 167 ms 12944 KB Output is correct
45 Correct 186 ms 13240 KB Output is correct
46 Correct 102 ms 10528 KB Output is correct
47 Correct 101 ms 10488 KB Output is correct
48 Correct 154 ms 9848 KB Output is correct
49 Correct 186 ms 11520 KB Output is correct
50 Correct 131 ms 9724 KB Output is correct
51 Correct 158 ms 10972 KB Output is correct
52 Correct 155 ms 11756 KB Output is correct
53 Correct 130 ms 8144 KB Output is correct
54 Correct 146 ms 9732 KB Output is correct
55 Correct 156 ms 10916 KB Output is correct
56 Correct 178 ms 11780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 336 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 1 ms 372 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 472 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 476 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 2 ms 372 KB Output is correct
41 Correct 2 ms 348 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 352 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 2 ms 340 KB Output is correct
48 Correct 2 ms 348 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 1 ms 376 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 1 ms 384 KB Output is correct
55 Correct 96 ms 7908 KB Output is correct
56 Correct 134 ms 11028 KB Output is correct
57 Correct 82 ms 10680 KB Output is correct
58 Correct 72 ms 7792 KB Output is correct
59 Correct 122 ms 7584 KB Output is correct
60 Correct 158 ms 8668 KB Output is correct
61 Correct 114 ms 7704 KB Output is correct
62 Correct 116 ms 9032 KB Output is correct
63 Correct 93 ms 6244 KB Output is correct
64 Correct 126 ms 8000 KB Output is correct
65 Correct 146 ms 9060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 336 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 336 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 1 ms 372 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 472 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 476 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 2 ms 372 KB Output is correct
41 Correct 2 ms 348 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 352 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 2 ms 340 KB Output is correct
48 Correct 2 ms 348 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 2 ms 468 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 1 ms 376 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 1 ms 384 KB Output is correct
55 Correct 122 ms 6128 KB Output is correct
56 Correct 167 ms 8844 KB Output is correct
57 Correct 116 ms 8280 KB Output is correct
58 Correct 100 ms 6140 KB Output is correct
59 Correct 104 ms 6132 KB Output is correct
60 Correct 113 ms 5192 KB Output is correct
61 Correct 128 ms 6000 KB Output is correct
62 Correct 167 ms 7548 KB Output is correct
63 Correct 141 ms 5844 KB Output is correct
64 Correct 155 ms 7108 KB Output is correct
65 Correct 116 ms 3984 KB Output is correct
66 Correct 117 ms 5060 KB Output is correct
67 Correct 164 ms 6556 KB Output is correct
68 Correct 176 ms 7280 KB Output is correct
69 Correct 126 ms 10428 KB Output is correct
70 Correct 167 ms 12944 KB Output is correct
71 Correct 186 ms 13240 KB Output is correct
72 Correct 102 ms 10528 KB Output is correct
73 Correct 101 ms 10488 KB Output is correct
74 Correct 154 ms 9848 KB Output is correct
75 Correct 186 ms 11520 KB Output is correct
76 Correct 131 ms 9724 KB Output is correct
77 Correct 158 ms 10972 KB Output is correct
78 Correct 155 ms 11756 KB Output is correct
79 Correct 130 ms 8144 KB Output is correct
80 Correct 146 ms 9732 KB Output is correct
81 Correct 156 ms 10916 KB Output is correct
82 Correct 178 ms 11780 KB Output is correct
83 Correct 96 ms 7908 KB Output is correct
84 Correct 134 ms 11028 KB Output is correct
85 Correct 82 ms 10680 KB Output is correct
86 Correct 72 ms 7792 KB Output is correct
87 Correct 122 ms 7584 KB Output is correct
88 Correct 158 ms 8668 KB Output is correct
89 Correct 114 ms 7704 KB Output is correct
90 Correct 116 ms 9032 KB Output is correct
91 Correct 93 ms 6244 KB Output is correct
92 Correct 126 ms 8000 KB Output is correct
93 Correct 146 ms 9060 KB Output is correct
94 Correct 134 ms 11176 KB Output is correct
95 Correct 177 ms 13964 KB Output is correct
96 Correct 124 ms 13624 KB Output is correct
97 Correct 108 ms 11212 KB Output is correct
98 Correct 118 ms 11264 KB Output is correct
99 Correct 135 ms 9940 KB Output is correct
100 Correct 168 ms 12612 KB Output is correct
101 Correct 137 ms 10552 KB Output is correct
102 Correct 167 ms 11644 KB Output is correct
103 Correct 176 ms 12760 KB Output is correct
104 Correct 129 ms 9416 KB Output is correct
105 Correct 165 ms 11368 KB Output is correct
106 Correct 181 ms 12356 KB Output is correct
107 Correct 112 ms 14224 KB Output is correct
108 Correct 135 ms 10948 KB Output is correct
109 Correct 138 ms 10948 KB Output is correct
110 Correct 134 ms 10952 KB Output is correct
111 Correct 137 ms 10904 KB Output is correct
112 Correct 140 ms 10948 KB Output is correct
113 Correct 129 ms 10964 KB Output is correct
114 Correct 133 ms 10948 KB Output is correct
115 Correct 144 ms 10976 KB Output is correct
116 Correct 131 ms 10856 KB Output is correct