Submission #973352

# Submission time Handle Problem Language Result Execution time Memory
973352 2024-05-01T19:48:42 Z vladilius Joker (BOI20_joker) C++17
100 / 100
448 ms 26196 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 clear(){
        while (!hist.empty()){
            hist.back().ff = hist.back().ss;
            hist.pop_back();
        }
        f.clear();
    }
};

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.clear();
    
    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;
        while (T.hist.size() != S){
            T.hist.back().ff = T.hist.back().ss;
            T.hist.pop_back();
        }
        while (T.f.size() != Sf){
            T.f.pop_back();
        }
        
        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]});
        while (T.hist.size() != S){
            T.hist.back().ff = T.hist.back().ss;
            T.hist.pop_back();
        }
        while (T.f.size() != Sf){
            T.f.pop_back();
        }
        
        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});
        while (T.hist.size() != S){
            T.hist.back().ff = T.hist.back().ss;
            T.hist.pop_back();
        }
        while (T.f.size() != Sf){
            T.f.pop_back();
        }
    };
    
    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 lambda function:
Joker.cpp:97:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int&, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   97 |         while (T.hist.size() != S){
      |                ~~~~~~~~~~~~~~^~~~
Joker.cpp:101:27: warning: comparison of integer expressions of different signedness: 'std::vector<bool>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  101 |         while (T.f.size() != Sf){
      |                ~~~~~~~~~~~^~~~~
Joker.cpp:110:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int&, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  110 |         while (T.hist.size() != S){
      |                ~~~~~~~~~~~~~~^~~~
Joker.cpp:114:27: warning: comparison of integer expressions of different signedness: 'std::vector<bool>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  114 |         while (T.f.size() != Sf){
      |                ~~~~~~~~~~~^~~~~
Joker.cpp:123:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int&, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  123 |         while (T.hist.size() != S){
      |                ~~~~~~~~~~~~~~^~~~
Joker.cpp:127:27: warning: comparison of integer expressions of different signedness: 'std::vector<bool>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  127 |         while (T.f.size() != Sf){
      |                ~~~~~~~~~~~^~~~~
# 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 344 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 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 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 1 ms 348 KB Output is correct
20 Correct 1 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 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 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 344 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 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 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 1 ms 348 KB Output is correct
20 Correct 1 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 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 856 KB Output is correct
30 Correct 2 ms 600 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 600 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 3 ms 604 KB Output is correct
35 Correct 3 ms 604 KB Output is correct
36 Correct 2 ms 856 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 2 ms 604 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 600 KB Output is correct
43 Correct 3 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 752 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 856 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 348 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 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 240 ms 12856 KB Output is correct
4 Correct 53 ms 25908 KB Output is correct
5 Correct 248 ms 23880 KB Output is correct
6 Correct 281 ms 16760 KB Output is correct
7 Correct 302 ms 15324 KB Output is correct
8 Correct 305 ms 11228 KB Output is correct
9 Correct 333 ms 15484 KB Output is correct
10 Correct 398 ms 24648 KB Output is correct
11 Correct 294 ms 15768 KB Output is correct
12 Correct 338 ms 24532 KB Output is correct
13 Correct 322 ms 8624 KB Output is correct
14 Correct 303 ms 11844 KB Output is correct
15 Correct 448 ms 23340 KB Output is correct
16 Correct 407 ms 24384 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 344 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 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 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 1 ms 348 KB Output is correct
20 Correct 1 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 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 240 ms 12856 KB Output is correct
30 Correct 53 ms 25908 KB Output is correct
31 Correct 248 ms 23880 KB Output is correct
32 Correct 281 ms 16760 KB Output is correct
33 Correct 302 ms 15324 KB Output is correct
34 Correct 305 ms 11228 KB Output is correct
35 Correct 333 ms 15484 KB Output is correct
36 Correct 398 ms 24648 KB Output is correct
37 Correct 294 ms 15768 KB Output is correct
38 Correct 338 ms 24532 KB Output is correct
39 Correct 322 ms 8624 KB Output is correct
40 Correct 303 ms 11844 KB Output is correct
41 Correct 448 ms 23340 KB Output is correct
42 Correct 407 ms 24384 KB Output is correct
43 Correct 242 ms 16848 KB Output is correct
44 Correct 51 ms 25924 KB Output is correct
45 Correct 356 ms 25508 KB Output is correct
46 Correct 277 ms 16952 KB Output is correct
47 Correct 282 ms 15928 KB Output is correct
48 Correct 348 ms 16288 KB Output is correct
49 Correct 423 ms 26184 KB Output is correct
50 Correct 268 ms 15168 KB Output is correct
51 Correct 307 ms 24076 KB Output is correct
52 Correct 357 ms 26196 KB Output is correct
53 Correct 316 ms 9036 KB Output is correct
54 Correct 348 ms 15484 KB Output is correct
55 Correct 388 ms 24644 KB Output is correct
56 Correct 404 ms 24892 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 344 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 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 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 1 ms 348 KB Output is correct
20 Correct 1 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 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 856 KB Output is correct
30 Correct 2 ms 600 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 600 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 3 ms 604 KB Output is correct
35 Correct 3 ms 604 KB Output is correct
36 Correct 2 ms 856 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 2 ms 604 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 600 KB Output is correct
43 Correct 3 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 752 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 856 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 348 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 227 ms 12524 KB Output is correct
56 Correct 46 ms 24452 KB Output is correct
57 Correct 220 ms 25668 KB Output is correct
58 Correct 261 ms 15688 KB Output is correct
59 Correct 330 ms 15868 KB Output is correct
60 Correct 397 ms 24380 KB Output is correct
61 Correct 269 ms 14164 KB Output is correct
62 Correct 298 ms 25428 KB Output is correct
63 Correct 264 ms 9112 KB Output is correct
64 Correct 351 ms 14924 KB Output is correct
65 Correct 371 ms 25396 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 344 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 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 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 1 ms 348 KB Output is correct
20 Correct 1 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 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 856 KB Output is correct
30 Correct 2 ms 600 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 600 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 3 ms 604 KB Output is correct
35 Correct 3 ms 604 KB Output is correct
36 Correct 2 ms 856 KB Output is correct
37 Correct 3 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 2 ms 604 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 600 KB Output is correct
43 Correct 3 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 752 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 856 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 348 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 240 ms 12856 KB Output is correct
56 Correct 53 ms 25908 KB Output is correct
57 Correct 248 ms 23880 KB Output is correct
58 Correct 281 ms 16760 KB Output is correct
59 Correct 302 ms 15324 KB Output is correct
60 Correct 305 ms 11228 KB Output is correct
61 Correct 333 ms 15484 KB Output is correct
62 Correct 398 ms 24648 KB Output is correct
63 Correct 294 ms 15768 KB Output is correct
64 Correct 338 ms 24532 KB Output is correct
65 Correct 322 ms 8624 KB Output is correct
66 Correct 303 ms 11844 KB Output is correct
67 Correct 448 ms 23340 KB Output is correct
68 Correct 407 ms 24384 KB Output is correct
69 Correct 242 ms 16848 KB Output is correct
70 Correct 51 ms 25924 KB Output is correct
71 Correct 356 ms 25508 KB Output is correct
72 Correct 277 ms 16952 KB Output is correct
73 Correct 282 ms 15928 KB Output is correct
74 Correct 348 ms 16288 KB Output is correct
75 Correct 423 ms 26184 KB Output is correct
76 Correct 268 ms 15168 KB Output is correct
77 Correct 307 ms 24076 KB Output is correct
78 Correct 357 ms 26196 KB Output is correct
79 Correct 316 ms 9036 KB Output is correct
80 Correct 348 ms 15484 KB Output is correct
81 Correct 388 ms 24644 KB Output is correct
82 Correct 404 ms 24892 KB Output is correct
83 Correct 227 ms 12524 KB Output is correct
84 Correct 46 ms 24452 KB Output is correct
85 Correct 220 ms 25668 KB Output is correct
86 Correct 261 ms 15688 KB Output is correct
87 Correct 330 ms 15868 KB Output is correct
88 Correct 397 ms 24380 KB Output is correct
89 Correct 269 ms 14164 KB Output is correct
90 Correct 298 ms 25428 KB Output is correct
91 Correct 264 ms 9112 KB Output is correct
92 Correct 351 ms 14924 KB Output is correct
93 Correct 371 ms 25396 KB Output is correct
94 Correct 246 ms 17284 KB Output is correct
95 Correct 342 ms 25412 KB Output is correct
96 Correct 248 ms 24904 KB Output is correct
97 Correct 289 ms 18236 KB Output is correct
98 Correct 284 ms 16188 KB Output is correct
99 Correct 323 ms 12976 KB Output is correct
100 Correct 374 ms 26180 KB Output is correct
101 Correct 260 ms 15740 KB Output is correct
102 Correct 331 ms 25156 KB Output is correct
103 Correct 360 ms 24652 KB Output is correct
104 Correct 299 ms 12352 KB Output is correct
105 Correct 389 ms 17848 KB Output is correct
106 Correct 407 ms 23888 KB Output is correct
107 Correct 247 ms 24880 KB Output is correct
108 Correct 245 ms 16696 KB Output is correct
109 Correct 243 ms 17648 KB Output is correct
110 Correct 249 ms 16060 KB Output is correct
111 Correct 244 ms 16472 KB Output is correct
112 Correct 249 ms 16796 KB Output is correct
113 Correct 247 ms 16696 KB Output is correct
114 Correct 240 ms 16700 KB Output is correct
115 Correct 243 ms 17464 KB Output is correct
116 Correct 247 ms 16644 KB Output is correct