Submission #551829

# Submission time Handle Problem Language Result Execution time Memory
551829 2022-04-21T16:44:28 Z oleh1421 Joker (BOI20_joker) C++17
100 / 100
492 ms 23680 KB
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#define endl '\n'
using namespace __gnu_pbds;
typedef long double ld;
//#define int ll
using namespace std;
mt19937 rnd(time(NULL));
typedef long long ll;
const int N=400100;
const int K=10000;
int a[N],b[N];
int ans[N];
int l[N/K+1];
int r[N/K+1];
int group[N];


int w[N];
int sz[N];
int dsu[N];
bool BAD=false;
vector<pair<int,pair<int,int> > >CH;
pair<int,int>get(int x){
    if (x==dsu[x]) return {x,w[x]};
    auto cur=get(dsu[x]);
    cur.second^=w[x];
    return cur;
}
void unite(int a,int b){
    if (a==0 && b==0) return;
    auto x=get(a);
    auto y=get(b);
    if (x.first==y.first){
        if (x.second==y.second){
            CH.push_back({4,{-1,BAD}});
            BAD=true;
        }
        return;
    }
    if (sz[x.first]>sz[y.first]) swap(x,y);
    CH.push_back({1,{x.first,dsu[x.first]}});
    CH.push_back({2,{y.first,sz[y.first]}});
    CH.push_back({3,{x.first,w[x.first]}});
    dsu[x.first]=y.first;
    sz[y.first]+=sz[x.first];
    w[x.first]^=(x.second^y.second^1^w[y.first]);
}
void roll_back(){
    if (!CH.empty()){
        auto cur=CH.back();
        CH.pop_back();
        if (cur.first==1) dsu[cur.second.first]=cur.second.second;
        else if (cur.first==2) sz[cur.second.first]=cur.second.second;
        else if (cur.first==3) w[cur.second.first]=cur.second.second;
        else BAD=cur.second.second;
    }
}
int L[N],R[N];
bool used[N];
int n,m,q;
void rec(int l,int r,int L,int R){
    if (l>r) return;
    int m=(l+r)/2;
    int len=CH.size();
    for (int i=l;i<=m;i++){
        unite(a[i],b[i]);
    }
    int ind=-1;
    for (int i=R;i>=L;i--){
        unite(a[i],b[i]);
        if (BAD){
            ind=i;
            break;
        }
    }
    ans[m]=ind;
    while (CH.size()>len) roll_back();
    for (int i=R;i>ind;i--){
        unite(a[i],b[i]);
    }
    rec(l,m-1,L,ind);
    while (CH.size()>len) roll_back();
//    for (int i=R;i>ind;i--){
//        unite(a[i],b[i]);
//    }
    for (int i=l;i<=m;i++){
        unite(a[i],b[i]);
    }
    rec(m+1,r,ind,R);
    while (CH.size()>len) roll_back();

}
void solve(){
    cin>>n>>m>>q;
    for (int i=1;i<=m;i++){
        cin>>a[i]>>b[i];
    }
    for (int i=1;i<=q;i++){
        cin>>L[i]>>R[i];
        used[L[i]-1]=true;
    }
    for (int i=1;i<=n;i++){
        dsu[i]=i;
        sz[i]=1;
        w[i]=0;
    }
    BAD=false;
    for (int i=1;i<=m;i++){
        unite(a[i],b[i]);
    }
    if (!BAD){
        for (int i=0;i<=m;i++) ans[i]=0;
    } else {
        while (!CH.empty()) roll_back();
        rec(0,m,1,m+1);
    }


    for (int i=1;i<=q;i++){
        if (ans[L[i]-1]<=R[i]) cout<<"NO\n";
        else cout<<"YES\n";
    }



}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int tt=1;
    while (tt--){
        solve();
    }

    return 0;
}

/**
6 8 2
1 3
1 5
1 6
2 5
2 6
3 4
3 5
5 6
4 8
4 7
**/

Compilation message

Joker.cpp: In function 'void rec(int, int, int, int)':
Joker.cpp:79:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   79 |     while (CH.size()>len) roll_back();
      |            ~~~~~~~~~^~~~
Joker.cpp:84:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   84 |     while (CH.size()>len) roll_back();
      |            ~~~~~~~~~^~~~
Joker.cpp:92:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   92 |     while (CH.size()>len) roll_back();
      |            ~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 516 KB Output is correct
30 Correct 4 ms 608 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 3 ms 612 KB Output is correct
36 Correct 3 ms 468 KB Output is correct
37 Correct 4 ms 596 KB Output is correct
38 Correct 2 ms 608 KB Output is correct
39 Correct 3 ms 596 KB Output is correct
40 Correct 3 ms 484 KB Output is correct
41 Correct 3 ms 468 KB Output is correct
42 Correct 3 ms 704 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 4 ms 484 KB Output is correct
45 Correct 4 ms 596 KB Output is correct
46 Correct 4 ms 596 KB Output is correct
47 Correct 3 ms 468 KB Output is correct
48 Correct 4 ms 528 KB Output is correct
49 Correct 3 ms 632 KB Output is correct
50 Correct 4 ms 608 KB Output is correct
51 Correct 3 ms 468 KB Output is correct
52 Correct 2 ms 468 KB Output is correct
53 Correct 3 ms 616 KB Output is correct
54 Correct 3 ms 608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 340 ms 10940 KB Output is correct
4 Correct 81 ms 18188 KB Output is correct
5 Correct 298 ms 18060 KB Output is correct
6 Correct 313 ms 10924 KB Output is correct
7 Correct 367 ms 10964 KB Output is correct
8 Correct 293 ms 8620 KB Output is correct
9 Correct 382 ms 10940 KB Output is correct
10 Correct 458 ms 13504 KB Output is correct
11 Correct 325 ms 10932 KB Output is correct
12 Correct 380 ms 12868 KB Output is correct
13 Correct 256 ms 6476 KB Output is correct
14 Correct 301 ms 8492 KB Output is correct
15 Correct 445 ms 11744 KB Output is correct
16 Correct 464 ms 13484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 340 ms 10940 KB Output is correct
30 Correct 81 ms 18188 KB Output is correct
31 Correct 298 ms 18060 KB Output is correct
32 Correct 313 ms 10924 KB Output is correct
33 Correct 367 ms 10964 KB Output is correct
34 Correct 293 ms 8620 KB Output is correct
35 Correct 382 ms 10940 KB Output is correct
36 Correct 458 ms 13504 KB Output is correct
37 Correct 325 ms 10932 KB Output is correct
38 Correct 380 ms 12868 KB Output is correct
39 Correct 256 ms 6476 KB Output is correct
40 Correct 301 ms 8492 KB Output is correct
41 Correct 445 ms 11744 KB Output is correct
42 Correct 464 ms 13484 KB Output is correct
43 Correct 301 ms 14092 KB Output is correct
44 Correct 102 ms 21436 KB Output is correct
45 Correct 407 ms 21304 KB Output is correct
46 Correct 296 ms 14012 KB Output is correct
47 Correct 313 ms 14020 KB Output is correct
48 Correct 374 ms 13972 KB Output is correct
49 Correct 455 ms 16604 KB Output is correct
50 Correct 293 ms 13900 KB Output is correct
51 Correct 358 ms 14700 KB Output is correct
52 Correct 411 ms 16580 KB Output is correct
53 Correct 267 ms 9644 KB Output is correct
54 Correct 343 ms 13852 KB Output is correct
55 Correct 465 ms 14764 KB Output is correct
56 Correct 454 ms 16520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 516 KB Output is correct
30 Correct 4 ms 608 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 3 ms 612 KB Output is correct
36 Correct 3 ms 468 KB Output is correct
37 Correct 4 ms 596 KB Output is correct
38 Correct 2 ms 608 KB Output is correct
39 Correct 3 ms 596 KB Output is correct
40 Correct 3 ms 484 KB Output is correct
41 Correct 3 ms 468 KB Output is correct
42 Correct 3 ms 704 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 4 ms 484 KB Output is correct
45 Correct 4 ms 596 KB Output is correct
46 Correct 4 ms 596 KB Output is correct
47 Correct 3 ms 468 KB Output is correct
48 Correct 4 ms 528 KB Output is correct
49 Correct 3 ms 632 KB Output is correct
50 Correct 4 ms 608 KB Output is correct
51 Correct 3 ms 468 KB Output is correct
52 Correct 2 ms 468 KB Output is correct
53 Correct 3 ms 616 KB Output is correct
54 Correct 3 ms 608 KB Output is correct
55 Correct 265 ms 11156 KB Output is correct
56 Correct 66 ms 19376 KB Output is correct
57 Correct 253 ms 19128 KB Output is correct
58 Correct 266 ms 11872 KB Output is correct
59 Correct 391 ms 11844 KB Output is correct
60 Correct 433 ms 12928 KB Output is correct
61 Correct 340 ms 11836 KB Output is correct
62 Correct 356 ms 13396 KB Output is correct
63 Correct 266 ms 8100 KB Output is correct
64 Correct 369 ms 12220 KB Output is correct
65 Correct 444 ms 13476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 516 KB Output is correct
30 Correct 4 ms 608 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 3 ms 612 KB Output is correct
36 Correct 3 ms 468 KB Output is correct
37 Correct 4 ms 596 KB Output is correct
38 Correct 2 ms 608 KB Output is correct
39 Correct 3 ms 596 KB Output is correct
40 Correct 3 ms 484 KB Output is correct
41 Correct 3 ms 468 KB Output is correct
42 Correct 3 ms 704 KB Output is correct
43 Correct 2 ms 468 KB Output is correct
44 Correct 4 ms 484 KB Output is correct
45 Correct 4 ms 596 KB Output is correct
46 Correct 4 ms 596 KB Output is correct
47 Correct 3 ms 468 KB Output is correct
48 Correct 4 ms 528 KB Output is correct
49 Correct 3 ms 632 KB Output is correct
50 Correct 4 ms 608 KB Output is correct
51 Correct 3 ms 468 KB Output is correct
52 Correct 2 ms 468 KB Output is correct
53 Correct 3 ms 616 KB Output is correct
54 Correct 3 ms 608 KB Output is correct
55 Correct 340 ms 10940 KB Output is correct
56 Correct 81 ms 18188 KB Output is correct
57 Correct 298 ms 18060 KB Output is correct
58 Correct 313 ms 10924 KB Output is correct
59 Correct 367 ms 10964 KB Output is correct
60 Correct 293 ms 8620 KB Output is correct
61 Correct 382 ms 10940 KB Output is correct
62 Correct 458 ms 13504 KB Output is correct
63 Correct 325 ms 10932 KB Output is correct
64 Correct 380 ms 12868 KB Output is correct
65 Correct 256 ms 6476 KB Output is correct
66 Correct 301 ms 8492 KB Output is correct
67 Correct 445 ms 11744 KB Output is correct
68 Correct 464 ms 13484 KB Output is correct
69 Correct 301 ms 14092 KB Output is correct
70 Correct 102 ms 21436 KB Output is correct
71 Correct 407 ms 21304 KB Output is correct
72 Correct 296 ms 14012 KB Output is correct
73 Correct 313 ms 14020 KB Output is correct
74 Correct 374 ms 13972 KB Output is correct
75 Correct 455 ms 16604 KB Output is correct
76 Correct 293 ms 13900 KB Output is correct
77 Correct 358 ms 14700 KB Output is correct
78 Correct 411 ms 16580 KB Output is correct
79 Correct 267 ms 9644 KB Output is correct
80 Correct 343 ms 13852 KB Output is correct
81 Correct 465 ms 14764 KB Output is correct
82 Correct 454 ms 16520 KB Output is correct
83 Correct 265 ms 11156 KB Output is correct
84 Correct 66 ms 19376 KB Output is correct
85 Correct 253 ms 19128 KB Output is correct
86 Correct 266 ms 11872 KB Output is correct
87 Correct 391 ms 11844 KB Output is correct
88 Correct 433 ms 12928 KB Output is correct
89 Correct 340 ms 11836 KB Output is correct
90 Correct 356 ms 13396 KB Output is correct
91 Correct 266 ms 8100 KB Output is correct
92 Correct 369 ms 12220 KB Output is correct
93 Correct 444 ms 13476 KB Output is correct
94 Correct 304 ms 14188 KB Output is correct
95 Correct 413 ms 23536 KB Output is correct
96 Correct 316 ms 23272 KB Output is correct
97 Correct 322 ms 16056 KB Output is correct
98 Correct 337 ms 16088 KB Output is correct
99 Correct 311 ms 13632 KB Output is correct
100 Correct 492 ms 18892 KB Output is correct
101 Correct 303 ms 15804 KB Output is correct
102 Correct 401 ms 17036 KB Output is correct
103 Correct 422 ms 18788 KB Output is correct
104 Correct 295 ms 12776 KB Output is correct
105 Correct 452 ms 16420 KB Output is correct
106 Correct 485 ms 18420 KB Output is correct
107 Correct 312 ms 23680 KB Output is correct
108 Correct 297 ms 15832 KB Output is correct
109 Correct 343 ms 15812 KB Output is correct
110 Correct 298 ms 15876 KB Output is correct
111 Correct 320 ms 15832 KB Output is correct
112 Correct 302 ms 15872 KB Output is correct
113 Correct 362 ms 15880 KB Output is correct
114 Correct 300 ms 15892 KB Output is correct
115 Correct 299 ms 15840 KB Output is correct
116 Correct 304 ms 15804 KB Output is correct