Submission #575564

# Submission time Handle Problem Language Result Execution time Memory
575564 2022-06-11T04:36:36 Z talant117408 Joker (BOI20_joker) C++17
100 / 100
316 ms 14380 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
#define pb                  push_back
#define mp                  make_pair
#define all(v)              (v).begin(),(v).end()
#define rall(v)             (v).rbegin(),(v).rend()
#define lb                  lower_bound
#define ub                  upper_bound
#define sz(v)               int((v).size())
#define do_not_disturb      ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                '\n'

const int N = 2e5+7;
struct query {
    int l, r;
    int index;
    query() {
        l = r = 0;
        index = -1;
    }
};
struct DSU {
    vector <int> link, saizu, color;
    int odd_cycles;
    vector <tuple <int, int, bool>> st;
    
    DSU() {}
    DSU(int n) {
        link.resize(n+1);
        saizu.resize(n+1);
        color.resize(n+1);
        odd_cycles = 0;
        for (int i = 1; i <= n; i++) {
            link[i] = i;
            saizu[i] = 1;
            color[i] = 0;
        }
    }
    int find(int n) {
        if (n == link[n]) return n;
        return find(link[n]);
    }
    int find_color(int n) {
        int x = color[n];
        while (n != link[n]) {
            n = link[n];
            x ^= color[n];
        }
        return x;
    } 
    void unite(int a, int b) {
        int u = a, v = b;
        a = find(a); b = find(b);
        st.emplace_back(a, b, find_color(u) == find_color(v));
        if (a == b) {
            if (find_color(u) == find_color(v)) {
                odd_cycles++;
            }
        }
        else {
            if (saizu[a] < saizu[b]) swap(a, b);
            if (find_color(u) == find_color(v)) color[b] ^= 1;
            color[b] ^= color[a];
            saizu[a] += saizu[b];
            link[b] = a;
        }
    }
    void rollback() {
        auto a = get<0>(st.back()), b = get<1>(st.back());
        auto odd = get<2>(st.back());
        st.pop_back();
        if (a == b) {
            if (odd) {
                odd_cycles--;
            }
        }
        else {
            if (saizu[a] < saizu[b]) swap(a, b);
            saizu[a] -= saizu[b];
            link[b] = b;
        }
    }
    void remove(int x) {
        while (x--) {
            assert(!st.empty());
            rollback();
        }
    }
};
int opt[N], from[N], to[N];
DSU dsu;

void dnc(int l1, int l2, int r1, int r2) {
    if (l1 > l2) return;
    int lmid = (l1+l2) >> 1;
    for (int i = l1; i < lmid; i++) {
        dsu.unite(from[i], to[i]);
    }
    
    opt[lmid] = r2;
    while (!dsu.odd_cycles && opt[lmid] >= r1) {
        dsu.unite(from[opt[lmid]], to[opt[lmid]]);
        opt[lmid]--;
    }
    dsu.remove(r2-opt[lmid]);
    
    dsu.unite(from[lmid], to[lmid]);
    dnc(lmid+1, l2, opt[lmid], r2);
    dsu.remove(lmid-l1+1);
    
    for (int i = opt[lmid]+1; i <= r2; i++) {
        dsu.unite(from[i], to[i]);
    }
    dnc(l1, lmid-1, r1, opt[lmid]);
    dsu.remove(r2-opt[lmid]);
}

void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    dsu = DSU(n);
    for (int i = 1; i <= m; i++) {
        cin >> from[i] >> to[i];
    }
    dnc(1, m, 1, m);
    while (q--) {
        int l, r;
        cin >> l >> r;
        if (r <= opt[l]) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
}

