Submission #973355

# Submission time Handle Problem Language Result Execution time Memory
973355 2024-05-01T19:54:55 Z vladilius Joker (BOI20_joker) C++17
100 / 100
390 ms 23788 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int unsigned int
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second

struct dsu{
    vector<pair<int&, int>> hist;
    vector<int> sz, p;
    vector<bool> f;
    int n;
    dsu(int ns){
        n = ns;
        p.resize(2 * n + 1);
        sz.resize(2 * n + 1);
        for (int i = 1; i <= 2 * n; i++){
            p[i] = i;
            sz[i] = 1;
        }
    }
    int get(int v){
        if (p[v] != v){
            return get(p[v]);
        }
        return v;
    }
    int op(int v){
        return v + n;
    }
    void unite(int x, int y){
        x = get(x); y = get(y);
        if (x == y) return;
        if (sz[x] > sz[y]) swap(x, y);
        hist.pb({p[x], p[x]});
        hist.pb({sz[y], sz[y]});
        p[x] = y;
        sz[y] += sz[x];
    }
    bool add(pii P){
        auto [x, y] = P;
        if (!x) return 0;
        unite(x, op(y));
        unite(y, op(x));
        bool k = ((!f.empty() && f.back()) | (get(x) == get(y)));
        f.pb(k);
        return k;
    }
    void roll_back(int S, int Sf){
        while (hist.size() != S){
            hist.back().ff = hist.back().ss;
            hist.pop_back();
        }
        while (f.size() != Sf){
            f.pop_back();
        }
    }
};

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

    int n, M, q; cin>>n>>M>>q;
    vector<pii> ed = {{0, 0}};
    for (int i = 1; i <= M; i++){
        int u, v; cin>>u>>v;
        ed.pb({u, v});
    }
    dsu T(n);
    for (int i = 1; i <= M; i++){
        T.add(ed[i]);
    }
    if (!T.f.back()){
        for (int i = 1; i <= q; i++){
            cout<<"NO"<<"\n";
        }
        return 0;
    }
    T.roll_back(0, 0);
    
    vector<int> last(M + 1);
    function<void(int, int, pii)> solve = [&](int l, int r, pii R){
        if (l > r) return;

        int m = (l + r) / 2;
        int S = (int) T.hist.size(), Sf = (int) T.f.size();
        for (int i = l; i < m; i++){
            T.add(ed[i]);
        }
        
        int i = R.ss;
        while (T.f.empty() || !T.f.back()){
            T.add(ed[i--]);
        }
        last[m] = i;
        T.roll_back(S, Sf);
        
        S = (int) T.hist.size(); Sf = (int) T.f.size();
        for (int i = last[m] + 1; i <= R.ss; i++){
            T.add(ed[i]);
        }
        solve(l, m - 1, {R.ff, last[m]});
        T.roll_back(S, Sf);
        
        S = (int) T.hist.size(); Sf = (int) T.f.size();
        for (int i = l; i <= m; i++){
            T.add(ed[i]);
        }
        solve(m + 1, r, {last[m], R.ss});
        T.roll_back(S, Sf);
    };
    
    solve(1, M, {0, M});

    while (q--){
        int l, r; cin>>l>>r;
        cout<<((r <= last[l]) ? "YES" : "NO")<<"\n";
    }
}
# 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 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 604 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 3 ms 744 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 2 ms 600 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 600 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 588 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 3 ms 856 KB Output is correct
46 Correct 3 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 3 ms 604 KB Output is correct
51 Correct 2 ms 592 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 2 ms 600 KB Output is correct
54 Correct 3 ms 604 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 233 ms 12356 KB Output is correct
4 Correct 51 ms 23788 KB Output is correct
5 Correct 247 ms 22432 KB Output is correct
6 Correct 265 ms 12364 KB Output is correct
7 Correct 273 ms 13652 KB Output is correct
8 Correct 284 ms 8332 KB Output is correct
9 Correct 317 ms 12884 KB Output is correct
10 Correct 363 ms 23364 KB Output is correct
11 Correct 255 ms 12620 KB Output is correct
12 Correct 341 ms 21700 KB Output is correct
13 Correct 286 ms 5584 KB Output is correct
14 Correct 283 ms 9048 KB Output is correct
15 Correct 384 ms 21492 KB Output is correct
16 Correct 368 ms 21824 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 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 233 ms 12356 KB Output is correct
30 Correct 51 ms 23788 KB Output is correct
31 Correct 247 ms 22432 KB Output is correct
32 Correct 265 ms 12364 KB Output is correct
33 Correct 273 ms 13652 KB Output is correct
34 Correct 284 ms 8332 KB Output is correct
35 Correct 317 ms 12884 KB Output is correct
36 Correct 363 ms 23364 KB Output is correct
37 Correct 255 ms 12620 KB Output is correct
38 Correct 341 ms 21700 KB Output is correct
39 Correct 286 ms 5584 KB Output is correct
40 Correct 283 ms 9048 KB Output is correct
41 Correct 384 ms 21492 KB Output is correct
42 Correct 368 ms 21824 KB Output is correct
43 Correct 224 ms 12616 KB Output is correct
44 Correct 49 ms 23620 KB Output is correct
45 Correct 336 ms 22596 KB Output is correct
46 Correct 267 ms 13908 KB Output is correct
47 Correct 261 ms 13128 KB Output is correct
48 Correct 339 ms 13164 KB Output is correct
49 Correct 370 ms 22860 KB Output is correct
50 Correct 245 ms 13388 KB Output is correct
51 Correct 299 ms 22288 KB Output is correct
52 Correct 332 ms 22856 KB Output is correct
53 Correct 286 ms 5300 KB Output is correct
54 Correct 341 ms 13440 KB Output is correct
55 Correct 377 ms 22564 KB Output is correct
56 Correct 378 ms 21828 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 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 604 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 3 ms 744 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 2 ms 600 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 600 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 588 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 3 ms 856 KB Output is correct
46 Correct 3 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 3 ms 604 KB Output is correct
51 Correct 2 ms 592 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 2 ms 600 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 201 ms 13140 KB Output is correct
56 Correct 41 ms 21836 KB Output is correct
57 Correct 199 ms 22748 KB Output is correct
58 Correct 232 ms 13136 KB Output is correct
59 Correct 307 ms 12112 KB Output is correct
60 Correct 381 ms 21320 KB Output is correct
61 Correct 243 ms 12888 KB Output is correct
62 Correct 316 ms 22292 KB Output is correct
63 Correct 247 ms 8536 KB Output is correct
64 Correct 334 ms 14164 KB Output is correct
65 Correct 367 ms 21824 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 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 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 604 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 3 ms 744 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 1 ms 600 KB Output is correct
39 Correct 2 ms 600 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 600 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 588 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 3 ms 856 KB Output is correct
46 Correct 3 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 604 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 3 ms 604 KB Output is correct
51 Correct 2 ms 592 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 2 ms 600 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 233 ms 12356 KB Output is correct
56 Correct 51 ms 23788 KB Output is correct
57 Correct 247 ms 22432 KB Output is correct
58 Correct 265 ms 12364 KB Output is correct
59 Correct 273 ms 13652 KB Output is correct
60 Correct 284 ms 8332 KB Output is correct
61 Correct 317 ms 12884 KB Output is correct
62 Correct 363 ms 23364 KB Output is correct
63 Correct 255 ms 12620 KB Output is correct
64 Correct 341 ms 21700 KB Output is correct
65 Correct 286 ms 5584 KB Output is correct
66 Correct 283 ms 9048 KB Output is correct
67 Correct 384 ms 21492 KB Output is correct
68 Correct 368 ms 21824 KB Output is correct
69 Correct 224 ms 12616 KB Output is correct
70 Correct 49 ms 23620 KB Output is correct
71 Correct 336 ms 22596 KB Output is correct
72 Correct 267 ms 13908 KB Output is correct
73 Correct 261 ms 13128 KB Output is correct
74 Correct 339 ms 13164 KB Output is correct
75 Correct 370 ms 22860 KB Output is correct
76 Correct 245 ms 13388 KB Output is correct
77 Correct 299 ms 22288 KB Output is correct
78 Correct 332 ms 22856 KB Output is correct
79 Correct 286 ms 5300 KB Output is correct
80 Correct 341 ms 13440 KB Output is correct
81 Correct 377 ms 22564 KB Output is correct
82 Correct 378 ms 21828 KB Output is correct
83 Correct 201 ms 13140 KB Output is correct
84 Correct 41 ms 21836 KB Output is correct
85 Correct 199 ms 22748 KB Output is correct
86 Correct 232 ms 13136 KB Output is correct
87 Correct 307 ms 12112 KB Output is correct
88 Correct 381 ms 21320 KB Output is correct
89 Correct 243 ms 12888 KB Output is correct
90 Correct 316 ms 22292 KB Output is correct
91 Correct 247 ms 8536 KB Output is correct
92 Correct 334 ms 14164 KB Output is correct
93 Correct 367 ms 21824 KB Output is correct
94 Correct 232 ms 13648 KB Output is correct
95 Correct 320 ms 21676 KB Output is correct
96 Correct 247 ms 22092 KB Output is correct
97 Correct 257 ms 12372 KB Output is correct
98 Correct 262 ms 13128 KB Output is correct
99 Correct 299 ms 8328 KB Output is correct
100 Correct 365 ms 23356 KB Output is correct
101 Correct 259 ms 12632 KB Output is correct
102 Correct 311 ms 21848 KB Output is correct
103 Correct 385 ms 23360 KB Output is correct
104 Correct 297 ms 7504 KB Output is correct
105 Correct 344 ms 12616 KB Output is correct
106 Correct 390 ms 21856 KB Output is correct
107 Correct 234 ms 22252 KB Output is correct
108 Correct 224 ms 12704 KB Output is correct
109 Correct 221 ms 13384 KB Output is correct
110 Correct 224 ms 13140 KB Output is correct
111 Correct 238 ms 13648 KB Output is correct
112 Correct 227 ms 11964 KB Output is correct
113 Correct 243 ms 12664 KB Output is correct
114 Correct 223 ms 12116 KB Output is correct
115 Correct 238 ms 12104 KB Output is correct
116 Correct 225 ms 12112 KB Output is correct