Submission #750747

# Submission time Handle Problem Language Result Execution time Memory
750747 2023-05-30T08:51:57 Z GrindMachine Joker (BOI20_joker) C++17
100 / 100
506 ms 28160 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "YES" << endl
#define no cout << "NO" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
edi
some submissions

*/

const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

struct DSU {
    vector<int> par, rankk;
    vector<int> weight;
    bool is_bip;
    vector<array<int,5>> history;

    DSU() {

    }

    DSU(int n) {
        init(n);
    }

    void init(int n) {
        par = vector<int>(n + 1);
        rankk = vector<int>(n + 1);
        weight = vector<int>(n + 1);
        is_bip = true;
        rep(i, n + 1) create(i);
    }

    void create(int u) {
        par[u] = u;
        rankk[u] = 0;
        weight[u] = 0;
    }

    pii find(int u) {
        if (u == par[u]) return {u,0};
        auto [p, cp] = find(par[u]);
        return {p, cp ^ weight[u]};        
    }

    bool same(int u, int v) {
        return find(u).ff == find(v).ff;
    }

    void merge(int u, int v) {
        auto [pu,cu] = find(u);
        auto [pv,cv] = find(v);

        history.pb({pu, par[pu], rankk[pu], weight[pu], is_bip});
        history.pb({pv, par[pv], rankk[pv], weight[pv], is_bip});

        if(pu == pv){
            if(cu == cv){
                is_bip = false;
            }

            return;
        }

        int w = cu ^ cv ^ 1;
        u = pu, v = pv;

        if (rankk[u] == rankk[v]) rankk[u]++;
        if (rankk[u] < rankk[v]) swap(u, v);

        par[v] = u;
        weight[v] = w;
    }

    void rollback(int times){
        times *= 2;
        rep(t,times){
            auto [u,pu,rnk,w,bip] = history.back();
            history.pop_back();

            par[u] = pu;
            rankk[u] = rnk;
            weight[u] = w;
            is_bip = bip;
        }
    }
};

vector<pii> edges(N);
DSU dsu(N);
vector<int> best(N);

void go(int l1, int r1, int l2, int r2){
    if(l1 > r1) return;

    int mid = (l1 + r1) >> 1;
    int opt_mid = r2;
    int cnt = 0;

    for(int i = l1; i < mid; ++i){
        auto [u,v] = edges[i];
        dsu.merge(u,v);
        cnt++;
    }

    rev(i,r2,l2){
        auto [u,v] = edges[i];
        dsu.merge(u,v);
        cnt++;

        if(dsu.is_bip){
            opt_mid = i - 1;
        }
        else{
            break;
        }
    }

    best[mid] = opt_mid;
    dsu.rollback(cnt);

    // add everybody > opt_mid
    cnt = 0;
    for(int i = opt_mid + 1; i <= r2; ++i){
        auto [u,v] = edges[i];
        dsu.merge(u,v);
        cnt++;
    }

    go(l1,mid-1,l2,opt_mid);
    dsu.rollback(cnt);

    // add everybody < mid+1
    cnt = 0;
    for(int i = l1; i <= mid; ++i){
        auto [u,v] = edges[i];
        dsu.merge(u,v);
        cnt++;
    }

    go(mid+1,r1,opt_mid,r2);
    dsu.rollback(cnt);
}