int main() {
    do_not_disturb
    
    int t = 1;
    //~ cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}
/*
6 8 8
1 3
1 5
1 6
2 5
2 6
3 4
3 5
5 6
1 3
1 7
1 5
1 1
1 6
1 4
1 2
1 8 

6 8 6
1 3
1 5
1 6
2 5
2 6
3 4
3 5
5 6
1 5
2 5
3 5
4 5
6 7
6 8

6 8 2
1 3
1 5
1 6
2 5
2 6
3 4
3 5
5 6
4 8
4 7

*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 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 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 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 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 324 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 328 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 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 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 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 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 324 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 328 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 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 2 ms 468 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 472 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
39 Correct 2 ms 400 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 2 ms 344 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 392 KB Output is correct
44 Correct 2 ms 344 KB Output is correct
45 Correct 2 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 2 ms 456 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 452 KB Output is correct
50 Correct 2 ms 400 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 2 ms 468 KB Output is correct
53 Correct 2 ms 464 KB Output is correct
54 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 170 ms 10868 KB Output is correct
4 Correct 258 ms 14060 KB Output is correct
5 Correct 137 ms 12036 KB Output is correct
6 Correct 177 ms 10864 KB Output is correct
7 Correct 193 ms 10976 KB Output is correct
8 Correct 191 ms 9940 KB Output is correct
9 Correct 213 ms 10388 KB Output is correct
10 Correct 279 ms 11908 KB Output is correct
11 Correct 204 ms 10832 KB Output is correct
12 Correct 240 ms 11888 KB Output is correct
13 Correct 194 ms 9756 KB Output is correct
14 Correct 193 ms 10412 KB Output is correct
15 Correct 276 ms 11412 KB Output is correct
16 Correct 273 ms 12216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 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 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 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 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 324 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 328 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 170 ms 10868 KB Output is correct
30 Correct 258 ms 14060 KB Output is correct
31 Correct 137 ms 12036 KB Output is correct
32 Correct 177 ms 10864 KB Output is correct
33 Correct 193 ms 10976 KB Output is correct
34 Correct 191 ms 9940 KB Output is correct
35 Correct 213 ms 10388 KB Output is correct
36 Correct 279 ms 11908 KB Output is correct
37 Correct 204 ms 10832 KB Output is correct
38 Correct 240 ms 11888 KB Output is correct
39 Correct 194 ms 9756 KB Output is correct
40 Correct 193 ms 10412 KB Output is correct
41 Correct 276 ms 11412 KB Output is correct
42 Correct 273 ms 12216 KB Output is correct
43 Correct 174 ms 11192 KB Output is correct
44 Correct 237 ms 14380 KB Output is correct
45 Correct 224 ms 12452 KB Output is correct
46 Correct 174 ms 11272 KB Output is correct
47 Correct 181 ms 11296 KB Output is correct
48 Correct 235 ms 10808 KB Output is correct
49 Correct 288 ms 12216 KB Output is correct
50 Correct 209 ms 10948 KB Output is correct
51 Correct 221 ms 11824 KB Output is correct
52 Correct 241 ms 12404 KB Output is correct
53 Correct 202 ms 10076 KB Output is correct
54 Correct 247 ms 10968 KB Output is correct
55 Correct 269 ms 11836 KB Output is correct
56 Correct 290 ms 12464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 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 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 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 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 324 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 328 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 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 2 ms 468 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 472 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
39 Correct 2 ms 400 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 2 ms 344 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 392 KB Output is correct
44 Correct 2 ms 344 KB Output is correct
45 Correct 2 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 2 ms 456 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 452 KB Output is correct
50 Correct 2 ms 400 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 2 ms 468 KB Output is correct
53 Correct 2 ms 464 KB Output is correct
54 Correct 2 ms 468 KB Output is correct
55 Correct 134 ms 8584 KB Output is correct
56 Correct 202 ms 12984 KB Output is correct
57 Correct 102 ms 9748 KB Output is correct
58 Correct 153 ms 8644 KB Output is correct
59 Correct 195 ms 8572 KB Output is correct
60 Correct 245 ms 9408 KB Output is correct
61 Correct 188 ms 8700 KB Output is correct
62 Correct 195 ms 9660 KB Output is correct
63 Correct 154 ms 7852 KB Output is correct
64 Correct 226 ms 8896 KB Output is correct
65 Correct 255 ms 9664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 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 328 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 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 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 324 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 328 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 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 2 ms 468 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 472 KB Output is correct
35 Correct 2 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
39 Correct 2 ms 400 KB Output is correct
40 Correct 2 ms 468 KB Output is correct
41 Correct 2 ms 344 KB Output is correct
42 Correct 2 ms 340 KB Output is correct
43 Correct 2 ms 392 KB Output is correct
44 Correct 2 ms 344 KB Output is correct
45 Correct 2 ms 468 KB Output is correct
46 Correct 2 ms 468 KB Output is correct
47 Correct 2 ms 456 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 2 ms 452 KB Output is correct
50 Correct 2 ms 400 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 2 ms 468 KB Output is correct
53 Correct 2 ms 464 KB Output is correct
54 Correct 2 ms 468 KB Output is correct
55 Correct 170 ms 10868 KB Output is correct
56 Correct 258 ms 14060 KB Output is correct
57 Correct 137 ms 12036 KB Output is correct
58 Correct 177 ms 10864 KB Output is correct
59 Correct 193 ms 10976 KB Output is correct
60 Correct 191 ms 9940 KB Output is correct
61 Correct 213 ms 10388 KB Output is correct
62 Correct 279 ms 11908 KB Output is correct
63 Correct 204 ms 10832 KB Output is correct
64 Correct 240 ms 11888 KB Output is correct
65 Correct 194 ms 9756 KB Output is correct
66 Correct 193 ms 10412 KB Output is correct
67 Correct 276 ms 11412 KB Output is correct
68 Correct 273 ms 12216 KB Output is correct
69 Correct 174 ms 11192 KB Output is correct
70 Correct 237 ms 14380 KB Output is correct
71 Correct 224 ms 12452 KB Output is correct
72 Correct 174 ms 11272 KB Output is correct
73 Correct 181 ms 11296 KB Output is correct
74 Correct 235 ms 10808 KB Output is correct
75 Correct 288 ms 12216 KB Output is correct
76 Correct 209 ms 10948 KB Output is correct
77 Correct 221 ms 11824 KB Output is correct
78 Correct 241 ms 12404 KB Output is correct
79 Correct 202 ms 10076 KB Output is correct
80 Correct 247 ms 10968 KB Output is correct
81 Correct 269 ms 11836 KB Output is correct
82 Correct 290 ms 12464 KB Output is correct
83 Correct 134 ms 8584 KB Output is correct
84 Correct 202 ms 12984 KB Output is correct
85 Correct 102 ms 9748 KB Output is correct
86 Correct 153 ms 8644 KB Output is correct
87 Correct 195 ms 8572 KB Output is correct
88 Correct 245 ms 9408 KB Output is correct
89 Correct 188 ms 8700 KB Output is correct
90 Correct 195 ms 9660 KB Output is correct
91 Correct 154 ms 7852 KB Output is correct
92 Correct 226 ms 8896 KB Output is correct
93 Correct 255 ms 9664 KB Output is correct
94 Correct 178 ms 11964 KB Output is correct
95 Correct 247 ms 13164 KB Output is correct
96 Correct 149 ms 12980 KB Output is correct
97 Correct 177 ms 11916 KB Output is correct
98 Correct 199 ms 12044 KB Output is correct
99 Correct 214 ms 11376 KB Output is correct
100 Correct 251 ms 13316 KB Output is correct
101 Correct 204 ms 11612 KB Output is correct
102 Correct 240 ms 12476 KB Output is correct
103 Correct 270 ms 13512 KB Output is correct
104 Correct 206 ms 11176 KB Output is correct
105 Correct 271 ms 12268 KB Output is correct
106 Correct 316 ms 13004 KB Output is correct
107 Correct 123 ms 13380 KB Output is correct
108 Correct 180 ms 11740 KB Output is correct
109 Correct 189 ms 11756 KB Output is correct
110 Correct 176 ms 11688 KB Output is correct
111 Correct 184 ms 11644 KB Output is correct
112 Correct 174 ms 11684 KB Output is correct
113 Correct 173 ms 11744 KB Output is correct
114 Correct 183 ms 11716 KB Output is correct
115 Correct 191 ms 11752 KB Output is correct
116 Correct 177 ms 11720 KB Output is correct