Submission #902238

# Submission time Handle Problem Language Result Execution time Memory
902238 2024-01-10T07:25:50 Z 089487 Joker (BOI20_joker) C++14
100 / 100
864 ms 41116 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse4,abm,avx,popcnt")
#include<bits/stdc++.h>
#define int long long
#define quick ios::sync_with_stdio(0);cin.tie(0);
#define rep(x,a,b) for(int x=a;x<=b;x++)
#define repd(x,a,b) for(int x=a;x>=b;x--)
#define lowbit(x) (x&-x)
#define F first
#define S second
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define mp make_pair
#define eb emplace_back
using namespace std;
typedef pair<int,int> pii;
void debug(){
    cout<<"\n";
}
template <class T,class ... U >
void debug(T a, U ... b){
    cout<<a<<" ",debug(b...);
}
const int N=5e5+2;
const int INF=1e18;
pii e[N];
int p[N];
int sz[N];
void init(int n){
    rep(i,1,n) p[i]=i,sz[i]=1;
}
int fp(int x){
    if(x!=p[x]) return fp(p[x]);
    return x;
}
int ans[N];
stack<tuple<int,int,int>> state;
int n,m;
int neg(int x){
    return x>n?x-n:x+n;
}
bool _Union(int a,int b){
    int c=fp(neg(a));
    a=fp(a);
    b=fp(b);
    if(c==b) return false;
    if(a==b){
        state.push(make_tuple(-1,-1,-1));
        return true;
    }
    if(sz[a]<sz[b]) swap(a,b);
    state.push(make_tuple(a,sz[a],b));
    p[b]=a;
    sz[a]+=sz[b];
    return true;
}
bool Union(int a,int b){
    if(_Union(a,neg(b))){
        _Union(b,neg(a));
        return true;
    }
    return false;
}
void undo(){
    auto [a,sza,b]=state.top();
    state.pop();
    if(a==-1) return ;
    sz[a]=sza;
    p[b]=b;
}
void solve(int l,int r,int vl,int vr){
    if(l>r) return ;
    //debug("solve",l,r,vl,vr);
    if(vl==vr){
        rep(i,l,r) ans[i]=vl;
        return ;
    }
    int mid=(l+r)>>1;
    ans[mid]=vr;
    int T0=sz(state);
    if(r<=vl){
        int t0=sz(state);
        for(int i=mid;i<=r;i++){
            Union(e[i].F,e[i].S);
        }
        int t1=sz(state)-(r==vl)*2;
        for(int i=max(r+1,vl);i<=vr;i++){
            if(!Union(e[i].F,e[i].S)){
                ans[mid]=i-1;
                break;
            }
        }
        //debug("mid",mid,"ans",ans[mid]);
        while(sz(state)>t1) undo();
        solve(l,mid-1,vl,ans[mid]);
        while(sz(state)>t0) undo();
        for(int i=vl;i<ans[mid];i++) Union(e[i].F,e[i].S);
        solve(mid+1,r,ans[mid],vr);
    }
    else{
        int t0=sz(state);
        for(int i=mid;i<=vr;i++){
            if(!Union(e[i].F,e[i].S)) {
                ans[mid]=i-1;
                break;
            }
        }
        //debug("mid",mid,"ans",ans[mid]);
        repd(t,ans[mid],max(vl,mid)){
            undo();undo();
        }
        solve(l,mid-1,vl,ans[mid]);
        while(sz(state)>t0) undo();
        rep(t,r+1,ans[mid]-1){
            Union(e[t].F,e[t].S);
        }
        solve(mid+1,r,ans[mid],vr);
    }
    while(sz(state)>T0) undo();
}
signed main(){
	quick
    int q;
    cin>>n>>m>>q;
    init(n*2);
    rep(i,0,m-1){
        cin>>e[i].F>>e[i].S;
        e[i+m]=e[i];
    }
    int m0=m;
    m*=2;
    solve(0,m-1,0,m-1);
    /*rep(i,0,m-1){
        cout<<ans[i]<<" \n"[i==m-1];
    }*/
    /*rep(i,0,m-1){
        init(2*n);
        ans[i]=-1;
        rep(j,i,m-1){
            if(Union(e[j].F,e[j].S)) ans[i]=j;
            else break;
        }
        cout<<ans[i]<<" \n"[i==m-1];
    }*/
    string Ans[2]={"YES","NO"};
    while(q--){
        int l,r;
        cin>>l>>r;
        --l,--r;
        cout<<Ans[(ans[r+1]>=l+m0-1)]<<"\n";
    }
}

Compilation message

Joker.cpp: In function 'void undo()':
Joker.cpp:65:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   65 |     auto [a,sza,b]=state.top();
      |          ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6604 KB Output is correct
5 Correct 1 ms 6608 KB Output is correct
6 Correct 1 ms 6496 KB Output is correct
7 Correct 1 ms 6496 KB Output is correct
8 Correct 1 ms 6620 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 1 ms 6608 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6496 KB Output is correct
14 Correct 1 ms 6488 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6604 KB Output is correct
17 Correct 1 ms 6488 KB Output is correct
18 Correct 1 ms 6544 KB Output is correct
19 Correct 2 ms 6496 KB Output is correct
20 Correct 1 ms 6496 KB Output is correct
21 Correct 1 ms 6488 KB Output is correct
22 Correct 2 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6608 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6504 KB Output is correct
28 Correct 1 ms 6616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6604 KB Output is correct
5 Correct 1 ms 6608 KB Output is correct
6 Correct 1 ms 6496 KB Output is correct
7 Correct 1 ms 6496 KB Output is correct
8 Correct 1 ms 6620 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 1 ms 6608 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6496 KB Output is correct
14 Correct 1 ms 6488 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6604 KB Output is correct
17 Correct 1 ms 6488 KB Output is correct
18 Correct 1 ms 6544 KB Output is correct
19 Correct 2 ms 6496 KB Output is correct
20 Correct 1 ms 6496 KB Output is correct
21 Correct 1 ms 6488 KB Output is correct
22 Correct 2 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6608 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6504 KB Output is correct
28 Correct 1 ms 6616 KB Output is correct
29 Correct 4 ms 6760 KB Output is correct
30 Correct 6 ms 6760 KB Output is correct
31 Correct 4 ms 6504 KB Output is correct
32 Correct 5 ms 6504 KB Output is correct
33 Correct 4 ms 6504 KB Output is correct
34 Correct 4 ms 6760 KB Output is correct
35 Correct 4 ms 6660 KB Output is correct
36 Correct 4 ms 6504 KB Output is correct
37 Correct 4 ms 6776 KB Output is correct
38 Correct 4 ms 6748 KB Output is correct
39 Correct 3 ms 6644 KB Output is correct
40 Correct 3 ms 6632 KB Output is correct
41 Correct 3 ms 6740 KB Output is correct
42 Correct 4 ms 6752 KB Output is correct
43 Correct 6 ms 6760 KB Output is correct
44 Correct 5 ms 6504 KB Output is correct
45 Correct 4 ms 6504 KB Output is correct
46 Correct 4 ms 6760 KB Output is correct
47 Correct 4 ms 6492 KB Output is correct
48 Correct 4 ms 6748 KB Output is correct
49 Correct 4 ms 6744 KB Output is correct
50 Correct 4 ms 6748 KB Output is correct
51 Correct 6 ms 6492 KB Output is correct
52 Correct 5 ms 6616 KB Output is correct
53 Correct 6 ms 6732 KB Output is correct
54 Correct 4 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 487 ms 28292 KB Output is correct
4 Correct 759 ms 41076 KB Output is correct
5 Correct 500 ms 30612 KB Output is correct
6 Correct 408 ms 26444 KB Output is correct
7 Correct 404 ms 27068 KB Output is correct
8 Correct 770 ms 26464 KB Output is correct
9 Correct 817 ms 26488 KB Output is correct
10 Correct 806 ms 27524 KB Output is correct
11 Correct 540 ms 28296 KB Output is correct
12 Correct 563 ms 28648 KB Output is correct
13 Correct 864 ms 22504 KB Output is correct
14 Correct 747 ms 26276 KB Output is correct
15 Correct 773 ms 28512 KB Output is correct
16 Correct 737 ms 27920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6604 KB Output is correct
5 Correct 1 ms 6608 KB Output is correct
6 Correct 1 ms 6496 KB Output is correct
7 Correct 1 ms 6496 KB Output is correct
8 Correct 1 ms 6620 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 1 ms 6608 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6496 KB Output is correct
14 Correct 1 ms 6488 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6604 KB Output is correct
17 Correct 1 ms 6488 KB Output is correct
18 Correct 1 ms 6544 KB Output is correct
19 Correct 2 ms 6496 KB Output is correct
20 Correct 1 ms 6496 KB Output is correct
21 Correct 1 ms 6488 KB Output is correct
22 Correct 2 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6608 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6504 KB Output is correct
28 Correct 1 ms 6616 KB Output is correct
29 Correct 487 ms 28292 KB Output is correct
30 Correct 759 ms 41076 KB Output is correct
31 Correct 500 ms 30612 KB Output is correct
32 Correct 408 ms 26444 KB Output is correct
33 Correct 404 ms 27068 KB Output is correct
34 Correct 770 ms 26464 KB Output is correct
35 Correct 817 ms 26488 KB Output is correct
36 Correct 806 ms 27524 KB Output is correct
37 Correct 540 ms 28296 KB Output is correct
38 Correct 563 ms 28648 KB Output is correct
39 Correct 864 ms 22504 KB Output is correct
40 Correct 747 ms 26276 KB Output is correct
41 Correct 773 ms 28512 KB Output is correct
42 Correct 737 ms 27920 KB Output is correct
43 Correct 498 ms 28588 KB Output is correct
44 Correct 816 ms 40996 KB Output is correct
45 Correct 817 ms 31652 KB Output is correct
46 Correct 477 ms 26656 KB Output is correct
47 Correct 396 ms 26652 KB Output is correct
48 Correct 762 ms 26712 KB Output is correct
49 Correct 731 ms 27044 KB Output is correct
50 Correct 591 ms 27344 KB Output is correct
51 Correct 522 ms 28860 KB Output is correct
52 Correct 564 ms 27752 KB Output is correct
53 Correct 756 ms 22328 KB Output is correct
54 Correct 767 ms 26508 KB Output is correct
55 Correct 720 ms 29008 KB Output is correct
56 Correct 844 ms 27248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6604 KB Output is correct
5 Correct 1 ms 6608 KB Output is correct
6 Correct 1 ms 6496 KB Output is correct
7 Correct 1 ms 6496 KB Output is correct
8 Correct 1 ms 6620 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 1 ms 6608 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6496 KB Output is correct
14 Correct 1 ms 6488 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6604 KB Output is correct
17 Correct 1 ms 6488 KB Output is correct
18 Correct 1 ms 6544 KB Output is correct
19 Correct 2 ms 6496 KB Output is correct
20 Correct 1 ms 6496 KB Output is correct
21 Correct 1 ms 6488 KB Output is correct
22 Correct 2 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6608 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6504 KB Output is correct
28 Correct 1 ms 6616 KB Output is correct
29 Correct 4 ms 6760 KB Output is correct
30 Correct 6 ms 6760 KB Output is correct
31 Correct 4 ms 6504 KB Output is correct
32 Correct 5 ms 6504 KB Output is correct
33 Correct 4 ms 6504 KB Output is correct
34 Correct 4 ms 6760 KB Output is correct
35 Correct 4 ms 6660 KB Output is correct
36 Correct 4 ms 6504 KB Output is correct
37 Correct 4 ms 6776 KB Output is correct
38 Correct 4 ms 6748 KB Output is correct
39 Correct 3 ms 6644 KB Output is correct
40 Correct 3 ms 6632 KB Output is correct
41 Correct 3 ms 6740 KB Output is correct
42 Correct 4 ms 6752 KB Output is correct
43 Correct 6 ms 6760 KB Output is correct
44 Correct 5 ms 6504 KB Output is correct
45 Correct 4 ms 6504 KB Output is correct
46 Correct 4 ms 6760 KB Output is correct
47 Correct 4 ms 6492 KB Output is correct
48 Correct 4 ms 6748 KB Output is correct
49 Correct 4 ms 6744 KB Output is correct
50 Correct 4 ms 6748 KB Output is correct
51 Correct 6 ms 6492 KB Output is correct
52 Correct 5 ms 6616 KB Output is correct
53 Correct 6 ms 6732 KB Output is correct
54 Correct 4 ms 6748 KB Output is correct
55 Correct 456 ms 26272 KB Output is correct
56 Correct 783 ms 41116 KB Output is correct
57 Correct 611 ms 30184 KB Output is correct
58 Correct 363 ms 26476 KB Output is correct
59 Correct 686 ms 26220 KB Output is correct
60 Correct 650 ms 26416 KB Output is correct
61 Correct 489 ms 26244 KB Output is correct
62 Correct 612 ms 26860 KB Output is correct
63 Correct 751 ms 26428 KB Output is correct
64 Correct 584 ms 26276 KB Output is correct
65 Correct 668 ms 26380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6604 KB Output is correct
5 Correct 1 ms 6608 KB Output is correct
6 Correct 1 ms 6496 KB Output is correct
7 Correct 1 ms 6496 KB Output is correct
8 Correct 1 ms 6620 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 1 ms 6608 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6496 KB Output is correct
14 Correct 1 ms 6488 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6604 KB Output is correct
17 Correct 1 ms 6488 KB Output is correct
18 Correct 1 ms 6544 KB Output is correct
19 Correct 2 ms 6496 KB Output is correct
20 Correct 1 ms 6496 KB Output is correct
21 Correct 1 ms 6488 KB Output is correct
22 Correct 2 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6608 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6504 KB Output is correct
28 Correct 1 ms 6616 KB Output is correct
29 Correct 4 ms 6760 KB Output is correct
30 Correct 6 ms 6760 KB Output is correct
31 Correct 4 ms 6504 KB Output is correct
32 Correct 5 ms 6504 KB Output is correct
33 Correct 4 ms 6504 KB Output is correct
34 Correct 4 ms 6760 KB Output is correct
35 Correct 4 ms 6660 KB Output is correct
36 Correct 4 ms 6504 KB Output is correct
37 Correct 4 ms 6776 KB Output is correct
38 Correct 4 ms 6748 KB Output is correct
39 Correct 3 ms 6644 KB Output is correct
40 Correct 3 ms 6632 KB Output is correct
41 Correct 3 ms 6740 KB Output is correct
42 Correct 4 ms 6752 KB Output is correct
43 Correct 6 ms 6760 KB Output is correct
44 Correct 5 ms 6504 KB Output is correct
45 Correct 4 ms 6504 KB Output is correct
46 Correct 4 ms 6760 KB Output is correct
47 Correct 4 ms 6492 KB Output is correct
48 Correct 4 ms 6748 KB Output is correct
49 Correct 4 ms 6744 KB Output is correct
50 Correct 4 ms 6748 KB Output is correct
51 Correct 6 ms 6492 KB Output is correct
52 Correct 5 ms 6616 KB Output is correct
53 Correct 6 ms 6732 KB Output is correct
54 Correct 4 ms 6748 KB Output is correct
55 Correct 487 ms 28292 KB Output is correct
56 Correct 759 ms 41076 KB Output is correct
57 Correct 500 ms 30612 KB Output is correct
58 Correct 408 ms 26444 KB Output is correct
59 Correct 404 ms 27068 KB Output is correct
60 Correct 770 ms 26464 KB Output is correct
61 Correct 817 ms 26488 KB Output is correct
62 Correct 806 ms 27524 KB Output is correct
63 Correct 540 ms 28296 KB Output is correct
64 Correct 563 ms 28648 KB Output is correct
65 Correct 864 ms 22504 KB Output is correct
66 Correct 747 ms 26276 KB Output is correct
67 Correct 773 ms 28512 KB Output is correct
68 Correct 737 ms 27920 KB Output is correct
69 Correct 498 ms 28588 KB Output is correct
70 Correct 816 ms 40996 KB Output is correct
71 Correct 817 ms 31652 KB Output is correct
72 Correct 477 ms 26656 KB Output is correct
73 Correct 396 ms 26652 KB Output is correct
74 Correct 762 ms 26712 KB Output is correct
75 Correct 731 ms 27044 KB Output is correct
76 Correct 591 ms 27344 KB Output is correct
77 Correct 522 ms 28860 KB Output is correct
78 Correct 564 ms 27752 KB Output is correct
79 Correct 756 ms 22328 KB Output is correct
80 Correct 767 ms 26508 KB Output is correct
81 Correct 720 ms 29008 KB Output is correct
82 Correct 844 ms 27248 KB Output is correct
83 Correct 456 ms 26272 KB Output is correct
84 Correct 783 ms 41116 KB Output is correct
85 Correct 611 ms 30184 KB Output is correct
86 Correct 363 ms 26476 KB Output is correct
87 Correct 686 ms 26220 KB Output is correct
88 Correct 650 ms 26416 KB Output is correct
89 Correct 489 ms 26244 KB Output is correct
90 Correct 612 ms 26860 KB Output is correct
91 Correct 751 ms 26428 KB Output is correct
92 Correct 584 ms 26276 KB Output is correct
93 Correct 668 ms 26380 KB Output is correct
94 Correct 543 ms 29412 KB Output is correct
95 Correct 675 ms 31492 KB Output is correct
96 Correct 662 ms 30492 KB Output is correct
97 Correct 439 ms 26488 KB Output is correct
98 Correct 407 ms 26580 KB Output is correct
99 Correct 695 ms 28588 KB Output is correct
100 Correct 679 ms 29928 KB Output is correct
101 Correct 488 ms 29392 KB Output is correct
102 Correct 620 ms 28924 KB Output is correct
103 Correct 755 ms 27512 KB Output is correct
104 Correct 772 ms 27224 KB Output is correct
105 Correct 735 ms 28680 KB Output is correct
106 Correct 688 ms 29380 KB Output is correct
107 Correct 433 ms 31588 KB Output is correct
108 Correct 481 ms 28992 KB Output is correct
109 Correct 490 ms 29144 KB Output is correct
110 Correct 509 ms 28984 KB Output is correct
111 Correct 497 ms 29088 KB Output is correct
112 Correct 498 ms 29184 KB Output is correct
113 Correct 517 ms 28976 KB Output is correct
114 Correct 497 ms 28868 KB Output is correct
115 Correct 525 ms 29028 KB Output is correct
116 Correct 481 ms 29012 KB Output is correct