#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define all(x) (x).begin(),(x).end()
const int maxn=2e5+10;
vector<pi> edge(maxn);
vi head(maxn);
vi col(maxn,1);
vi depth(maxn,1);
vector<vi> changes;
bool is_bipartite=1;
pi get(int a) {
if (a==head[a]) {
return {a,col[a]};
}
else {
pi temp=get(head[a]);
temp.se^=col[a];
return temp;
}
}
void unite(int a, int b) {
int cola,colb;
tie(a,cola)=get(a);
tie(b,colb)=get(b);
if (a==b) {
if (cola==colb) {
is_bipartite=0;
}
return;
}
if (depth[a]<depth[b]) {
swap(a,b);
}
changes.pb({b,head[b],col[b],a,int(depth[a]==depth[b])});
head[b]=a;
col[b]=cola^colb^1;
depth[a]+=int(depth[a]==depth[b]);
}
void rollback(int k) {
while (changes.size()>k) {
vi t=changes.back();
changes.pop_back();
head[t[0]]=t[1];
col[t[0]]=t[2];
depth[t[3]]-=t[4];
}
}
int n,m,q;
vector<bool> ans(maxn);
vector<vector<vi>> queries(maxn);
vi mn(4*maxn,1e9),mx(4*maxn,-1e9);
void add(int l, int r, int i, int v=1, int tl=0, int tr=m-1) {
mn[v]=min(mn[v],l);
mx[v]=max(mx[v],r);
if (tl==tr) {
queries[tl].pb({l,r,i});
}
else {
int tm=tl+(tr-tl)/2;
if (l<=tm) {
add(l,r,i,v*2,tl,tm);
}
else {
add(l,r,i,v*2+1,tm+1,tr);
}
}
}
void dfs(int l_added=0, int r_added=m-1, int v=1, int tl=0, int tr=m-1) {
if (mn[v]==1e9) {
return;
}
bool temp_is_bip=is_bipartite;
int temp_sze=changes.size();
while (l_added<mn[v]) {
unite(edge[l_added].fi,edge[l_added].se);
l_added++;
if (!is_bipartite) {
l_added=m;
r_added=-1;
}
}
while (r_added>mx[v]) {
unite(edge[r_added].fi,edge[r_added].se);
r_added--;
if (!is_bipartite) {
l_added=m;
r_added=-1;
}
}
if (tl==tr) {
sort(all(queries[tl]),[](vi a, vi b){return a[1]>b[1];});
for (auto temp:queries[tl]) {
while (r_added>temp[1]) {
unite(edge[r_added].fi,edge[r_added].se);
r_added--;
if (!is_bipartite) {
r_added=-1;
}
}
ans[temp[2]]=is_bipartite;
}
}
else {
int tm=tl+(tr-tl)/2;
dfs(l_added,r_added,v*2,tl,tm);
dfs(l_added,r_added,v*2+1,tm+1,tr);
}
is_bipartite=temp_is_bip;
rollback(temp_sze);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
iota(all(head),0);
for (int i=0; i<m; i++) {
cin >> edge[i].fi >> edge[i].se;
edge[i].fi--;
edge[i].se--;
}
int l,r;
for (int i=0; i<q; i++) {
cin >> l >> r;
add(l-1,r-1,i);
}
dfs();
for (int i=0; i<q; i++) {
cout << (ans[i]?"NO\n":"YES\n");
}
return 0;
}
Compilation message
Joker.cpp: In function 'void rollback(int)':
Joker.cpp:51:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | while (changes.size()>k) {
| ~~~~~~~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15196 KB |
Output is correct |
2 |
Correct |
7 ms |
15196 KB |
Output is correct |
3 |
Correct |
7 ms |
15172 KB |
Output is correct |
4 |
Correct |
7 ms |
15196 KB |
Output is correct |
5 |
Correct |
7 ms |
15196 KB |
Output is correct |
6 |
Correct |
7 ms |
15196 KB |
Output is correct |
7 |
Correct |
7 ms |
15196 KB |
Output is correct |
8 |
Correct |
7 ms |
15196 KB |
Output is correct |
9 |
Correct |
7 ms |
15196 KB |
Output is correct |
10 |
Correct |
9 ms |
15168 KB |
Output is correct |
11 |
Correct |
9 ms |
15196 KB |
Output is correct |
12 |
Correct |
8 ms |
15196 KB |
Output is correct |
13 |
Correct |
7 ms |
15384 KB |
Output is correct |
14 |
Correct |
7 ms |
15196 KB |
Output is correct |
15 |
Correct |
8 ms |
15192 KB |
Output is correct |
16 |
Correct |
6 ms |
15196 KB |
Output is correct |
17 |
Correct |
6 ms |
15196 KB |
Output is correct |
18 |
Correct |
7 ms |
15196 KB |
Output is correct |
19 |
Correct |
7 ms |
15388 KB |
Output is correct |
20 |
Correct |
6 ms |
15196 KB |
Output is correct |
21 |
Correct |
6 ms |
15196 KB |
Output is correct |
22 |
Correct |
6 ms |
15192 KB |
Output is correct |
23 |
Correct |
7 ms |
15196 KB |
Output is correct |
24 |
Correct |
7 ms |
15196 KB |
Output is correct |
25 |
Correct |
7 ms |
15384 KB |
Output is correct |
26 |
Correct |
7 ms |
15196 KB |
Output is correct |
27 |
Correct |
7 ms |
15196 KB |
Output is correct |
28 |
Correct |
8 ms |
15264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15196 KB |
Output is correct |
2 |
Correct |
7 ms |
15196 KB |
Output is correct |
3 |
Correct |
7 ms |
15172 KB |
Output is correct |
4 |
Correct |
7 ms |
15196 KB |
Output is correct |
5 |
Correct |
7 ms |
15196 KB |
Output is correct |
6 |
Correct |
7 ms |
15196 KB |
Output is correct |
7 |
Correct |
7 ms |
15196 KB |
Output is correct |
8 |
Correct |
7 ms |
15196 KB |
Output is correct |
9 |
Correct |
7 ms |
15196 KB |
Output is correct |
10 |
Correct |
9 ms |
15168 KB |
Output is correct |
11 |
Correct |
9 ms |
15196 KB |
Output is correct |
12 |
Correct |
8 ms |
15196 KB |
Output is correct |
13 |
Correct |
7 ms |
15384 KB |
Output is correct |
14 |
Correct |
7 ms |
15196 KB |
Output is correct |
15 |
Correct |
8 ms |
15192 KB |
Output is correct |
16 |
Correct |
6 ms |
15196 KB |
Output is correct |
17 |
Correct |
6 ms |
15196 KB |
Output is correct |
18 |
Correct |
7 ms |
15196 KB |
Output is correct |
19 |
Correct |
7 ms |
15388 KB |
Output is correct |
20 |
Correct |
6 ms |
15196 KB |
Output is correct |
21 |
Correct |
6 ms |
15196 KB |
Output is correct |
22 |
Correct |
6 ms |
15192 KB |
Output is correct |
23 |
Correct |
7 ms |
15196 KB |
Output is correct |
24 |
Correct |
7 ms |
15196 KB |
Output is correct |
25 |
Correct |
7 ms |
15384 KB |
Output is correct |
26 |
Correct |
7 ms |
15196 KB |
Output is correct |
27 |
Correct |
7 ms |
15196 KB |
Output is correct |
28 |
Correct |
8 ms |
15264 KB |
Output is correct |
29 |
Correct |
22 ms |
15452 KB |
Output is correct |
30 |
Correct |
14 ms |
15452 KB |
Output is correct |
31 |
Correct |
11 ms |
15596 KB |
Output is correct |
32 |
Correct |
9 ms |
15480 KB |
Output is correct |
33 |
Correct |
9 ms |
15452 KB |
Output is correct |
34 |
Correct |
11 ms |
15452 KB |
Output is correct |
35 |
Correct |
24 ms |
15628 KB |
Output is correct |
36 |
Correct |
23 ms |
15452 KB |
Output is correct |
37 |
Correct |
27 ms |
15452 KB |
Output is correct |
38 |
Correct |
38 ms |
15520 KB |
Output is correct |
39 |
Correct |
9 ms |
15412 KB |
Output is correct |
40 |
Correct |
10 ms |
15416 KB |
Output is correct |
41 |
Correct |
10 ms |
15384 KB |
Output is correct |
42 |
Correct |
9 ms |
15396 KB |
Output is correct |
43 |
Correct |
8 ms |
15452 KB |
Output is correct |
44 |
Correct |
9 ms |
15520 KB |
Output is correct |
45 |
Correct |
16 ms |
15480 KB |
Output is correct |
46 |
Correct |
14 ms |
15452 KB |
Output is correct |
47 |
Correct |
11 ms |
15452 KB |
Output is correct |
48 |
Correct |
10 ms |
15452 KB |
Output is correct |
49 |
Correct |
11 ms |
15600 KB |
Output is correct |
50 |
Correct |
14 ms |
15412 KB |
Output is correct |
51 |
Correct |
9 ms |
15452 KB |
Output is correct |
52 |
Correct |
13 ms |
15600 KB |
Output is correct |
53 |
Correct |
11 ms |
15500 KB |
Output is correct |
54 |
Correct |
14 ms |
15628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15196 KB |
Output is correct |
2 |
Correct |
7 ms |
15196 KB |
Output is correct |
3 |
Correct |
237 ms |
36856 KB |
Output is correct |
4 |
Correct |
235 ms |
42260 KB |
Output is correct |
5 |
Correct |
239 ms |
32528 KB |
Output is correct |
6 |
Correct |
240 ms |
36668 KB |
Output is correct |
7 |
Correct |
218 ms |
36852 KB |
Output is correct |
8 |
Correct |
238 ms |
33528 KB |
Output is correct |
9 |
Correct |
233 ms |
35852 KB |
Output is correct |
10 |
Correct |
211 ms |
36384 KB |
Output is correct |
11 |
Correct |
229 ms |
36508 KB |
Output is correct |
12 |
Correct |
242 ms |
36848 KB |
Output is correct |
13 |
Correct |
239 ms |
32072 KB |
Output is correct |
14 |
Correct |
214 ms |
34076 KB |
Output is correct |
15 |
Correct |
247 ms |
36648 KB |
Output is correct |
16 |
Correct |
227 ms |
36948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15196 KB |
Output is correct |
2 |
Correct |
7 ms |
15196 KB |
Output is correct |
3 |
Correct |
7 ms |
15172 KB |
Output is correct |
4 |
Correct |
7 ms |
15196 KB |
Output is correct |
5 |
Correct |
7 ms |
15196 KB |
Output is correct |
6 |
Correct |
7 ms |
15196 KB |
Output is correct |
7 |
Correct |
7 ms |
15196 KB |
Output is correct |
8 |
Correct |
7 ms |
15196 KB |
Output is correct |
9 |
Correct |
7 ms |
15196 KB |
Output is correct |
10 |
Correct |
9 ms |
15168 KB |
Output is correct |
11 |
Correct |
9 ms |
15196 KB |
Output is correct |
12 |
Correct |
8 ms |
15196 KB |
Output is correct |
13 |
Correct |
7 ms |
15384 KB |
Output is correct |
14 |
Correct |
7 ms |
15196 KB |
Output is correct |
15 |
Correct |
8 ms |
15192 KB |
Output is correct |
16 |
Correct |
6 ms |
15196 KB |
Output is correct |
17 |
Correct |
6 ms |
15196 KB |
Output is correct |
18 |
Correct |
7 ms |
15196 KB |
Output is correct |
19 |
Correct |
7 ms |
15388 KB |
Output is correct |
20 |
Correct |
6 ms |
15196 KB |
Output is correct |
21 |
Correct |
6 ms |
15196 KB |
Output is correct |
22 |
Correct |
6 ms |
15192 KB |
Output is correct |
23 |
Correct |
7 ms |
15196 KB |
Output is correct |
24 |
Correct |
7 ms |
15196 KB |
Output is correct |
25 |
Correct |
7 ms |
15384 KB |
Output is correct |
26 |
Correct |
7 ms |
15196 KB |
Output is correct |
27 |
Correct |
7 ms |
15196 KB |
Output is correct |
28 |
Correct |
8 ms |
15264 KB |
Output is correct |
29 |
Correct |
237 ms |
36856 KB |
Output is correct |
30 |
Correct |
235 ms |
42260 KB |
Output is correct |
31 |
Correct |
239 ms |
32528 KB |
Output is correct |
32 |
Correct |
240 ms |
36668 KB |
Output is correct |
33 |
Correct |
218 ms |
36852 KB |
Output is correct |
34 |
Correct |
238 ms |
33528 KB |
Output is correct |
35 |
Correct |
233 ms |
35852 KB |
Output is correct |
36 |
Correct |
211 ms |
36384 KB |
Output is correct |
37 |
Correct |
229 ms |
36508 KB |
Output is correct |
38 |
Correct |
242 ms |
36848 KB |
Output is correct |
39 |
Correct |
239 ms |
32072 KB |
Output is correct |
40 |
Correct |
214 ms |
34076 KB |
Output is correct |
41 |
Correct |
247 ms |
36648 KB |
Output is correct |
42 |
Correct |
227 ms |
36948 KB |
Output is correct |
43 |
Correct |
1523 ms |
37060 KB |
Output is correct |
44 |
Execution timed out |
2067 ms |
41716 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15196 KB |
Output is correct |
2 |
Correct |
7 ms |
15196 KB |
Output is correct |
3 |
Correct |
7 ms |
15172 KB |
Output is correct |
4 |
Correct |
7 ms |
15196 KB |
Output is correct |
5 |
Correct |
7 ms |
15196 KB |
Output is correct |
6 |
Correct |
7 ms |
15196 KB |
Output is correct |
7 |
Correct |
7 ms |
15196 KB |
Output is correct |
8 |
Correct |
7 ms |
15196 KB |
Output is correct |
9 |
Correct |
7 ms |
15196 KB |
Output is correct |
10 |
Correct |
9 ms |
15168 KB |
Output is correct |
11 |
Correct |
9 ms |
15196 KB |
Output is correct |
12 |
Correct |
8 ms |
15196 KB |
Output is correct |
13 |
Correct |
7 ms |
15384 KB |
Output is correct |
14 |
Correct |
7 ms |
15196 KB |
Output is correct |
15 |
Correct |
8 ms |
15192 KB |
Output is correct |
16 |
Correct |
6 ms |
15196 KB |
Output is correct |
17 |
Correct |
6 ms |
15196 KB |
Output is correct |
18 |
Correct |
7 ms |
15196 KB |
Output is correct |
19 |
Correct |
7 ms |
15388 KB |
Output is correct |
20 |
Correct |
6 ms |
15196 KB |
Output is correct |
21 |
Correct |
6 ms |
15196 KB |
Output is correct |
22 |
Correct |
6 ms |
15192 KB |
Output is correct |
23 |
Correct |
7 ms |
15196 KB |
Output is correct |
24 |
Correct |
7 ms |
15196 KB |
Output is correct |
25 |
Correct |
7 ms |
15384 KB |
Output is correct |
26 |
Correct |
7 ms |
15196 KB |
Output is correct |
27 |
Correct |
7 ms |
15196 KB |
Output is correct |
28 |
Correct |
8 ms |
15264 KB |
Output is correct |
29 |
Correct |
22 ms |
15452 KB |
Output is correct |
30 |
Correct |
14 ms |
15452 KB |
Output is correct |
31 |
Correct |
11 ms |
15596 KB |
Output is correct |
32 |
Correct |
9 ms |
15480 KB |
Output is correct |
33 |
Correct |
9 ms |
15452 KB |
Output is correct |
34 |
Correct |
11 ms |
15452 KB |
Output is correct |
35 |
Correct |
24 ms |
15628 KB |
Output is correct |
36 |
Correct |
23 ms |
15452 KB |
Output is correct |
37 |
Correct |
27 ms |
15452 KB |
Output is correct |
38 |
Correct |
38 ms |
15520 KB |
Output is correct |
39 |
Correct |
9 ms |
15412 KB |
Output is correct |
40 |
Correct |
10 ms |
15416 KB |
Output is correct |
41 |
Correct |
10 ms |
15384 KB |
Output is correct |
42 |
Correct |
9 ms |
15396 KB |
Output is correct |
43 |
Correct |
8 ms |
15452 KB |
Output is correct |
44 |
Correct |
9 ms |
15520 KB |
Output is correct |
45 |
Correct |
16 ms |
15480 KB |
Output is correct |
46 |
Correct |
14 ms |
15452 KB |
Output is correct |
47 |
Correct |
11 ms |
15452 KB |
Output is correct |
48 |
Correct |
10 ms |
15452 KB |
Output is correct |
49 |
Correct |
11 ms |
15600 KB |
Output is correct |
50 |
Correct |
14 ms |
15412 KB |
Output is correct |
51 |
Correct |
9 ms |
15452 KB |
Output is correct |
52 |
Correct |
13 ms |
15600 KB |
Output is correct |
53 |
Correct |
11 ms |
15500 KB |
Output is correct |
54 |
Correct |
14 ms |
15628 KB |
Output is correct |
55 |
Correct |
690 ms |
23304 KB |
Output is correct |
56 |
Execution timed out |
2025 ms |
28928 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15196 KB |
Output is correct |
2 |
Correct |
7 ms |
15196 KB |
Output is correct |
3 |
Correct |
7 ms |
15172 KB |
Output is correct |
4 |
Correct |
7 ms |
15196 KB |
Output is correct |
5 |
Correct |
7 ms |
15196 KB |
Output is correct |
6 |
Correct |
7 ms |
15196 KB |
Output is correct |
7 |
Correct |
7 ms |
15196 KB |
Output is correct |
8 |
Correct |
7 ms |
15196 KB |
Output is correct |
9 |
Correct |
7 ms |
15196 KB |
Output is correct |
10 |
Correct |
9 ms |
15168 KB |
Output is correct |
11 |
Correct |
9 ms |
15196 KB |
Output is correct |
12 |
Correct |
8 ms |
15196 KB |
Output is correct |
13 |
Correct |
7 ms |
15384 KB |
Output is correct |
14 |
Correct |
7 ms |
15196 KB |
Output is correct |
15 |
Correct |
8 ms |
15192 KB |
Output is correct |
16 |
Correct |
6 ms |
15196 KB |
Output is correct |
17 |
Correct |
6 ms |
15196 KB |
Output is correct |
18 |
Correct |
7 ms |
15196 KB |
Output is correct |
19 |
Correct |
7 ms |
15388 KB |
Output is correct |
20 |
Correct |
6 ms |
15196 KB |
Output is correct |
21 |
Correct |
6 ms |
15196 KB |
Output is correct |
22 |
Correct |
6 ms |
15192 KB |
Output is correct |
23 |
Correct |
7 ms |
15196 KB |
Output is correct |
24 |
Correct |
7 ms |
15196 KB |
Output is correct |
25 |
Correct |
7 ms |
15384 KB |
Output is correct |
26 |
Correct |
7 ms |
15196 KB |
Output is correct |
27 |
Correct |
7 ms |
15196 KB |
Output is correct |
28 |
Correct |
8 ms |
15264 KB |
Output is correct |
29 |
Correct |
22 ms |
15452 KB |
Output is correct |
30 |
Correct |
14 ms |
15452 KB |
Output is correct |
31 |
Correct |
11 ms |
15596 KB |
Output is correct |
32 |
Correct |
9 ms |
15480 KB |
Output is correct |
33 |
Correct |
9 ms |
15452 KB |
Output is correct |
34 |
Correct |
11 ms |
15452 KB |
Output is correct |
35 |
Correct |
24 ms |
15628 KB |
Output is correct |
36 |
Correct |
23 ms |
15452 KB |
Output is correct |
37 |
Correct |
27 ms |
15452 KB |
Output is correct |
38 |
Correct |
38 ms |
15520 KB |
Output is correct |
39 |
Correct |
9 ms |
15412 KB |
Output is correct |
40 |
Correct |
10 ms |
15416 KB |
Output is correct |
41 |
Correct |
10 ms |
15384 KB |
Output is correct |
42 |
Correct |
9 ms |
15396 KB |
Output is correct |
43 |
Correct |
8 ms |
15452 KB |
Output is correct |
44 |
Correct |
9 ms |
15520 KB |
Output is correct |
45 |
Correct |
16 ms |
15480 KB |
Output is correct |
46 |
Correct |
14 ms |
15452 KB |
Output is correct |
47 |
Correct |
11 ms |
15452 KB |
Output is correct |
48 |
Correct |
10 ms |
15452 KB |
Output is correct |
49 |
Correct |
11 ms |
15600 KB |
Output is correct |
50 |
Correct |
14 ms |
15412 KB |
Output is correct |
51 |
Correct |
9 ms |
15452 KB |
Output is correct |
52 |
Correct |
13 ms |
15600 KB |
Output is correct |
53 |
Correct |
11 ms |
15500 KB |
Output is correct |
54 |
Correct |
14 ms |
15628 KB |
Output is correct |
55 |
Correct |
237 ms |
36856 KB |
Output is correct |
56 |
Correct |
235 ms |
42260 KB |
Output is correct |
57 |
Correct |
239 ms |
32528 KB |
Output is correct |
58 |
Correct |
240 ms |
36668 KB |
Output is correct |
59 |
Correct |
218 ms |
36852 KB |
Output is correct |
60 |
Correct |
238 ms |
33528 KB |
Output is correct |
61 |
Correct |
233 ms |
35852 KB |
Output is correct |
62 |
Correct |
211 ms |
36384 KB |
Output is correct |
63 |
Correct |
229 ms |
36508 KB |
Output is correct |
64 |
Correct |
242 ms |
36848 KB |
Output is correct |
65 |
Correct |
239 ms |
32072 KB |
Output is correct |
66 |
Correct |
214 ms |
34076 KB |
Output is correct |
67 |
Correct |
247 ms |
36648 KB |
Output is correct |
68 |
Correct |
227 ms |
36948 KB |
Output is correct |
69 |
Correct |
1523 ms |
37060 KB |
Output is correct |
70 |
Execution timed out |
2067 ms |
41716 KB |
Time limit exceeded |
71 |
Halted |
0 ms |
0 KB |
- |