Submission #634867

# Submission time Handle Problem Language Result Execution time Memory
634867 2022-08-25T07:28:00 Z Do_you_copy Joker (BOI20_joker) C++17
100 / 100
241 ms 13004 KB
#include <bits/stdc++.h>
//#define int long long
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());

ll min(const ll &a, const ll &b){
    return (a < b) ? a : b;
}

ll max(const ll &a, const ll &b){
    return (a > b) ? a : b;
}

//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 3e5 + 1;
int n, m, q;
int cnt, cnt1, cnt2;
bool color[maxN];

struct TDSU{
    vector <pii> save;
    vector <int> parity;
    vector <int> lab;
    TDSU(){}
    TDSU(int _n){
        lab.resize(_n, -1);
    }
    inline void resize(int _n){
        lab.resize(_n, -1);
    }
    inline int find_set(int u){
        if (lab[u] < 0) return u;
        return lab[u] = find_set(lab[u]);
    }
    inline int find(int u){
        cnt ^= color[u];
        if (lab[u] < 0) return u;
        return find(lab[u]);
    }
    inline void merge(int u, int v){
        if (lab[u] > lab[v]) swap(u, v);
        save.pb({v, lab[v]});
        lab[u] += lab[v];
        lab[v] = u;
        parity.pb(color[v]);
        color[v] = cnt1 ^ cnt2 ^ 1;
    }
    inline void undo(){
        int v = save.back().fi;
        int u = lab[v];
        lab[v] = save.back().se;
        lab[u] -= lab[v];
        color[v] = parity.back();
        parity.pop_back();
        save.pop_back();
    }
    inline void clear(){
        lab.clear();
        save.clear();
    }
    inline void reset(int _n){
        clear();
        resize(_n);
    }
    inline void roll_back(int _n){
        while (save.size() > _n) undo();
    }
};

TDSU dsu;
pii e[maxN];

inline bool Unite(int u, int v){
    if (u == 0) return 0;
    cnt = 0; cnt1 = 0, cnt2 = 0;
    int x = dsu.find(u);
    cnt1 = cnt; cnt = 0;
    int y = dsu.find(v);
    cnt2 = cnt; cnt = 0;
    if (x == y){
        if (cnt1 == cnt2){
            return 1;
        }
        return 0;
    }
    dsu.merge(x, y);
    return 0;
}
int last[maxN];

void DnC(int l, int r, int from, int to){
    if (l > r) return;
    int mid = (l + r) / 2;
    int sz = dsu.save.size();
    bool flag = 0;
    for (int i = l; i <= mid; ++i){
        if (Unite(e[i].fi, e[i].se)){
            flag = 1;
            break;
        }
    }
    if (!flag){
        int sz1 = dsu.save.size();
        for (int i = to; i >= max(mid + 1, from); --i){
            if (Unite(e[i].fi, e[i].se)){
                last[mid] = i;
                break;
            }
        }
        if (last[mid] == m + 1) last[mid] = mid;
        dsu.roll_back(sz1);
        DnC(mid + 1, r, last[mid], to);
    }
    dsu.roll_back(sz);
    for (int i = to; i > last[mid]; --i){
        Unite(e[i].fi, e[i].se);
    }
    DnC(l, mid - 1, from, last[mid]);
    dsu.roll_back(sz);
}

void Init(){
    cin >> n >> m >> q;
    fill(last + 1, last + m + 1, m + 1);
    for (int i = 1; i <= m; ++i){
        cin >> e[i].fi >> e[i].se;
    }
    dsu.resize(n + 1);
    DnC(0, m, 1, m);
    for (int i = 1; i <= q; ++i){
        int x, y; cin >> x >> y;
        --x; ++y;
        if (last[x] >= y) cout << "YES\n";
        else cout << "NO\n";
    }
}

#define debu
#define taskname "test"
signed main(){
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        #ifdef debug
        freopen(taskname".out", "w", stdout);
        #endif
    }
    faster;
    ll tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
    if (fopen("timeout.txt", "r")){
        ofstream timeout("timeout.txt");
        timeout << 1000 * double(clock()) / CLOCKS_PER_SEC;
        #ifndef debug
        cerr << "\nTime elapsed: " << 1000 * double(clock()) / CLOCKS_PER_SEC << "ms\n";
        #endif
    }
}

Compilation message

Joker.cpp: In member function 'void TDSU::roll_back(int)':
Joker.cpp:79:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   79 |         while (save.size() > _n) undo();
      |                ~~~~~~~~~~~~^~~~
