Submission #904829

# Submission time Handle Problem Language Result Execution time Memory
904829 2024-01-12T09:20:13 Z LOLOLO Joker (BOI20_joker) C++17
100 / 100
474 ms 38720 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 3e5 + 100;
int p[N], s[N], a[N], b[N], odd[N], g[N], lst[N];

vector < vector <int>> action;

int find(int n) {
    if (p[n] == 0)
        return n;

    return find(p[n]);
}

int get(int a) {
    if (p[a] == 0)
        return 0;

    return odd[a] ^ get(p[a]);
}

bool check(int a, int b) {
    return find(a) == find(b) && get(a) == get(b);
}

void upsz(int a, int b) {
    action.pb({0, a, s[a]});
    s[a] = b;
}

void uppar(int a, int b) {
    action.pb({1, a, p[a]});
    p[a] = b;
}

void upodd(int a, int b) {
    action.pb({2, a, odd[a]});
    odd[a] = b;
}

void unite(int a, int b) {
    int x = get(a), y = get(b);
    a = find(a);
    b = find(b);
    if (a == b)
        return;

    if (s[a] < s[b])
        swap(a, b);

    uppar(b, a);
    upodd(b, x ^ y ^ 1);

    if (s[a] == s[b])
        upsz(a, s[a] + 1);
}

void rollback(int x) {
    while (sz(action) > x) {
        auto t = action.back();
        action.pop_back();
        if (t[0] == 0) {
            s[t[1]] = t[2];
        }

        if (t[0] == 1) {
            p[t[1]] = t[2];
        }

        if (t[0] == 2) {
            odd[t[1]] = t[2];
        }
    }
}

