Submission #1013820

# Submission time Handle Problem Language Result Execution time Memory
1013820 2024-07-04T06:33:47 Z Mr_Husanboy Joker (BOI20_joker) C++17
100 / 100
214 ms 35632 KB
//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;



#ifdef LOCAL
#include "debugger.cpp"
#else
#define debug(...)
#endif
template<class T>
int len(T &a){
    return a.size();
}

using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;



struct DSU_roll_back{
    int n;
    vector<int> p;
    vector<int> sz;
    vector<int> d;
    vector<pair<int &, int>> his;
    int bip = 1;

    DSU_roll_back (int n) : n(n), p(n), sz(n), d(n){
        for(int i = 0; i < n;i ++) p[i] = i, sz[i] = 1;
    }

    int get(int x, int &col){
        col ^= d[x];
        return p[x] == x ? x : get(p[x], col);
    }

    void unite(int a, int b){
        if(bip == 0){
            return;
        }
        int ca = 0, cb = 0;
        a = get(a, ca), b = get(b, cb);
        if(a == b){
            if(ca == cb){
                his.push_back({bip, bip});
                bip = 0;
            }
            return;
        }
        if(sz[a] < sz[b]) swap(a, b);
        his.push_back({sz[a], sz[a]});
        his.push_back({p[b], p[b]});
        his.push_back({d[b], d[b]});
        d[b] = ca == cb;
        sz[a] += sz[b];
        p[b] = a;
    }

    int snap(){
        return his.size();
    }

    void roll_back(int until){
        while(len(his) > until){
            his.back().ff = his.back().ss;
            his.pop_back();
        }
    }
};

void solve(){
    int n, m, q; cin >> n >> m >> q;

    vector<int> a(m + 1), b(m + 1);

    for(int i = 1; i <= m; i ++){
        cin >> a[i] >> b[i]; a[i] --; b[i] --;
    }
    vector<vector<pair<int,int>>> qu(m + 1);
    for(int i = 0; i < q; i++){
        int l, r; cin >> l >> r;
        qu[l].push_back({r, i});
    }

    DSU_roll_back ds(n);
    vector<int> ans(q);


    auto calc = [&](auto &calc, int l, int r, int optr)->void{
        if(l > r) return;
        int m = (l + r) / 2;
        int lsz = ds.snap();
        for(int i = l; i < m; i ++) ds.unite(a[i], b[i]);
        int optm = optr;
        while(optr >= m && ds.bip){
            optm --;
            ds.unite(a[optm], b[optm]);
        }
        for(auto [rig, j] : qu[m]){
            ans[j] = rig < optm;
        }

        ds.roll_back(lsz);

        for(int i = optr - 1; i >= optm; i --){
            ds.unite(a[i], b[i]);
        }
        calc(calc, l, m - 1, optm);
        ds.roll_back(lsz);
        for(int i = l; i <= m; i ++){
            ds.unite(a[i], b[i]);
        }
        calc(calc, m + 1, r, optr);
        ds.roll_back(lsz);
    };

    calc(calc, 1, m, m + 1);
    for(auto u : ans) cout << (u ? "YES\n" : "NO\n");
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    auto start = chrono::high_resolution_clock::now();
    #ifndef LOCAL
    if(fileio.size()){
        freopen((fileio + ".in").c_str(), "r", stdin);
        freopen((fileio + ".out").c_str(), "w", stdout);
    }
    #endif
    int testcases = 1;
    #ifdef Tests
    cin >> testcases;
    #endif
    while(testcases --){
        solve();
        if(testcases) cout << '\n';
        #ifdef LOCAL
        else cout << '\n';
        cout << "_________________________" << endl;
        #endif
    }
    #ifdef LOCAL
    auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
    
    cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
    #endif
    return 0;
}

Compilation message

Joker.cpp: In function 'int main()':
Joker.cpp:136:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
  136 |     auto start = chrono::high_resolution_clock::now();
      |          ^~~~~
Joker.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         freopen((fileio + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen((fileio + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 0 ms 456 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 452 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 0 ms 456 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 452 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 616 KB Output is correct
31 Correct 1 ms 764 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 600 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 872 KB Output is correct
38 Correct 2 ms 780 KB Output is correct
39 Correct 2 ms 616 KB Output is correct
40 Correct 2 ms 616 KB Output is correct
41 Correct 1 ms 616 KB Output is correct
42 Correct 1 ms 616 KB Output is correct
43 Correct 1 ms 616 KB Output is correct
44 Correct 1 ms 568 KB Output is correct
45 Correct 2 ms 616 KB Output is correct
46 Correct 2 ms 616 KB Output is correct
47 Correct 1 ms 616 KB Output is correct
48 Correct 1 ms 576 KB Output is correct
49 Correct 1 ms 752 KB Output is correct
50 Correct 2 ms 616 KB Output is correct
51 Correct 2 ms 616 KB Output is correct
52 Correct 2 ms 616 KB Output is correct
53 Correct 1 ms 616 KB Output is correct
54 Correct 1 ms 744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 154 ms 22504 KB Output is correct
4 Correct 168 ms 31328 KB Output is correct
5 Correct 107 ms 31796 KB Output is correct
6 Correct 106 ms 22456 KB Output is correct
7 Correct 102 ms 22456 KB Output is correct
8 Correct 118 ms 17120 KB Output is correct
9 Correct 113 ms 18244 KB Output is correct
10 Correct 142 ms 23124 KB Output is correct
11 Correct 123 ms 18756 KB Output is correct
12 Correct 135 ms 23644 KB Output is correct
13 Correct 101 ms 15664 KB Output is correct
14 Correct 100 ms 17996 KB Output is correct
15 Correct 163 ms 22848 KB Output is correct
16 Correct 155 ms 23632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 0 ms 456 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 452 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 154 ms 22504 KB Output is correct
30 Correct 168 ms 31328 KB Output is correct
31 Correct 107 ms 31796 KB Output is correct
32 Correct 106 ms 22456 KB Output is correct
33 Correct 102 ms 22456 KB Output is correct
34 Correct 118 ms 17120 KB Output is correct
35 Correct 113 ms 18244 KB Output is correct
36 Correct 142 ms 23124 KB Output is correct
37 Correct 123 ms 18756 KB Output is correct
38 Correct 135 ms 23644 KB Output is correct
39 Correct 101 ms 15664 KB Output is correct
40 Correct 100 ms 17996 KB Output is correct
41 Correct 163 ms 22848 KB Output is correct
42 Correct 155 ms 23632 KB Output is correct
43 Correct 152 ms 23508 KB Output is correct
44 Correct 163 ms 32580 KB Output is correct
45 Correct 154 ms 32716 KB Output is correct
46 Correct 102 ms 23360 KB Output is correct
47 Correct 103 ms 23524 KB Output is correct
48 Correct 120 ms 19272 KB Output is correct
49 Correct 154 ms 24288 KB Output is correct
50 Correct 114 ms 18916 KB Output is correct
51 Correct 147 ms 24000 KB Output is correct
52 Correct 145 ms 24784 KB Output is correct
53 Correct 98 ms 15556 KB Output is correct
54 Correct 120 ms 19172 KB Output is correct
55 Correct 158 ms 24252 KB Output is correct
56 Correct 155 ms 24612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 0 ms 456 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 452 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 616 KB Output is correct
31 Correct 1 ms 764 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 600 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 872 KB Output is correct
38 Correct 2 ms 780 KB Output is correct
39 Correct 2 ms 616 KB Output is correct
40 Correct 2 ms 616 KB Output is correct
41 Correct 1 ms 616 KB Output is correct
42 Correct 1 ms 616 KB Output is correct
43 Correct 1 ms 616 KB Output is correct
44 Correct 1 ms 568 KB Output is correct
45 Correct 2 ms 616 KB Output is correct
46 Correct 2 ms 616 KB Output is correct
47 Correct 1 ms 616 KB Output is correct
48 Correct 1 ms 576 KB Output is correct
49 Correct 1 ms 752 KB Output is correct
50 Correct 2 ms 616 KB Output is correct
51 Correct 2 ms 616 KB Output is correct
52 Correct 2 ms 616 KB Output is correct
53 Correct 1 ms 616 KB Output is correct
54 Correct 1 ms 744 KB Output is correct
55 Correct 128 ms 18516 KB Output is correct
56 Correct 151 ms 28088 KB Output is correct
57 Correct 83 ms 27824 KB Output is correct
58 Correct 77 ms 18620 KB Output is correct
59 Correct 109 ms 14536 KB Output is correct
60 Correct 152 ms 19388 KB Output is correct
61 Correct 124 ms 14536 KB Output is correct
62 Correct 117 ms 19560 KB Output is correct
63 Correct 75 ms 11720 KB Output is correct
64 Correct 125 ms 18748 KB Output is correct
65 Correct 151 ms 19652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 0 ms 456 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 452 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 2 ms 616 KB Output is correct
31 Correct 1 ms 764 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 600 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 872 KB Output is correct
38 Correct 2 ms 780 KB Output is correct
39 Correct 2 ms 616 KB Output is correct
40 Correct 2 ms 616 KB Output is correct
41 Correct 1 ms 616 KB Output is correct
42 Correct 1 ms 616 KB Output is correct
43 Correct 1 ms 616 KB Output is correct
44 Correct 1 ms 568 KB Output is correct
45 Correct 2 ms 616 KB Output is correct
46 Correct 2 ms 616 KB Output is correct
47 Correct 1 ms 616 KB Output is correct
48 Correct 1 ms 576 KB Output is correct
49 Correct 1 ms 752 KB Output is correct
50 Correct 2 ms 616 KB Output is correct
51 Correct 2 ms 616 KB Output is correct
52 Correct 2 ms 616 KB Output is correct
53 Correct 1 ms 616 KB Output is correct
54 Correct 1 ms 744 KB Output is correct
55 Correct 154 ms 22504 KB Output is correct
56 Correct 168 ms 31328 KB Output is correct
57 Correct 107 ms 31796 KB Output is correct
58 Correct 106 ms 22456 KB Output is correct
59 Correct 102 ms 22456 KB Output is correct
60 Correct 118 ms 17120 KB Output is correct
61 Correct 113 ms 18244 KB Output is correct
62 Correct 142 ms 23124 KB Output is correct
63 Correct 123 ms 18756 KB Output is correct
64 Correct 135 ms 23644 KB Output is correct
65 Correct 101 ms 15664 KB Output is correct
66 Correct 100 ms 17996 KB Output is correct
67 Correct 163 ms 22848 KB Output is correct
68 Correct 155 ms 23632 KB Output is correct
69 Correct 152 ms 23508 KB Output is correct
70 Correct 163 ms 32580 KB Output is correct
71 Correct 154 ms 32716 KB Output is correct
72 Correct 102 ms 23360 KB Output is correct
73 Correct 103 ms 23524 KB Output is correct
74 Correct 120 ms 19272 KB Output is correct
75 Correct 154 ms 24288 KB Output is correct
76 Correct 114 ms 18916 KB Output is correct
77 Correct 147 ms 24000 KB Output is correct
78 Correct 145 ms 24784 KB Output is correct
79 Correct 98 ms 15556 KB Output is correct
80 Correct 120 ms 19172 KB Output is correct
81 Correct 158 ms 24252 KB Output is correct
82 Correct 155 ms 24612 KB Output is correct
83 Correct 128 ms 18516 KB Output is correct
84 Correct 151 ms 28088 KB Output is correct
85 Correct 83 ms 27824 KB Output is correct
86 Correct 77 ms 18620 KB Output is correct
87 Correct 109 ms 14536 KB Output is correct
88 Correct 152 ms 19388 KB Output is correct
89 Correct 124 ms 14536 KB Output is correct
90 Correct 117 ms 19560 KB Output is correct
91 Correct 75 ms 11720 KB Output is correct
92 Correct 125 ms 18748 KB Output is correct
93 Correct 151 ms 19652 KB Output is correct
94 Correct 182 ms 26124 KB Output is correct
95 Correct 192 ms 35632 KB Output is correct
96 Correct 115 ms 35508 KB Output is correct
97 Correct 142 ms 26044 KB Output is correct
98 Correct 120 ms 26072 KB Output is correct
99 Correct 127 ms 21444 KB Output is correct
100 Correct 190 ms 27348 KB Output is correct
101 Correct 148 ms 21952 KB Output is correct
102 Correct 193 ms 26624 KB Output is correct
103 Correct 177 ms 27332 KB Output is correct
104 Correct 130 ms 19652 KB Output is correct
105 Correct 177 ms 26308 KB Output is correct
106 Correct 205 ms 27124 KB Output is correct
107 Correct 105 ms 33348 KB Output is correct
108 Correct 183 ms 25284 KB Output is correct
109 Correct 179 ms 25296 KB Output is correct
110 Correct 190 ms 25280 KB Output is correct
111 Correct 191 ms 25360 KB Output is correct
112 Correct 182 ms 25412 KB Output is correct
113 Correct 214 ms 25416 KB Output is correct
114 Correct 190 ms 25380 KB Output is correct
115 Correct 204 ms 25372 KB Output is correct
116 Correct 172 ms 25456 KB Output is correct