#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(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();
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);
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:25:15: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
25 | const int INF=1e18;
| ^~~~
Joker.cpp: In function 'void undo()':
Joker.cpp:61:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | auto [a,sza,b]=state.top();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6900 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6900 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
3 ms |
6492 KB |
Output is correct |
31 |
Correct |
3 ms |
6492 KB |
Output is correct |
32 |
Correct |
3 ms |
6492 KB |
Output is correct |
33 |
Correct |
3 ms |
6492 KB |
Output is correct |
34 |
Correct |
3 ms |
6492 KB |
Output is correct |
35 |
Correct |
3 ms |
6672 KB |
Output is correct |
36 |
Correct |
3 ms |
6492 KB |
Output is correct |
37 |
Correct |
3 ms |
6488 KB |
Output is correct |
38 |
Correct |
3 ms |
6748 KB |
Output is correct |
39 |
Correct |
3 ms |
6492 KB |
Output is correct |
40 |
Correct |
3 ms |
6492 KB |
Output is correct |
41 |
Correct |
3 ms |
6492 KB |
Output is correct |
42 |
Correct |
3 ms |
6492 KB |
Output is correct |
43 |
Correct |
13 ms |
6944 KB |
Output is correct |
44 |
Correct |
7 ms |
6492 KB |
Output is correct |
45 |
Correct |
3 ms |
6492 KB |
Output is correct |
46 |
Correct |
4 ms |
6492 KB |
Output is correct |
47 |
Correct |
4 ms |
6492 KB |
Output is correct |
48 |
Correct |
3 ms |
6492 KB |
Output is correct |
49 |
Correct |
4 ms |
6680 KB |
Output is correct |
50 |
Correct |
4 ms |
6492 KB |
Output is correct |
51 |
Correct |
11 ms |
6488 KB |
Output is correct |
52 |
Correct |
7 ms |
6488 KB |
Output is correct |
53 |
Correct |
4 ms |
6492 KB |
Output is correct |
54 |
Correct |
3 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
374 ms |
12376 KB |
Output is correct |
4 |
Correct |
463 ms |
19004 KB |
Output is correct |
5 |
Correct |
305 ms |
13644 KB |
Output is correct |
6 |
Correct |
276 ms |
12064 KB |
Output is correct |
7 |
Correct |
275 ms |
12032 KB |
Output is correct |
8 |
Execution timed out |
2043 ms |
10832 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6900 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
374 ms |
12376 KB |
Output is correct |
30 |
Correct |
463 ms |
19004 KB |
Output is correct |
31 |
Correct |
305 ms |
13644 KB |
Output is correct |
32 |
Correct |
276 ms |
12064 KB |
Output is correct |
33 |
Correct |
275 ms |
12032 KB |
Output is correct |
34 |
Execution timed out |
2043 ms |
10832 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6900 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
3 ms |
6492 KB |
Output is correct |
31 |
Correct |
3 ms |
6492 KB |
Output is correct |
32 |
Correct |
3 ms |
6492 KB |
Output is correct |
33 |
Correct |
3 ms |
6492 KB |
Output is correct |
34 |
Correct |
3 ms |
6492 KB |
Output is correct |
35 |
Correct |
3 ms |
6672 KB |
Output is correct |
36 |
Correct |
3 ms |
6492 KB |
Output is correct |
37 |
Correct |
3 ms |
6488 KB |
Output is correct |
38 |
Correct |
3 ms |
6748 KB |
Output is correct |
39 |
Correct |
3 ms |
6492 KB |
Output is correct |
40 |
Correct |
3 ms |
6492 KB |
Output is correct |
41 |
Correct |
3 ms |
6492 KB |
Output is correct |
42 |
Correct |
3 ms |
6492 KB |
Output is correct |
43 |
Correct |
13 ms |
6944 KB |
Output is correct |
44 |
Correct |
7 ms |
6492 KB |
Output is correct |
45 |
Correct |
3 ms |
6492 KB |
Output is correct |
46 |
Correct |
4 ms |
6492 KB |
Output is correct |
47 |
Correct |
4 ms |
6492 KB |
Output is correct |
48 |
Correct |
3 ms |
6492 KB |
Output is correct |
49 |
Correct |
4 ms |
6680 KB |
Output is correct |
50 |
Correct |
4 ms |
6492 KB |
Output is correct |
51 |
Correct |
11 ms |
6488 KB |
Output is correct |
52 |
Correct |
7 ms |
6488 KB |
Output is correct |
53 |
Correct |
4 ms |
6492 KB |
Output is correct |
54 |
Correct |
3 ms |
6492 KB |
Output is correct |
55 |
Correct |
359 ms |
12404 KB |
Output is correct |
56 |
Correct |
417 ms |
18912 KB |
Output is correct |
57 |
Correct |
382 ms |
13848 KB |
Output is correct |
58 |
Correct |
262 ms |
11860 KB |
Output is correct |
59 |
Execution timed out |
2065 ms |
11640 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6900 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
3 ms |
6492 KB |
Output is correct |
31 |
Correct |
3 ms |
6492 KB |
Output is correct |
32 |
Correct |
3 ms |
6492 KB |
Output is correct |
33 |
Correct |
3 ms |
6492 KB |
Output is correct |
34 |
Correct |
3 ms |
6492 KB |
Output is correct |
35 |
Correct |
3 ms |
6672 KB |
Output is correct |
36 |
Correct |
3 ms |
6492 KB |
Output is correct |
37 |
Correct |
3 ms |
6488 KB |
Output is correct |
38 |
Correct |
3 ms |
6748 KB |
Output is correct |
39 |
Correct |
3 ms |
6492 KB |
Output is correct |
40 |
Correct |
3 ms |
6492 KB |
Output is correct |
41 |
Correct |
3 ms |
6492 KB |
Output is correct |
42 |
Correct |
3 ms |
6492 KB |
Output is correct |
43 |
Correct |
13 ms |
6944 KB |
Output is correct |
44 |
Correct |
7 ms |
6492 KB |
Output is correct |
45 |
Correct |
3 ms |
6492 KB |
Output is correct |
46 |
Correct |
4 ms |
6492 KB |
Output is correct |
47 |
Correct |
4 ms |
6492 KB |
Output is correct |
48 |
Correct |
3 ms |
6492 KB |
Output is correct |
49 |
Correct |
4 ms |
6680 KB |
Output is correct |
50 |
Correct |
4 ms |
6492 KB |
Output is correct |
51 |
Correct |
11 ms |
6488 KB |
Output is correct |
52 |
Correct |
7 ms |
6488 KB |
Output is correct |
53 |
Correct |
4 ms |
6492 KB |
Output is correct |
54 |
Correct |
3 ms |
6492 KB |
Output is correct |
55 |
Correct |
374 ms |
12376 KB |
Output is correct |
56 |
Correct |
463 ms |
19004 KB |
Output is correct |
57 |
Correct |
305 ms |
13644 KB |
Output is correct |
58 |
Correct |
276 ms |
12064 KB |
Output is correct |
59 |
Correct |
275 ms |
12032 KB |
Output is correct |
60 |
Execution timed out |
2043 ms |
10832 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |