Submission #1102075

# Submission time Handle Problem Language Result Execution time Memory
1102075 2024-10-17T11:41:40 Z epicci23 Joker (BOI20_joker) C++17
100 / 100
252 ms 16968 KB
#include "bits/stdc++.h"
#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;

const int N = 2e5 + 5;

struct DSU{
 int n;
 stack<array<int,4>> st;
 vector<int> par,siz,col;
 bool ok=1;
 DSU(int _n){
  n=_n+5;
  par.assign(n,0);
  col.assign(n,0);
  iota(all(par),0);
  siz.assign(n,1);
 }
 inline int find(int a){
  while(par[a]!=a) a=par[a];
  return a;
 }
 inline int color(int a,int res=0){
  while(par[a]!=a){
    res^=col[a];
    a=par[a];
  }
  return res^col[a];
 }
 inline bool unite(int a,int b){
  int cola=color(a),colb=color(b);
  if(find(a)!=find(b)){
   a=find(a),b=find(b);
   if(siz[a]>siz[b]) swap(a,b);
   st.push({a,b,siz[a],col[a]});
   par[a]=b;
   col[a]=cola^colb^1;
   siz[b]+=siz[a];
   return true;
  }
  else{
    st.push({-1,-1,-1,ok});
    if(cola==colb) ok=0;
    return ok;
  }
 }
 inline void rollback(){
   if(st.empty()) return;
   array<int,4> c = st.top();
   st.pop();
   if(c[0]==-1) ok=c[3];
   else{
    par[c[0]]=c[0];
    col[c[0]]=c[3];
    siz[c[1]]-=c[2];
   }
 }
};

vector<array<int,2>> v;
vector<int> last;
int n,m,q;
DSU dsu(N);

void f(int l,int r,int optl,int optr){
  int mid=(l+r)/2,p=0;

  for(int i=l;i<mid;i++) p++,dsu.unite(v[i][0],v[i][1]);
  
  for(int i=optr;i>=mid;i--){
    p++;
    if(!dsu.unite(v[i][0],v[i][1])){
      last[mid]=i;
      break;
    } 
  }
  while(p>0){
    p--;
    dsu.rollback();
  }
  
  if(mid<r){
    for(int i=l;i<=mid;i++) p++,dsu.unite(v[i][0],v[i][1]);
    f(mid+1,r,max(mid+1,last[mid]),optr);
  }
  while(p>0){
    p--;
    dsu.rollback();
  }
 
  if(l<mid){
    for(int i=optr;i>last[mid];i--) p++,dsu.unite(v[i][0],v[i][1]);
    f(l,mid-1,optl,last[mid]);
  }
  while(p>0){
    p--;
    dsu.rollback();
  }
}

void _(){
  cin >> n >> m >> q;
  last.resize(m+5);
  v.resize(m+5);
  for(int i=1;i<=m;i++) cin >> v[i][0] >> v[i][1];
  
  int p=0,hm=1;
  for(int i=1;i<=m;i++){
    p++;
    if(!dsu.unite(v[i][0],v[i][1])) break;
    hm++;
  }
  while(p>0){
    p--;
    dsu.rollback();
  }  

  for(int i=hm+1;i<=m;i++) last[i]=m+1;
 
  if(hm<=m) f(1,hm,1,m);

  while(q--){
    int l,r;
    cin >> l >> r;
    if(last[l]<=r) cout << "NO\n";
    else cout << "YES\n";
  }
}

int32_t main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tc=1;//cin >> tc;
  while(tc--) _();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Correct 4 ms 4944 KB Output is correct