void dnc(int l, int r, int l1, int r1) {
    if (l > r)
        return;

    if (l1 == r1) {
        for (int i = l; i <= r; i++)
            lst[i] = l1;
        return;
    }
    int tmp1 = sz(action);
    int m = (l + r) / 2;

    for (int i = l; i < m; i++) {
        unite(a[i], b[i]);
    }

    int tmp2 = sz(action);

    for (int i = r1; i >= max(m, l1); i--) {
        if (check(a[i], b[i])) {
            lst[m] = i;
            break;
        }

        unite(a[i], b[i]);
    }                              

    rollback(tmp2);
    if (m < r) {
        unite(a[m], b[m]);
        dnc(m + 1, r, max(m + 1, lst[m]), r1);
    }

    rollback(tmp1);
    if (l < m) {
        for (int i = r1; i > lst[m]; i--) {
            unite(a[i], b[i]);
        }

        dnc(l, m - 1, l1, lst[m]);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m, q;
    cin >> n >> m >> q;

    for (int i = 1; i <= m; i++) {
        cin >> a[i] >> b[i];
    }

    ll v = 1;
    for (int i = 1; i <= m; i++) {
        if (check(a[i], b[i])) {
            for (int j = i + 1; j <= m; j++) {
                lst[j] = m + 1;
            }

            v = i;
            break;
        }

        unite(a[i], b[i]);
    }

    rollback(0);
    if (v <= m)
        dnc(1, v, 1, m);

    while (q--) {
        int l, r;
        cin >> l >> r;

        if (r < lst[l]) {
            cout << "YES\n";
        } else {
            cout << "NO\n";
        }
    }
    return 0;
}
# 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 4440 KB Output is correct
5 Correct 1 ms 4444 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 4564 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4560 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 2 ms 4696 KB Output is correct
14 Correct 2 ms 4444 KB Output is correct
15 Correct 2 ms 4696 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 9 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 2 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4564 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 4440 KB Output is correct
5 Correct 1 ms 4444 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 4564 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4560 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 2 ms 4696 KB Output is correct
14 Correct 2 ms 4444 KB Output is correct
15 Correct 2 ms 4696 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 9 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 2 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4564 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 2 ms 4700 KB Output is correct
30 Correct 3 ms 4700 KB Output is correct
31 Correct 3 ms 4700 KB Output is correct
32 Correct 3 ms 4700 KB Output is correct
33 Correct 2 ms 4444 KB Output is correct
34 Correct 3 ms 4700 KB Output is correct
35 Correct 3 ms 4700 KB Output is correct
36 Correct 3 ms 4700 KB Output is correct
37 Correct 5 ms 4956 KB Output is correct
38 Correct 2 ms 4956 KB Output is correct
39 Correct 2 ms 4700 KB Output is correct
40 Correct 2 ms 4700 KB Output is correct
41 Correct 3 ms 4700 KB Output is correct
42 Correct 3 ms 4700 KB Output is correct
43 Correct 2 ms 4700 KB Output is correct
44 Correct 3 ms 4700 KB Output is correct
45 Correct 3 ms 4700 KB Output is correct
46 Correct 4 ms 4700 KB Output is correct
47 Correct 3 ms 4568 KB Output is correct
48 Correct 3 ms 4700 KB Output is correct
49 Correct 3 ms 4700 KB Output is correct
50 Correct 3 ms 4700 KB Output is correct
51 Correct 3 ms 4608 KB Output is correct
52 Correct 3 ms 4720 KB Output is correct
53 Correct 3 ms 4580 KB Output is correct
54 Correct 3 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 300 ms 20108 KB Output is correct
4 Correct 110 ms 34708 KB Output is correct
5 Correct 159 ms 34180 KB Output is correct
6 Correct 145 ms 24232 KB Output is correct
7 Correct 139 ms 24280 KB Output is correct
8 Correct 152 ms 18736 KB Output is correct
9 Correct 287 ms 22308 KB Output is correct
10 Correct 368 ms 26332 KB Output is correct
11 Correct 238 ms 23756 KB Output is correct
12 Correct 306 ms 26100 KB Output is correct
13 Correct 139 ms 13828 KB Output is correct
14 Correct 168 ms 20216 KB Output is correct
15 Correct 286 ms 24820 KB Output is correct
16 Correct 345 ms 26616 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 4440 KB Output is correct
5 Correct 1 ms 4444 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 4564 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4560 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 2 ms 4696 KB Output is correct
14 Correct 2 ms 4444 KB Output is correct
15 Correct 2 ms 4696 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 9 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 2 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4564 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 300 ms 20108 KB Output is correct
30 Correct 110 ms 34708 KB Output is correct
31 Correct 159 ms 34180 KB Output is correct
32 Correct 145 ms 24232 KB Output is correct
33 Correct 139 ms 24280 KB Output is correct
34 Correct 152 ms 18736 KB Output is correct
35 Correct 287 ms 22308 KB Output is correct
36 Correct 368 ms 26332 KB Output is correct
37 Correct 238 ms 23756 KB Output is correct
38 Correct 306 ms 26100 KB Output is correct
39 Correct 139 ms 13828 KB Output is correct
40 Correct 168 ms 20216 KB Output is correct
41 Correct 286 ms 24820 KB Output is correct
42 Correct 345 ms 26616 KB Output is correct
43 Correct 331 ms 23112 KB Output is correct
44 Correct 127 ms 35820 KB Output is correct
45 Correct 384 ms 38720 KB Output is correct
46 Correct 122 ms 24568 KB Output is correct
47 Correct 136 ms 24824 KB Output is correct
48 Correct 218 ms 23028 KB Output is correct
49 Correct 336 ms 32092 KB Output is correct
50 Correct 213 ms 21736 KB Output is correct
51 Correct 255 ms 24648 KB Output is correct
52 Correct 307 ms 27116 KB Output is correct
53 Correct 128 ms 14088 KB Output is correct
54 Correct 214 ms 21228 KB Output is correct
55 Correct 290 ms 25248 KB Output is correct
56 Correct 327 ms 31732 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 4440 KB Output is correct
5 Correct 1 ms 4444 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 4564 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4560 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 2 ms 4696 KB Output is correct
14 Correct 2 ms 4444 KB Output is correct
15 Correct 2 ms 4696 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 9 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 2 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4564 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 2 ms 4700 KB Output is correct
30 Correct 3 ms 4700 KB Output is correct
31 Correct 3 ms 4700 KB Output is correct
32 Correct 3 ms 4700 KB Output is correct
33 Correct 2 ms 4444 KB Output is correct
34 Correct 3 ms 4700 KB Output is correct
35 Correct 3 ms 4700 KB Output is correct
36 Correct 3 ms 4700 KB Output is correct
37 Correct 5 ms 4956 KB Output is correct
38 Correct 2 ms 4956 KB Output is correct
39 Correct 2 ms 4700 KB Output is correct
40 Correct 2 ms 4700 KB Output is correct
41 Correct 3 ms 4700 KB Output is correct
42 Correct 3 ms 4700 KB Output is correct
43 Correct 2 ms 4700 KB Output is correct
44 Correct 3 ms 4700 KB Output is correct
45 Correct 3 ms 4700 KB Output is correct
46 Correct 4 ms 4700 KB Output is correct
47 Correct 3 ms 4568 KB Output is correct
48 Correct 3 ms 4700 KB Output is correct
49 Correct 3 ms 4700 KB Output is correct
50 Correct 3 ms 4700 KB Output is correct
51 Correct 3 ms 4608 KB Output is correct
52 Correct 3 ms 4720 KB Output is correct
53 Correct 3 ms 4580 KB Output is correct
54 Correct 3 ms 4700 KB Output is correct
55 Correct 254 ms 20312 KB Output is correct
56 Correct 111 ms 33388 KB Output is correct
57 Correct 129 ms 30408 KB Output is correct
58 Correct 110 ms 21868 KB Output is correct
59 Correct 200 ms 20596 KB Output is correct
60 Correct 282 ms 23288 KB Output is correct
61 Correct 227 ms 20068 KB Output is correct
62 Correct 474 ms 23840 KB Output is correct
63 Correct 120 ms 15376 KB Output is correct
64 Correct 234 ms 22500 KB Output is correct
65 Correct 318 ms 23756 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 4440 KB Output is correct
5 Correct 1 ms 4444 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 4564 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4560 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 2 ms 4696 KB Output is correct
14 Correct 2 ms 4444 KB Output is correct
15 Correct 2 ms 4696 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 9 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 2 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4564 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 2 ms 4700 KB Output is correct
30 Correct 3 ms 4700 KB Output is correct
31 Correct 3 ms 4700 KB Output is correct
32 Correct 3 ms 4700 KB Output is correct
33 Correct 2 ms 4444 KB Output is correct
34 Correct 3 ms 4700 KB Output is correct
35 Correct 3 ms 4700 KB Output is correct
36 Correct 3 ms 4700 KB Output is correct
37 Correct 5 ms 4956 KB Output is correct
38 Correct 2 ms 4956 KB Output is correct
39 Correct 2 ms 4700 KB Output is correct
40 Correct 2 ms 4700 KB Output is correct
41 Correct 3 ms 4700 KB Output is correct
42 Correct 3 ms 4700 KB Output is correct
43 Correct 2 ms 4700 KB Output is correct
44 Correct 3 ms 4700 KB Output is correct
45 Correct 3 ms 4700 KB Output is correct
46 Correct 4 ms 4700 KB Output is correct
47 Correct 3 ms 4568 KB Output is correct
48 Correct 3 ms 4700 KB Output is correct
49 Correct 3 ms 4700 KB Output is correct
50 Correct 3 ms 4700 KB Output is correct
51 Correct 3 ms 4608 KB Output is correct
52 Correct 3 ms 4720 KB Output is correct
53 Correct 3 ms 4580 KB Output is correct
54 Correct 3 ms 4700 KB Output is correct
55 Correct 300 ms 20108 KB Output is correct
56 Correct 110 ms 34708 KB Output is correct
57 Correct 159 ms 34180 KB Output is correct
58 Correct 145 ms 24232 KB Output is correct
59 Correct 139 ms 24280 KB Output is correct
60 Correct 152 ms 18736 KB Output is correct
61 Correct 287 ms 22308 KB Output is correct
62 Correct 368 ms 26332 KB Output is correct
63 Correct 238 ms 23756 KB Output is correct
64 Correct 306 ms 26100 KB Output is correct
65 Correct 139 ms 13828 KB Output is correct
66 Correct 168 ms 20216 KB Output is correct
67 Correct 286 ms 24820 KB Output is correct
68 Correct 345 ms 26616 KB Output is correct
69 Correct 331 ms 23112 KB Output is correct
70 Correct 127 ms 35820 KB Output is correct
71 Correct 384 ms 38720 KB Output is correct
72 Correct 122 ms 24568 KB Output is correct
73 Correct 136 ms 24824 KB Output is correct
74 Correct 218 ms 23028 KB Output is correct
75 Correct 336 ms 32092 KB Output is correct
76 Correct 213 ms 21736 KB Output is correct
77 Correct 255 ms 24648 KB Output is correct
78 Correct 307 ms 27116 KB Output is correct
79 Correct 128 ms 14088 KB Output is correct
80 Correct 214 ms 21228 KB Output is correct
81 Correct 290 ms 25248 KB Output is correct
82 Correct 327 ms 31732 KB Output is correct
83 Correct 254 ms 20312 KB Output is correct
84 Correct 111 ms 33388 KB Output is correct
85 Correct 129 ms 30408 KB Output is correct
86 Correct 110 ms 21868 KB Output is correct
87 Correct 200 ms 20596 KB Output is correct
88 Correct 282 ms 23288 KB Output is correct
89 Correct 227 ms 20068 KB Output is correct
90 Correct 474 ms 23840 KB Output is correct
91 Correct 120 ms 15376 KB Output is correct
92 Correct 234 ms 22500 KB Output is correct
93 Correct 318 ms 23756 KB Output is correct
94 Correct 298 ms 23856 KB Output is correct
95 Correct 445 ms 36940 KB Output is correct
96 Correct 171 ms 33520 KB Output is correct
97 Correct 128 ms 25204 KB Output is correct
98 Correct 174 ms 25472 KB Output is correct
99 Correct 199 ms 20844 KB Output is correct
100 Correct 356 ms 28164 KB Output is correct
101 Correct 212 ms 23284 KB Output is correct
102 Correct 292 ms 26320 KB Output is correct
103 Correct 341 ms 30972 KB Output is correct
104 Correct 155 ms 18128 KB Output is correct
105 Correct 270 ms 24888 KB Output is correct
106 Correct 414 ms 27128 KB Output is correct
107 Correct 169 ms 37104 KB Output is correct
108 Correct 273 ms 24244 KB Output is correct
109 Correct 271 ms 23452 KB Output is correct
110 Correct 274 ms 23604 KB Output is correct
111 Correct 291 ms 25080 KB Output is correct
112 Correct 271 ms 24164 KB Output is correct
113 Correct 312 ms 24820 KB Output is correct
114 Correct 280 ms 23524 KB Output is correct
115 Correct 291 ms 23344 KB Output is correct
116 Correct 298 ms 23448 KB Output is correct