Submission #973353

# Submission time Handle Problem Language Result Execution time Memory
973353 2024-05-01T19:51:29 Z vladilius Joker (BOI20_joker) C++17
100 / 100
430 ms 23628 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
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();
        }
    }
};

int 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";
    }
}

Compilation message

Joker.cpp: In member function 'void dsu::roll_back(int, int)':
Joker.cpp:51:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int&, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   51 |         while (hist.size() != S){
      |                ~~~~~~~~~~~~^~~~
Joker.cpp:55:25: warning: comparison of integer expressions of different signedness: 'std::vector<bool>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   55 |         while (f.size() != Sf){
      |                ~~~~~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 344 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 1 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 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 344 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 1 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 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 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 676 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 3 ms 748 KB Output is correct
35 Correct 3 ms 600 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 776 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 3 ms 524 KB Output is correct
40 Correct 3 ms 760 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 348 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 2 ms 604 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 2 ms 604 KB Output is correct
51 Correct 3 ms 596 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 3 ms 604 KB Output is correct
54 Correct 3 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 238 ms 11860 KB Output is correct
4 Correct 50 ms 22340 KB Output is correct
5 Correct 259 ms 21576 KB Output is correct
6 Correct 272 ms 12360 KB Output is correct
7 Correct 323 ms 13392 KB Output is correct
8 Correct 336 ms 8524 KB Output is correct
9 Correct 343 ms 12108 KB Output is correct
10 Correct 408 ms 23320 KB Output is correct
11 Correct 270 ms 13644 KB Output is correct
12 Correct 333 ms 21960 KB Output is correct
13 Correct 324 ms 5320 KB Output is correct
14 Correct 319 ms 8536 KB Output is correct
15 Correct 398 ms 21576 KB Output is correct
16 Correct 399 ms 22340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 344 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 1 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 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 238 ms 11860 KB Output is correct
30 Correct 50 ms 22340 KB Output is correct
31 Correct 259 ms 21576 KB Output is correct
32 Correct 272 ms 12360 KB Output is correct
33 Correct 323 ms 13392 KB Output is correct
34 Correct 336 ms 8524 KB Output is correct
35 Correct 343 ms 12108 KB Output is correct
36 Correct 408 ms 23320 KB Output is correct
37 Correct 270 ms 13644 KB Output is correct
38 Correct 333 ms 21960 KB Output is correct
39 Correct 324 ms 5320 KB Output is correct
40 Correct 319 ms 8536 KB Output is correct
41 Correct 398 ms 21576 KB Output is correct
42 Correct 399 ms 22340 KB Output is correct
43 Correct 246 ms 13268 KB Output is correct
44 Correct 51 ms 22576 KB Output is correct
45 Correct 386 ms 22848 KB Output is correct
46 Correct 276 ms 12584 KB Output is correct
47 Correct 279 ms 12372 KB Output is correct
48 Correct 367 ms 13380 KB Output is correct
49 Correct 404 ms 22720 KB Output is correct
50 Correct 295 ms 12080 KB Output is correct
51 Correct 312 ms 22180 KB Output is correct
52 Correct 341 ms 22092 KB Output is correct
53 Correct 306 ms 5324 KB Output is correct
54 Correct 362 ms 12112 KB Output is correct
55 Correct 395 ms 21056 KB Output is correct
56 Correct 430 ms 22296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 344 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 1 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 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 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 676 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 3 ms 748 KB Output is correct
35 Correct 3 ms 600 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 776 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 3 ms 524 KB Output is correct
40 Correct 3 ms 760 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 348 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 2 ms 604 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 2 ms 604 KB Output is correct
51 Correct 3 ms 596 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 3 ms 604 KB Output is correct
54 Correct 3 ms 600 KB Output is correct
55 Correct 230 ms 12908 KB Output is correct
56 Correct 46 ms 23628 KB Output is correct
57 Correct 222 ms 21824 KB Output is correct
58 Correct 276 ms 12628 KB Output is correct
59 Correct 338 ms 12364 KB Output is correct
60 Correct 390 ms 22756 KB Output is correct
61 Correct 276 ms 13644 KB Output is correct
62 Correct 306 ms 21828 KB Output is correct
63 Correct 270 ms 7756 KB Output is correct
64 Correct 357 ms 13908 KB Output is correct
65 Correct 397 ms 22340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 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 344 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 1 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 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 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 676 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 3 ms 748 KB Output is correct
35 Correct 3 ms 600 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 3 ms 776 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 3 ms 524 KB Output is correct
40 Correct 3 ms 760 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 2 ms 348 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 2 ms 604 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 2 ms 604 KB Output is correct
51 Correct 3 ms 596 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 3 ms 604 KB Output is correct
54 Correct 3 ms 600 KB Output is correct
55 Correct 238 ms 11860 KB Output is correct
56 Correct 50 ms 22340 KB Output is correct
57 Correct 259 ms 21576 KB Output is correct
58 Correct 272 ms 12360 KB Output is correct
59 Correct 323 ms 13392 KB Output is correct
60 Correct 336 ms 8524 KB Output is correct
61 Correct 343 ms 12108 KB Output is correct
62 Correct 408 ms 23320 KB Output is correct
63 Correct 270 ms 13644 KB Output is correct
64 Correct 333 ms 21960 KB Output is correct
65 Correct 324 ms 5320 KB Output is correct
66 Correct 319 ms 8536 KB Output is correct
67 Correct 398 ms 21576 KB Output is correct
68 Correct 399 ms 22340 KB Output is correct
69 Correct 246 ms 13268 KB Output is correct
70 Correct 51 ms 22576 KB Output is correct
71 Correct 386 ms 22848 KB Output is correct
72 Correct 276 ms 12584 KB Output is correct
73 Correct 279 ms 12372 KB Output is correct
74 Correct 367 ms 13380 KB Output is correct
75 Correct 404 ms 22720 KB Output is correct
76 Correct 295 ms 12080 KB Output is correct
77 Correct 312 ms 22180 KB Output is correct
78 Correct 341 ms 22092 KB Output is correct
79 Correct 306 ms 5324 KB Output is correct
80 Correct 362 ms 12112 KB Output is correct
81 Correct 395 ms 21056 KB Output is correct
82 Correct 430 ms 22296 KB Output is correct
83 Correct 230 ms 12908 KB Output is correct
84 Correct 46 ms 23628 KB Output is correct
85 Correct 222 ms 21824 KB Output is correct
86 Correct 276 ms 12628 KB Output is correct
87 Correct 338 ms 12364 KB Output is correct
88 Correct 390 ms 22756 KB Output is correct
89 Correct 276 ms 13644 KB Output is correct
90 Correct 306 ms 21828 KB Output is correct
91 Correct 270 ms 7756 KB Output is correct
92 Correct 357 ms 13908 KB Output is correct
93 Correct 397 ms 22340 KB Output is correct
94 Correct 251 ms 13200 KB Output is correct
95 Correct 361 ms 22588 KB Output is correct
96 Correct 242 ms 22092 KB Output is correct
97 Correct 284 ms 13140 KB Output is correct
98 Correct 290 ms 12876 KB Output is correct
99 Correct 332 ms 8524 KB Output is correct
100 Correct 393 ms 22856 KB Output is correct
101 Correct 262 ms 12624 KB Output is correct
102 Correct 336 ms 20804 KB Output is correct
103 Correct 375 ms 22364 KB Output is correct
104 Correct 320 ms 8280 KB Output is correct
105 Correct 384 ms 13900 KB Output is correct
106 Correct 425 ms 21316 KB Output is correct
107 Correct 244 ms 22344 KB Output is correct
108 Correct 255 ms 13152 KB Output is correct
109 Correct 238 ms 12112 KB Output is correct
110 Correct 243 ms 13524 KB Output is correct
111 Correct 279 ms 12108 KB Output is correct
112 Correct 242 ms 13396 KB Output is correct
113 Correct 253 ms 12872 KB Output is correct
114 Correct 240 ms 13392 KB Output is correct
115 Correct 245 ms 13900 KB Output is correct
116 Correct 257 ms 13388 KB Output is correct