Joker.cpp: In function 'int main()':
Joker.cpp:155:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  155 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 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 356 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 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 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 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 356 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 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 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 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 2 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 2 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 133 ms 7988 KB Output is correct
4 Correct 236 ms 11224 KB Output is correct
5 Correct 124 ms 11572 KB Output is correct
6 Correct 114 ms 9512 KB Output is correct
7 Correct 116 ms 9472 KB Output is correct
8 Correct 118 ms 7960 KB Output is correct
9 Correct 163 ms 8772 KB Output is correct
10 Correct 179 ms 9724 KB Output is correct
11 Correct 141 ms 9204 KB Output is correct
12 Correct 171 ms 10012 KB Output is correct
13 Correct 129 ms 7624 KB Output is correct
14 Correct 131 ms 8500 KB Output is correct
15 Correct 182 ms 9620 KB Output is correct
16 Correct 195 ms 10124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 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 356 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 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 133 ms 7988 KB Output is correct
30 Correct 236 ms 11224 KB Output is correct
31 Correct 124 ms 11572 KB Output is correct
32 Correct 114 ms 9512 KB Output is correct
33 Correct 116 ms 9472 KB Output is correct
34 Correct 118 ms 7960 KB Output is correct
35 Correct 163 ms 8772 KB Output is correct
36 Correct 179 ms 9724 KB Output is correct
37 Correct 141 ms 9204 KB Output is correct
38 Correct 171 ms 10012 KB Output is correct
39 Correct 129 ms 7624 KB Output is correct
40 Correct 131 ms 8500 KB Output is correct
41 Correct 182 ms 9620 KB Output is correct
42 Correct 195 ms 10124 KB Output is correct
43 Correct 145 ms 9720 KB Output is correct
44 Correct 241 ms 11652 KB Output is correct
45 Correct 216 ms 12032 KB Output is correct
46 Correct 113 ms 9840 KB Output is correct
47 Correct 117 ms 9804 KB Output is correct
48 Correct 156 ms 9276 KB Output is correct
49 Correct 226 ms 10164 KB Output is correct
50 Correct 153 ms 9308 KB Output is correct
51 Correct 174 ms 10048 KB Output is correct
52 Correct 224 ms 10408 KB Output is correct
53 Correct 170 ms 7888 KB Output is correct
54 Correct 168 ms 9328 KB Output is correct
55 Correct 210 ms 9992 KB Output is correct
56 Correct 225 ms 10436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 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 356 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 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 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 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 2 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 2 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 2 ms 340 KB Output is correct
55 Correct 109 ms 5620 KB Output is correct
56 Correct 217 ms 9600 KB Output is correct
57 Correct 101 ms 9236 KB Output is correct
58 Correct 97 ms 7132 KB Output is correct
59 Correct 131 ms 7028 KB Output is correct
60 Correct 179 ms 7644 KB Output is correct
61 Correct 133 ms 7120 KB Output is correct
62 Correct 131 ms 7892 KB Output is correct
63 Correct 93 ms 5884 KB Output is correct
64 Correct 138 ms 7272 KB Output is correct
65 Correct 166 ms 7800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 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 356 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 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 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 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 2 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 2 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 2 ms 340 KB Output is correct
55 Correct 133 ms 7988 KB Output is correct
56 Correct 236 ms 11224 KB Output is correct
57 Correct 124 ms 11572 KB Output is correct
58 Correct 114 ms 9512 KB Output is correct
59 Correct 116 ms 9472 KB Output is correct
60 Correct 118 ms 7960 KB Output is correct
61 Correct 163 ms 8772 KB Output is correct
62 Correct 179 ms 9724 KB Output is correct
63 Correct 141 ms 9204 KB Output is correct
64 Correct 171 ms 10012 KB Output is correct
65 Correct 129 ms 7624 KB Output is correct
66 Correct 131 ms 8500 KB Output is correct
67 Correct 182 ms 9620 KB Output is correct
68 Correct 195 ms 10124 KB Output is correct
69 Correct 145 ms 9720 KB Output is correct
70 Correct 241 ms 11652 KB Output is correct
71 Correct 216 ms 12032 KB Output is correct
72 Correct 113 ms 9840 KB Output is correct
73 Correct 117 ms 9804 KB Output is correct
74 Correct 156 ms 9276 KB Output is correct
75 Correct 226 ms 10164 KB Output is correct
76 Correct 153 ms 9308 KB Output is correct
77 Correct 174 ms 10048 KB Output is correct
78 Correct 224 ms 10408 KB Output is correct
79 Correct 170 ms 7888 KB Output is correct
80 Correct 168 ms 9328 KB Output is correct
81 Correct 210 ms 9992 KB Output is correct
82 Correct 225 ms 10436 KB Output is correct
83 Correct 109 ms 5620 KB Output is correct
84 Correct 217 ms 9600 KB Output is correct
85 Correct 101 ms 9236 KB Output is correct
86 Correct 97 ms 7132 KB Output is correct
87 Correct 131 ms 7028 KB Output is correct
88 Correct 179 ms 7644 KB Output is correct
89 Correct 133 ms 7120 KB Output is correct
90 Correct 131 ms 7892 KB Output is correct
91 Correct 93 ms 5884 KB Output is correct
92 Correct 138 ms 7272 KB Output is correct
93 Correct 166 ms 7800 KB Output is correct
94 Correct 146 ms 10464 KB Output is correct
95 Correct 196 ms 12660 KB Output is correct
96 Correct 136 ms 12464 KB Output is correct
97 Correct 118 ms 10516 KB Output is correct
98 Correct 125 ms 10488 KB Output is correct
99 Correct 139 ms 9516 KB Output is correct
100 Correct 172 ms 11276 KB Output is correct
101 Correct 141 ms 10044 KB Output is correct
102 Correct 167 ms 10732 KB Output is correct
103 Correct 179 ms 11340 KB Output is correct
104 Correct 138 ms 9160 KB Output is correct
105 Correct 176 ms 10536 KB Output is correct
106 Correct 223 ms 11076 KB Output is correct
107 Correct 127 ms 13004 KB Output is correct
108 Correct 147 ms 10260 KB Output is correct
109 Correct 140 ms 10256 KB Output is correct
110 Correct 137 ms 10232 KB Output is correct
111 Correct 140 ms 10228 KB Output is correct
112 Correct 139 ms 10232 KB Output is correct
113 Correct 138 ms 10236 KB Output is correct
114 Correct 134 ms 10308 KB Output is correct
115 Correct 140 ms 10268 KB Output is correct
116 Correct 138 ms 10180 KB Output is correct