5 Correct 4 ms 5128 KB Output is correct
6 Correct 4 ms 4944 KB Output is correct
7 Correct 3 ms 4944 KB Output is correct
8 Correct 3 ms 4944 KB Output is correct
9 Correct 4 ms 4944 KB Output is correct
10 Correct 4 ms 4944 KB Output is correct
11 Correct 4 ms 4996 KB Output is correct
12 Correct 4 ms 4944 KB Output is correct
13 Correct 3 ms 5004 KB Output is correct
14 Correct 3 ms 4944 KB Output is correct
15 Correct 4 ms 4960 KB Output is correct
16 Correct 5 ms 5020 KB Output is correct
17 Correct 4 ms 4944 KB Output is correct
18 Correct 5 ms 5120 KB Output is correct
19 Correct 4 ms 5048 KB Output is correct
20 Correct 4 ms 4936 KB Output is correct
21 Correct 4 ms 4944 KB Output is correct
22 Correct 3 ms 4944 KB Output is correct
23 Correct 4 ms 4944 KB Output is correct
24 Correct 4 ms 4944 KB Output is correct
25 Correct 4 ms 5112 KB Output is correct
26 Correct 4 ms 4944 KB Output is correct
27 Correct 4 ms 5148 KB Output is correct
28 Correct 4 ms 4944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Correct 4 ms 4944 KB Output is correct
5 Correct 4 ms 5128 KB Output is correct
6 Correct 4 ms 4944 KB Output is correct
7 Correct 3 ms 4944 KB Output is correct
8 Correct 3 ms 4944 KB Output is correct
9 Correct 4 ms 4944 KB Output is correct
10 Correct 4 ms 4944 KB Output is correct
11 Correct 4 ms 4996 KB Output is correct
12 Correct 4 ms 4944 KB Output is correct
13 Correct 3 ms 5004 KB Output is correct
14 Correct 3 ms 4944 KB Output is correct
15 Correct 4 ms 4960 KB Output is correct
16 Correct 5 ms 5020 KB Output is correct
17 Correct 4 ms 4944 KB Output is correct
18 Correct 5 ms 5120 KB Output is correct
19 Correct 4 ms 5048 KB Output is correct
20 Correct 4 ms 4936 KB Output is correct
21 Correct 4 ms 4944 KB Output is correct
22 Correct 3 ms 4944 KB Output is correct
23 Correct 4 ms 4944 KB Output is correct
24 Correct 4 ms 4944 KB Output is correct
25 Correct 4 ms 5112 KB Output is correct
26 Correct 4 ms 4944 KB Output is correct
27 Correct 4 ms 5148 KB Output is correct
28 Correct 4 ms 4944 KB Output is correct
29 Correct 4 ms 5368 KB Output is correct
30 Correct 5 ms 5368 KB Output is correct
31 Correct 4 ms 5200 KB Output is correct
32 Correct 5 ms 5200 KB Output is correct
33 Correct 4 ms 5200 KB Output is correct
34 Correct 4 ms 5200 KB Output is correct
35 Correct 5 ms 5200 KB Output is correct
36 Correct 4 ms 5200 KB Output is correct
37 Correct 5 ms 5200 KB Output is correct
38 Correct 5 ms 5200 KB Output is correct
39 Correct 5 ms 5200 KB Output is correct
40 Correct 4 ms 5200 KB Output is correct
41 Correct 4 ms 5200 KB Output is correct
42 Correct 4 ms 5200 KB Output is correct
43 Correct 5 ms 5368 KB Output is correct
44 Correct 7 ms 5160 KB Output is correct
45 Correct 5 ms 5232 KB Output is correct
46 Correct 5 ms 5200 KB Output is correct
47 Correct 6 ms 5200 KB Output is correct
48 Correct 4 ms 5200 KB Output is correct
49 Correct 4 ms 5200 KB Output is correct
50 Correct 4 ms 5200 KB Output is correct
51 Correct 4 ms 5200 KB Output is correct
52 Correct 4 ms 5200 KB Output is correct
53 Correct 4 ms 5144 KB Output is correct
54 Correct 5 ms 5200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 135 ms 13152 KB Output is correct
4 Correct 54 ms 16968 KB Output is correct
5 Correct 166 ms 15740 KB Output is correct
6 Correct 113 ms 13712 KB Output is correct
7 Correct 112 ms 13128 KB Output is correct
8 Correct 145 ms 13704 KB Output is correct
9 Correct 167 ms 13668 KB Output is correct
10 Correct 200 ms 13756 KB Output is correct
11 Correct 149 ms 13636 KB Output is correct
12 Correct 170 ms 13692 KB Output is correct
13 Correct 160 ms 13668 KB Output is correct
14 Correct 154 ms 13688 KB Output is correct
15 Correct 194 ms 13612 KB Output is correct
16 Correct 221 ms 13824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Correct 4 ms 4944 KB Output is correct
5 Correct 4 ms 5128 KB Output is correct
6 Correct 4 ms 4944 KB Output is correct
7 Correct 3 ms 4944 KB Output is correct
8 Correct 3 ms 4944 KB Output is correct
9 Correct 4 ms 4944 KB Output is correct
10 Correct 4 ms 4944 KB Output is correct
11 Correct 4 ms 4996 KB Output is correct
12 Correct 4 ms 4944 KB Output is correct
13 Correct 3 ms 5004 KB Output is correct
14 Correct 3 ms 4944 KB Output is correct
15 Correct 4 ms 4960 KB Output is correct
16 Correct 5 ms 5020 KB Output is correct
17 Correct 4 ms 4944 KB Output is correct
18 Correct 5 ms 5120 KB Output is correct
19 Correct 4 ms 5048 KB Output is correct
20 Correct 4 ms 4936 KB Output is correct
21 Correct 4 ms 4944 KB Output is correct
22 Correct 3 ms 4944 KB Output is correct
23 Correct 4 ms 4944 KB Output is correct
24 Correct 4 ms 4944 KB Output is correct
25 Correct 4 ms 5112 KB Output is correct
26 Correct 4 ms 4944 KB Output is correct
27 Correct 4 ms 5148 KB Output is correct
28 Correct 4 ms 4944 KB Output is correct
29 Correct 135 ms 13152 KB Output is correct
30 Correct 54 ms 16968 KB Output is correct
31 Correct 166 ms 15740 KB Output is correct
32 Correct 113 ms 13712 KB Output is correct
33 Correct 112 ms 13128 KB Output is correct
34 Correct 145 ms 13704 KB Output is correct
35 Correct 167 ms 13668 KB Output is correct
36 Correct 200 ms 13756 KB Output is correct
37 Correct 149 ms 13636 KB Output is correct
38 Correct 170 ms 13692 KB Output is correct
39 Correct 160 ms 13668 KB Output is correct
40 Correct 154 ms 13688 KB Output is correct
41 Correct 194 ms 13612 KB Output is correct
42 Correct 221 ms 13824 KB Output is correct
43 Correct 140 ms 13080 KB Output is correct
44 Correct 57 ms 16764 KB Output is correct
45 Correct 252 ms 16356 KB Output is correct
46 Correct 113 ms 13592 KB Output is correct
47 Correct 117 ms 13752 KB Output is correct
48 Correct 170 ms 13800 KB Output is correct
49 Correct 211 ms 14132 KB Output is correct
50 Correct 155 ms 13584 KB Output is correct
51 Correct 159 ms 13572 KB Output is correct
52 Correct 172 ms 13696 KB Output is correct
53 Correct 153 ms 13676 KB Output is correct
54 Correct 177 ms 13784 KB Output is correct
55 Correct 187 ms 13752 KB Output is correct
56 Correct 224 ms 13880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Correct 4 ms 4944 KB Output is correct
5 Correct 4 ms 5128 KB Output is correct
6 Correct 4 ms 4944 KB Output is correct
7 Correct 3 ms 4944 KB Output is correct
8 Correct 3 ms 4944 KB Output is correct
9 Correct 4 ms 4944 KB Output is correct
10 Correct 4 ms 4944 KB Output is correct
11 Correct 4 ms 4996 KB Output is correct
12 Correct 4 ms 4944 KB Output is correct
13 Correct 3 ms 5004 KB Output is correct
14 Correct 3 ms 4944 KB Output is correct
15 Correct 4 ms 4960 KB Output is correct
16 Correct 5 ms 5020 KB Output is correct
17 Correct 4 ms 4944 KB Output is correct
18 Correct 5 ms 5120 KB Output is correct
19 Correct 4 ms 5048 KB Output is correct
20 Correct 4 ms 4936 KB Output is correct
21 Correct 4 ms 4944 KB Output is correct
22 Correct 3 ms 4944 KB Output is correct
23 Correct 4 ms 4944 KB Output is correct
24 Correct 4 ms 4944 KB Output is correct
25 Correct 4 ms 5112 KB Output is correct
26 Correct 4 ms 4944 KB Output is correct
27 Correct 4 ms 5148 KB Output is correct
28 Correct 4 ms 4944 KB Output is correct
29 Correct 4 ms 5368 KB Output is correct
30 Correct 5 ms 5368 KB Output is correct
31 Correct 4 ms 5200 KB Output is correct
32 Correct 5 ms 5200 KB Output is correct
33 Correct 4 ms 5200 KB Output is correct
34 Correct 4 ms 5200 KB Output is correct
35 Correct 5 ms 5200 KB Output is correct
36 Correct 4 ms 5200 KB Output is correct
37 Correct 5 ms 5200 KB Output is correct
38 Correct 5 ms 5200 KB Output is correct
39 Correct 5 ms 5200 KB Output is correct
40 Correct 4 ms 5200 KB Output is correct
41 Correct 4 ms 5200 KB Output is correct
42 Correct 4 ms 5200 KB Output is correct
43 Correct 5 ms 5368 KB Output is correct
44 Correct 7 ms 5160 KB Output is correct
45 Correct 5 ms 5232 KB Output is correct
46 Correct 5 ms 5200 KB Output is correct
47 Correct 6 ms 5200 KB Output is correct
48 Correct 4 ms 5200 KB Output is correct
49 Correct 4 ms 5200 KB Output is correct
50 Correct 4 ms 5200 KB Output is correct
51 Correct 4 ms 5200 KB Output is correct
52 Correct 4 ms 5200 KB Output is correct
53 Correct 4 ms 5144 KB Output is correct
54 Correct 5 ms 5200 KB Output is correct
55 Correct 120 ms 13140 KB Output is correct
56 Correct 39 ms 16200 KB Output is correct
57 Correct 137 ms 15728 KB Output is correct
58 Correct 96 ms 12920 KB Output is correct
59 Correct 158 ms 13128 KB Output is correct
60 Correct 191 ms 13116 KB Output is correct
61 Correct 148 ms 13072 KB Output is correct
62 Correct 157 ms 13384 KB Output is correct
63 Correct 132 ms 13136 KB Output is correct
64 Correct 176 ms 13128 KB Output is correct
65 Correct 177 ms 13128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4944 KB Output is correct
2 Correct 3 ms 4944 KB Output is correct
3 Correct 4 ms 4944 KB Output is correct
4 Correct 4 ms 4944 KB Output is correct
5 Correct 4 ms 5128 KB Output is correct
6 Correct 4 ms 4944 KB Output is correct
7 Correct 3 ms 4944 KB Output is correct
8 Correct 3 ms 4944 KB Output is correct
9 Correct 4 ms 4944 KB Output is correct
10 Correct 4 ms 4944 KB Output is correct
11 Correct 4 ms 4996 KB Output is correct
12 Correct 4 ms 4944 KB Output is correct
13 Correct 3 ms 5004 KB Output is correct
14 Correct 3 ms 4944 KB Output is correct
15 Correct 4 ms 4960 KB Output is correct
16 Correct 5 ms 5020 KB Output is correct
17 Correct 4 ms 4944 KB Output is correct
18 Correct 5 ms 5120 KB Output is correct
19 Correct 4 ms 5048 KB Output is correct
20 Correct 4 ms 4936 KB Output is correct
21 Correct 4 ms 4944 KB Output is correct
22 Correct 3 ms 4944 KB Output is correct
23 Correct 4 ms 4944 KB Output is correct
24 Correct 4 ms 4944 KB Output is correct
25 Correct 4 ms 5112 KB Output is correct
26 Correct 4 ms 4944 KB Output is correct
27 Correct 4 ms 5148 KB Output is correct
28 Correct 4 ms 4944 KB Output is correct
29 Correct 4 ms 5368 KB Output is correct
30 Correct 5 ms 5368 KB Output is correct
31 Correct 4 ms 5200 KB Output is correct
32 Correct 5 ms 5200 KB Output is correct
33 Correct 4 ms 5200 KB Output is correct
34 Correct 4 ms 5200 KB Output is correct
35 Correct 5 ms 5200 KB Output is correct
36 Correct 4 ms 5200 KB Output is correct
37 Correct 5 ms 5200 KB Output is correct
38 Correct 5 ms 5200 KB Output is correct
39 Correct 5 ms 5200 KB Output is correct
40 Correct 4 ms 5200 KB Output is correct
41 Correct 4 ms 5200 KB Output is correct
42 Correct 4 ms 5200 KB Output is correct
43 Correct 5 ms 5368 KB Output is correct
44 Correct 7 ms 5160 KB Output is correct
45 Correct 5 ms 5232 KB Output is correct
46 Correct 5 ms 5200 KB Output is correct
47 Correct 6 ms 5200 KB Output is correct
48 Correct 4 ms 5200 KB Output is correct
49 Correct 4 ms 5200 KB Output is correct
50 Correct 4 ms 5200 KB Output is correct
51 Correct 4 ms 5200 KB Output is correct
52 Correct 4 ms 5200 KB Output is correct
53 Correct 4 ms 5144 KB Output is correct
54 Correct 5 ms 5200 KB Output is correct
55 Correct 135 ms 13152 KB Output is correct
56 Correct 54 ms 16968 KB Output is correct
57 Correct 166 ms 15740 KB Output is correct
58 Correct 113 ms 13712 KB Output is correct
59 Correct 112 ms 13128 KB Output is correct
60 Correct 145 ms 13704 KB Output is correct
61 Correct 167 ms 13668 KB Output is correct
62 Correct 200 ms 13756 KB Output is correct
63 Correct 149 ms 13636 KB Output is correct
64 Correct 170 ms 13692 KB Output is correct
65 Correct 160 ms 13668 KB Output is correct
66 Correct 154 ms 13688 KB Output is correct
67 Correct 194 ms 13612 KB Output is correct
68 Correct 221 ms 13824 KB Output is correct
69 Correct 140 ms 13080 KB Output is correct
70 Correct 57 ms 16764 KB Output is correct
71 Correct 252 ms 16356 KB Output is correct
72 Correct 113 ms 13592 KB Output is correct
73 Correct 117 ms 13752 KB Output is correct
74 Correct 170 ms 13800 KB Output is correct
75 Correct 211 ms 14132 KB Output is correct
76 Correct 155 ms 13584 KB Output is correct
77 Correct 159 ms 13572 KB Output is correct
78 Correct 172 ms 13696 KB Output is correct
79 Correct 153 ms 13676 KB Output is correct
80 Correct 177 ms 13784 KB Output is correct
81 Correct 187 ms 13752 KB Output is correct
82 Correct 224 ms 13880 KB Output is correct
83 Correct 120 ms 13140 KB Output is correct
84 Correct 39 ms 16200 KB Output is correct
85 Correct 137 ms 15728 KB Output is correct
86 Correct 96 ms 12920 KB Output is correct
87 Correct 158 ms 13128 KB Output is correct
88 Correct 191 ms 13116 KB Output is correct
89 Correct 148 ms 13072 KB Output is correct
90 Correct 157 ms 13384 KB Output is correct
91 Correct 132 ms 13136 KB Output is correct
92 Correct 176 ms 13128 KB Output is correct
93 Correct 177 ms 13128 KB Output is correct
94 Correct 140 ms 13120 KB Output is correct
95 Correct 221 ms 16384 KB Output is correct
96 Correct 150 ms 15728 KB Output is correct
97 Correct 114 ms 13136 KB Output is correct
98 Correct 122 ms 13136 KB Output is correct
99 Correct 172 ms 13896 KB Output is correct
100 Correct 170 ms 14028 KB Output is correct
101 Correct 159 ms 13668 KB Output is correct
102 Correct 222 ms 13872 KB Output is correct
103 Correct 192 ms 13520 KB Output is correct
104 Correct 157 ms 13668 KB Output is correct
105 Correct 191 ms 13664 KB Output is correct
106 Correct 209 ms 13768 KB Output is correct
107 Correct 133 ms 16872 KB Output is correct
108 Correct 147 ms 13128 KB Output is correct
109 Correct 149 ms 13136 KB Output is correct
110 Correct 148 ms 13084 KB Output is correct
111 Correct 153 ms 13136 KB Output is correct
112 Correct 149 ms 13136 KB Output is correct
113 Correct 156 ms 13136 KB Output is correct
114 Correct 147 ms 13140 KB Output is correct
115 Correct 151 ms 13128 KB Output is correct
116 Correct 148 ms 13136 KB Output is correct