void solve(int test_case)
{
    int n,m,q; cin >> n >> m >> q;
    rep1(i,m) cin >> edges[i].ff >> edges[i].ss;
    edges[m+1] = {0,n+1};

    go(1,m,1,m+1);

    assert(dsu.history.empty());

    // rep1(l,n){
    //     cout << best[l] << " ";
    // }
    // cout << endl;

    while(q--){
        int l,r; cin >> l >> r;
        if(r < best[l]){
            yes;
        }
        else{
            no;
        }
    }
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5040 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5044 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5040 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 5040 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 4 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5040 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5044 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5040 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 5040 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 4 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 4 ms 5184 KB Output is correct
30 Correct 5 ms 5208 KB Output is correct
31 Correct 5 ms 5204 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 5 ms 5156 KB Output is correct
34 Correct 5 ms 5184 KB Output is correct
35 Correct 5 ms 5180 KB Output is correct
36 Correct 6 ms 5124 KB Output is correct
37 Correct 5 ms 5176 KB Output is correct
38 Correct 6 ms 5300 KB Output is correct
39 Correct 4 ms 5204 KB Output is correct
40 Correct 4 ms 5180 KB Output is correct
41 Correct 5 ms 5204 KB Output is correct
42 Correct 5 ms 5176 KB Output is correct
43 Correct 4 ms 5204 KB Output is correct
44 Correct 5 ms 5204 KB Output is correct
45 Correct 5 ms 5204 KB Output is correct
46 Correct 4 ms 5204 KB Output is correct
47 Correct 6 ms 5180 KB Output is correct
48 Correct 5 ms 5204 KB Output is correct
49 Correct 5 ms 5204 KB Output is correct
50 Correct 5 ms 5180 KB Output is correct
51 Correct 6 ms 5136 KB Output is correct
52 Correct 4 ms 5180 KB Output is correct
53 Correct 5 ms 5300 KB Output is correct
54 Correct 5 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 314 ms 15348 KB Output is correct
4 Correct 502 ms 25628 KB Output is correct
5 Correct 246 ms 15376 KB Output is correct
6 Correct 289 ms 15368 KB Output is correct
7 Correct 293 ms 15416 KB Output is correct
8 Correct 331 ms 15336 KB Output is correct
9 Correct 360 ms 15376 KB Output is correct
10 Correct 506 ms 15340 KB Output is correct
11 Correct 338 ms 15356 KB Output is correct
12 Correct 348 ms 15384 KB Output is correct
13 Correct 281 ms 15360 KB Output is correct
14 Correct 304 ms 15376 KB Output is correct
15 Correct 381 ms 15408 KB Output is correct
16 Correct 387 ms 15408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5040 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5044 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5040 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 5040 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 4 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 314 ms 15348 KB Output is correct
30 Correct 502 ms 25628 KB Output is correct
31 Correct 246 ms 15376 KB Output is correct
32 Correct 289 ms 15368 KB Output is correct
33 Correct 293 ms 15416 KB Output is correct
34 Correct 331 ms 15336 KB Output is correct
35 Correct 360 ms 15376 KB Output is correct
36 Correct 506 ms 15340 KB Output is correct
37 Correct 338 ms 15356 KB Output is correct
38 Correct 348 ms 15384 KB Output is correct
39 Correct 281 ms 15360 KB Output is correct
40 Correct 304 ms 15376 KB Output is correct
41 Correct 381 ms 15408 KB Output is correct
42 Correct 387 ms 15408 KB Output is correct
43 Correct 297 ms 17164 KB Output is correct
44 Correct 499 ms 27316 KB Output is correct
45 Correct 373 ms 17140 KB Output is correct
46 Correct 271 ms 17076 KB Output is correct
47 Correct 302 ms 17068 KB Output is correct
48 Correct 345 ms 17324 KB Output is correct
49 Correct 423 ms 17084 KB Output is correct
50 Correct 296 ms 16916 KB Output is correct
51 Correct 372 ms 17132 KB Output is correct
52 Correct 368 ms 17072 KB Output is correct
53 Correct 316 ms 16988 KB Output is correct
54 Correct 371 ms 16948 KB Output is correct
55 Correct 392 ms 17040 KB Output is correct
56 Correct 401 ms 16988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5040 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5044 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5040 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 5040 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 4 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 4 ms 5184 KB Output is correct
30 Correct 5 ms 5208 KB Output is correct
31 Correct 5 ms 5204 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 5 ms 5156 KB Output is correct
34 Correct 5 ms 5184 KB Output is correct
35 Correct 5 ms 5180 KB Output is correct
36 Correct 6 ms 5124 KB Output is correct
37 Correct 5 ms 5176 KB Output is correct
38 Correct 6 ms 5300 KB Output is correct
39 Correct 4 ms 5204 KB Output is correct
40 Correct 4 ms 5180 KB Output is correct
41 Correct 5 ms 5204 KB Output is correct
42 Correct 5 ms 5176 KB Output is correct
43 Correct 4 ms 5204 KB Output is correct
44 Correct 5 ms 5204 KB Output is correct
45 Correct 5 ms 5204 KB Output is correct
46 Correct 4 ms 5204 KB Output is correct
47 Correct 6 ms 5180 KB Output is correct
48 Correct 5 ms 5204 KB Output is correct
49 Correct 5 ms 5204 KB Output is correct
50 Correct 5 ms 5180 KB Output is correct
51 Correct 6 ms 5136 KB Output is correct
52 Correct 4 ms 5180 KB Output is correct
53 Correct 5 ms 5300 KB Output is correct
54 Correct 5 ms 5204 KB Output is correct
55 Correct 274 ms 17684 KB Output is correct
56 Correct 407 ms 28160 KB Output is correct
57 Correct 196 ms 17912 KB Output is correct
58 Correct 242 ms 17676 KB Output is correct
59 Correct 311 ms 17712 KB Output is correct
60 Correct 357 ms 17912 KB Output is correct
61 Correct 274 ms 17696 KB Output is correct
62 Correct 316 ms 17844 KB Output is correct
63 Correct 237 ms 17584 KB Output is correct
64 Correct 304 ms 17872 KB Output is correct
65 Correct 382 ms 17880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5040 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5044 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 5040 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 5040 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 4 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 4 ms 5184 KB Output is correct
30 Correct 5 ms 5208 KB Output is correct
31 Correct 5 ms 5204 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 5 ms 5156 KB Output is correct
34 Correct 5 ms 5184 KB Output is correct
35 Correct 5 ms 5180 KB Output is correct
36 Correct 6 ms 5124 KB Output is correct
37 Correct 5 ms 5176 KB Output is correct
38 Correct 6 ms 5300 KB Output is correct
39 Correct 4 ms 5204 KB Output is correct
40 Correct 4 ms 5180 KB Output is correct
41 Correct 5 ms 5204 KB Output is correct
42 Correct 5 ms 5176 KB Output is correct
43 Correct 4 ms 5204 KB Output is correct
44 Correct 5 ms 5204 KB Output is correct
45 Correct 5 ms 5204 KB Output is correct
46 Correct 4 ms 5204 KB Output is correct
47 Correct 6 ms 5180 KB Output is correct
48 Correct 5 ms 5204 KB Output is correct
49 Correct 5 ms 5204 KB Output is correct
50 Correct 5 ms 5180 KB Output is correct
51 Correct 6 ms 5136 KB Output is correct
52 Correct 4 ms 5180 KB Output is correct
53 Correct 5 ms 5300 KB Output is correct
54 Correct 5 ms 5204 KB Output is correct
55 Correct 314 ms 15348 KB Output is correct
56 Correct 502 ms 25628 KB Output is correct
57 Correct 246 ms 15376 KB Output is correct
58 Correct 289 ms 15368 KB Output is correct
59 Correct 293 ms 15416 KB Output is correct
60 Correct 331 ms 15336 KB Output is correct
61 Correct 360 ms 15376 KB Output is correct
62 Correct 506 ms 15340 KB Output is correct
63 Correct 338 ms 15356 KB Output is correct
64 Correct 348 ms 15384 KB Output is correct
65 Correct 281 ms 15360 KB Output is correct
66 Correct 304 ms 15376 KB Output is correct
67 Correct 381 ms 15408 KB Output is correct
68 Correct 387 ms 15408 KB Output is correct
69 Correct 297 ms 17164 KB Output is correct
70 Correct 499 ms 27316 KB Output is correct
71 Correct 373 ms 17140 KB Output is correct
72 Correct 271 ms 17076 KB Output is correct
73 Correct 302 ms 17068 KB Output is correct
74 Correct 345 ms 17324 KB Output is correct
75 Correct 423 ms 17084 KB Output is correct
76 Correct 296 ms 16916 KB Output is correct
77 Correct 372 ms 17132 KB Output is correct
78 Correct 368 ms 17072 KB Output is correct
79 Correct 316 ms 16988 KB Output is correct
80 Correct 371 ms 16948 KB Output is correct
81 Correct 392 ms 17040 KB Output is correct
82 Correct 401 ms 16988 KB Output is correct
83 Correct 274 ms 17684 KB Output is correct
84 Correct 407 ms 28160 KB Output is correct
85 Correct 196 ms 17912 KB Output is correct
86 Correct 242 ms 17676 KB Output is correct
87 Correct 311 ms 17712 KB Output is correct
88 Correct 357 ms 17912 KB Output is correct
89 Correct 274 ms 17696 KB Output is correct
90 Correct 316 ms 17844 KB Output is correct
91 Correct 237 ms 17584 KB Output is correct
92 Correct 304 ms 17872 KB Output is correct
93 Correct 382 ms 17880 KB Output is correct
94 Correct 285 ms 18656 KB Output is correct
95 Correct 373 ms 18600 KB Output is correct
96 Correct 241 ms 18800 KB Output is correct
97 Correct 286 ms 18648 KB Output is correct
98 Correct 305 ms 18596 KB Output is correct
99 Correct 329 ms 18672 KB Output is correct
100 Correct 384 ms 18812 KB Output is correct
101 Correct 291 ms 18676 KB Output is correct
102 Correct 347 ms 18692 KB Output is correct
103 Correct 389 ms 18804 KB Output is correct
104 Correct 285 ms 18624 KB Output is correct
105 Correct 372 ms 18724 KB Output is correct
106 Correct 402 ms 18860 KB Output is correct
107 Correct 213 ms 18864 KB Output is correct
108 Correct 295 ms 18404 KB Output is correct
109 Correct 296 ms 18440 KB Output is correct
110 Correct 274 ms 18448 KB Output is correct
111 Correct 282 ms 18416 KB Output is correct
112 Correct 310 ms 18368 KB Output is correct
113 Correct 301 ms 18464 KB Output is correct
114 Correct 291 ms 18424 KB Output is correct
115 Correct 281 ms 18340 KB Output is correct
116 Correct 285 ms 18440 KB Output is correct