Submission #876209

# Submission time Handle Problem Language Result Execution time Memory
876209 2023-11-21T12:38:59 Z sleepntsheep Joker (BOI20_joker) C++17
100 / 100
151 ms 32692 KB
#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using i64 = long long;
using f80 = long double;
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false)

#define N 200005
#define ALL(x) x.begin(), x.end()

int n, m, q, e[N][2], r[N+1];

struct dsu
{
    vector<pair<int, int>> p;
    vector<int> rank, up;
    int cb, dumb;
    vector<tuple<int, int, pair<int, int>, int, int, int>> save;

    dsu(int n) : rank(n, 1), cb(1), up(n) { for (int i = 0; i < n; ++i) p.emplace_back(i, 0); }

    pair<int, int> find(int x)
    {
        if (p[x].first == x) return {x, 0};
        auto [A, B] = find(p[x].first);
        return {A, B ^ up[x]};
    }

    int unite(int x0, int y0)
    {
        int col { 1 };
        auto [x, A] { find(x0) };
        auto [y, B] { find(y0) };
        col ^= A ^ B;
        if (rank[x] > rank[y]) swap(x, y);
        save.emplace_back(x, y, p[x], cb, rank[x], rank[y]);
        if (x == y)
        {
            if (A == B) cb = 0;
            return 0;
        }
        if (rank[x] == rank[y]) ++rank[y];
        p[x] = {y, A^B^1};
        up[x] = col;
        return 1;
    }

    void rollback()
    {
        auto [x, y, px, cb0, rx, ry] = save.back(); save.pop_back();
        p[x] = px;
        rank[x] = rx, rank[y] = ry;
        cb = cb0;
    }
};

struct qdsu
{
    dsu ds;
    int bottom;
    struct upd { char type; int x, y; };
    vector<upd> S;

    qdsu(int n) : ds(n), bottom(0) {}

    void bottomify()
    {
        while (bottom < (int)S.size() && S[bottom].type == 'B') ++bottom;
    }

    void fix()
    {
        if (S.empty() || S.back().type == 'A') return;
        bottomify();
        deque<upd> s[2];
        for (int j = (int)S.size() - 1; (s[1].size() != s[0].size() || s[0].empty()) && j >= bottom; --j)
            s[S[j].type == 'A'].push_front(S[j]), ds.rollback(), S.pop_back();
        for (auto j : {0, 1}) for (auto x : s[j]) S.push_back(x), ds.unite(x.x, x.y);
        bottomify();
    }

    void new_phase()
    {
        for (auto i = S.size(); i--;) ds.rollback();
        for (auto i = S.size(); i--;) S[i].type = 'A', ds.unite(S[i].x, S[i].y);
        reverse(S.begin(), S.end());
        bottom = 0;
    }

    void pop()
    {
        bottomify();
        if (bottom == (int)S.size()) new_phase();
        fix();
        S.pop_back();
        ds.rollback();
    }

    int unite(int x, int y)
    {
        S.push_back(upd{'B', x, y});
        return ds.unite(x, y);
    }
};

int main()
{
    ShinLena;
    cin >> n >> m >> q;
    qdsu qd(n);
    for (int i = 0; i < m; ++i) cin >> e[i][0] >> e[i][1], qd.unite(--e[i][0], --e[i][1]) ;
    
    for (int i = 0, j = 0; i < m; ++i)
    {
        for (; !qd.ds.cb && j < m; ++j) qd.pop();
        if (qd.ds.cb) r[i] = j - 1;
        else r[i] = 1e9;
        qd.unite(e[i][0], e[i][1]);
    }

    for (int i = 0, x, y; i < q; ++i) cin >> x >> y, cout << (r[--x] <= --y ? "NO\n" : "YES\n");

    return 0;
}


Compilation message

Joker.cpp: In constructor 'dsu::dsu(int)':
Joker.cpp:26:9: warning: 'dsu::cb' will be initialized after [-Wreorder]
   26 |     int cb, dumb;
      |         ^~
Joker.cpp:25:23: warning:   'std::vector<int> dsu::up' [-Wreorder]
   25 |     vector<int> rank, up;
      |                       ^~
Joker.cpp:29:5: warning:   when initialized here [-Wreorder]
   29 |     dsu(int n) : rank(n, 1), cb(1), up(n) { for (int i = 0; i < n; ++i) p.emplace_back(i, 0); }
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2512 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2508 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2508 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2512 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2508 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2508 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2648 KB Output is correct
36 Correct 2 ms 2652 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 1 ms 2656 KB Output is correct
40 Correct 1 ms 2656 KB Output is correct
41 Correct 1 ms 2656 KB Output is correct
42 Correct 1 ms 2652 KB Output is correct
43 Correct 1 ms 2652 KB Output is correct
44 Correct 2 ms 2652 KB Output is correct
45 Correct 1 ms 2652 KB Output is correct
46 Correct 1 ms 2652 KB Output is correct
47 Correct 1 ms 2648 KB Output is correct
48 Correct 1 ms 2648 KB Output is correct
49 Correct 1 ms 2652 KB Output is correct
50 Correct 1 ms 2736 KB Output is correct
51 Correct 1 ms 2652 KB Output is correct
52 Correct 1 ms 2652 KB Output is correct
53 Correct 1 ms 2652 KB Output is correct
54 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 123 ms 16712 KB Output is correct
4 Correct 66 ms 29372 KB Output is correct
5 Correct 83 ms 17872 KB Output is correct
6 Correct 86 ms 19040 KB Output is correct
7 Correct 91 ms 19288 KB Output is correct
8 Correct 101 ms 17860 KB Output is correct
9 Correct 99 ms 17988 KB Output is correct
10 Correct 113 ms 21964 KB Output is correct
11 Correct 98 ms 19072 KB Output is correct
12 Correct 108 ms 21964 KB Output is correct
13 Correct 86 ms 16644 KB Output is correct
14 Correct 98 ms 17868 KB Output is correct
15 Correct 113 ms 21248 KB Output is correct
16 Correct 115 ms 21156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2512 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2508 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2508 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 123 ms 16712 KB Output is correct
30 Correct 66 ms 29372 KB Output is correct
31 Correct 83 ms 17872 KB Output is correct
32 Correct 86 ms 19040 KB Output is correct
33 Correct 91 ms 19288 KB Output is correct
34 Correct 101 ms 17860 KB Output is correct
35 Correct 99 ms 17988 KB Output is correct
36 Correct 113 ms 21964 KB Output is correct
37 Correct 98 ms 19072 KB Output is correct
38 Correct 108 ms 21964 KB Output is correct
39 Correct 86 ms 16644 KB Output is correct
40 Correct 98 ms 17868 KB Output is correct
41 Correct 113 ms 21248 KB Output is correct
42 Correct 115 ms 21156 KB Output is correct
43 Correct 126 ms 18300 KB Output is correct
44 Correct 71 ms 32692 KB Output is correct
45 Correct 101 ms 22212 KB Output is correct
46 Correct 89 ms 19488 KB Output is correct
47 Correct 96 ms 19000 KB Output is correct
48 Correct 112 ms 19476 KB Output is correct
49 Correct 116 ms 21820 KB Output is correct
50 Correct 98 ms 19508 KB Output is correct
51 Correct 107 ms 22344 KB Output is correct
52 Correct 107 ms 23176 KB Output is correct
53 Correct 96 ms 17536 KB Output is correct
54 Correct 106 ms 17996 KB Output is correct
55 Correct 116 ms 20692 KB Output is correct
56 Correct 115 ms 22396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2512 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2508 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2508 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2648 KB Output is correct
36 Correct 2 ms 2652 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 1 ms 2656 KB Output is correct
40 Correct 1 ms 2656 KB Output is correct
41 Correct 1 ms 2656 KB Output is correct
42 Correct 1 ms 2652 KB Output is correct
43 Correct 1 ms 2652 KB Output is correct
44 Correct 2 ms 2652 KB Output is correct
45 Correct 1 ms 2652 KB Output is correct
46 Correct 1 ms 2652 KB Output is correct
47 Correct 1 ms 2648 KB Output is correct
48 Correct 1 ms 2648 KB Output is correct
49 Correct 1 ms 2652 KB Output is correct
50 Correct 1 ms 2736 KB Output is correct
51 Correct 1 ms 2652 KB Output is correct
52 Correct 1 ms 2652 KB Output is correct
53 Correct 1 ms 2652 KB Output is correct
54 Correct 1 ms 2652 KB Output is correct
55 Correct 98 ms 17896 KB Output is correct
56 Correct 48 ms 31420 KB Output is correct
57 Correct 55 ms 20416 KB Output is correct
58 Correct 61 ms 17980 KB Output is correct
59 Correct 83 ms 18104 KB Output is correct
60 Correct 97 ms 20676 KB Output is correct
61 Correct 72 ms 16236 KB Output is correct
62 Correct 80 ms 20160 KB Output is correct
63 Correct 71 ms 15228 KB Output is correct
64 Correct 93 ms 17732 KB Output is correct
65 Correct 103 ms 21200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2512 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2508 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2508 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2648 KB Output is correct
36 Correct 2 ms 2652 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 1 ms 2656 KB Output is correct
40 Correct 1 ms 2656 KB Output is correct
41 Correct 1 ms 2656 KB Output is correct
42 Correct 1 ms 2652 KB Output is correct
43 Correct 1 ms 2652 KB Output is correct
44 Correct 2 ms 2652 KB Output is correct
45 Correct 1 ms 2652 KB Output is correct
46 Correct 1 ms 2652 KB Output is correct
47 Correct 1 ms 2648 KB Output is correct
48 Correct 1 ms 2648 KB Output is correct
49 Correct 1 ms 2652 KB Output is correct
50 Correct 1 ms 2736 KB Output is correct
51 Correct 1 ms 2652 KB Output is correct
52 Correct 1 ms 2652 KB Output is correct
53 Correct 1 ms 2652 KB Output is correct
54 Correct 1 ms 2652 KB Output is correct
55 Correct 123 ms 16712 KB Output is correct
56 Correct 66 ms 29372 KB Output is correct
57 Correct 83 ms 17872 KB Output is correct
58 Correct 86 ms 19040 KB Output is correct
59 Correct 91 ms 19288 KB Output is correct
60 Correct 101 ms 17860 KB Output is correct
61 Correct 99 ms 17988 KB Output is correct
62 Correct 113 ms 21964 KB Output is correct
63 Correct 98 ms 19072 KB Output is correct
64 Correct 108 ms 21964 KB Output is correct
65 Correct 86 ms 16644 KB Output is correct
66 Correct 98 ms 17868 KB Output is correct
67 Correct 113 ms 21248 KB Output is correct
68 Correct 115 ms 21156 KB Output is correct
69 Correct 126 ms 18300 KB Output is correct
70 Correct 71 ms 32692 KB Output is correct
71 Correct 101 ms 22212 KB Output is correct
72 Correct 89 ms 19488 KB Output is correct
73 Correct 96 ms 19000 KB Output is correct
74 Correct 112 ms 19476 KB Output is correct
75 Correct 116 ms 21820 KB Output is correct
76 Correct 98 ms 19508 KB Output is correct
77 Correct 107 ms 22344 KB Output is correct
78 Correct 107 ms 23176 KB Output is correct
79 Correct 96 ms 17536 KB Output is correct
80 Correct 106 ms 17996 KB Output is correct
81 Correct 116 ms 20692 KB Output is correct
82 Correct 115 ms 22396 KB Output is correct
83 Correct 98 ms 17896 KB Output is correct
84 Correct 48 ms 31420 KB Output is correct
85 Correct 55 ms 20416 KB Output is correct
86 Correct 61 ms 17980 KB Output is correct
87 Correct 83 ms 18104 KB Output is correct
88 Correct 97 ms 20676 KB Output is correct
89 Correct 72 ms 16236 KB Output is correct
90 Correct 80 ms 20160 KB Output is correct
91 Correct 71 ms 15228 KB Output is correct
92 Correct 93 ms 17732 KB Output is correct
93 Correct 103 ms 21200 KB Output is correct
94 Correct 129 ms 19628 KB Output is correct
95 Correct 144 ms 23532 KB Output is correct
96 Correct 87 ms 23488 KB Output is correct
97 Correct 91 ms 20508 KB Output is correct
98 Correct 97 ms 19784 KB Output is correct
99 Correct 105 ms 18800 KB Output is correct
100 Correct 107 ms 23904 KB Output is correct
101 Correct 110 ms 19520 KB Output is correct
102 Correct 126 ms 22980 KB Output is correct
103 Correct 126 ms 22976 KB Output is correct
104 Correct 105 ms 18392 KB Output is correct
105 Correct 119 ms 20696 KB Output is correct
106 Correct 131 ms 23164 KB Output is correct
107 Correct 90 ms 23492 KB Output is correct
108 Correct 129 ms 19248 KB Output is correct
109 Correct 133 ms 19764 KB Output is correct
110 Correct 127 ms 20056 KB Output is correct
111 Correct 142 ms 19448 KB Output is correct
112 Correct 151 ms 18852 KB Output is correct
113 Correct 134 ms 19644 KB Output is correct
114 Correct 137 ms 19924 KB Output is correct
115 Correct 137 ms 18912 KB Output is correct
116 Correct 130 ms 19536 KB Output is correct