Submission #1083607

# Submission time Handle Problem Language Result Execution time Memory
1083607 2024-09-03T14:58:18 Z VinhLuu Joker (BOI20_joker) C++17
100 / 100
240 ms 25148 KB
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
//#define pb push_back
#define all(vin) vin.begin(), vin.end()

using namespace std;

typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 2e5 + 5;
const int oo = 1e16;

int n, a[N], b[N], m, q;
vector<int> le[N], ri[N];

int pa[N], sz[N], val[N], cnt[N], e, ptl, ptr, w[N];
stack<tuple<int,int,int,int,int>> st;

int fin(int u){
  return pa[u] == u ? u : fin(pa[u]);
}

int cal(int u){
  return (pa[u] == u ? val[u] : val[u] ^ cal(pa[u]));
}

bool dsu(int u,int v){
  int x = fin(u);
  int y = fin(v);
//  cerr << u << " " << v << " " << x << " " << y << " " << cal(u) << " " << cal(v) << " g\n";
  if(x == y){
    if(cal(u) == cal(v)){
      st.push({0, 0, 0, 0, 0});
//      cerr << "                                     push" << 0 << "\n";
      e++;
      return 1;
    }
    return 0;
  }
  if(sz[x] < sz[y]) swap(x, y), swap(u, v);
  st.push({x, sz[x], y, pa[y], val[y]});
//  cerr << "                                     push" << x << " " << sz[x] << " " << y << " " << pa[y] << " " << (val[u] == val[v] ? 1 : 0) << "\n";
  sz[x] += sz[y];
  pa[y] = x;
  if(cal(u) == cal(v)) val[y] ^= 1;

  return 1;
}

void roll_back(){
  int x, sx, y, py, vly; tie(x, sx, y, py, vly) = st.top(); st.pop();
//  cerr << "                                     pop\n";
  if(!x){
    e--;
    return;
  }
  sz[x] = sx, pa[y] = py;
  val[y] = vly;
}

int f[N];

void solve(int l,int r,int pl,int pr){
  int mid = (l + r) / 2;
//  cerr << "                                              " << mid << " " << pl << " " << pr << " " << ptl << " " << ptr << " y\n";

  while(ptr > mid + 1){
    ptr--;
    cnt[ptr] += dsu(a[ptr], b[ptr]);
  }

  int mrk = ptl;
  if(e) f[mid] = -1;
  else f[mid] = 0;

  if(!e){
    for(int i = pl; i <= min(mid - 1, pr); i ++){
      while(ptl < i){
        ptl++;
        cnt[ptl] += dsu(a[ptl], b[ptl]);
      }
      if(e){
        f[mid] = i;
        break;
      }
    }
  }
  int nxt = mid - 1;
  if(f[mid] == -1) nxt = 1;
  else if(f[mid] == 0) nxt = max(1, mid - 1);
  else nxt = f[mid];

//  cerr << mid << " " << f[mid] << " " << nxt << " yyy\n";

  while(ptl >= pl){
    if(cnt[ptl]) cnt[ptl]--, roll_back();
    ptl--;
  }
  while(ptr > mid){
    ptr--;
    cnt[ptr] += dsu(a[ptr], b[ptr]);
  }
  if(l <= mid - 1) solve(l, mid - 1, pl, nxt);

  while(ptl >= pl){
    if(cnt[ptl]) cnt[ptl]--, roll_back();
    ptl--;
  }

  while(ptr <= r){
    if(cnt[ptr]) cnt[ptr]--, roll_back();
    ptr++;
  }
  while(ptl < nxt - 1){
    ptl++;
    cnt[ptl] += dsu(a[ptl], b[ptl]);
  }
  if(mid + 1 <= r) solve(mid + 1, r, nxt, pr);

  while(ptl >= pl){
    if(cnt[ptl]) cnt[ptl]--, roll_back();
    ptl--;
  }
  while(ptr <= r){
    if(cnt[ptr]) cnt[ptr]--, roll_back();
    ptr++;
  }
}

signed main(){
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #define task "v"
  if(fopen(task ".inp","r")){
    freopen(task ".inp","r",stdin);
    freopen(task ".out","w",stdout);
  }
  cin >> n >> m >> q;
  for(int i = 1; i <= n; i ++) pa[i] = i, sz[i] = 1;
  for(int i = 1; i <= m; i ++){
    cin >> a[i] >> b[i];
  }
  ptl = 0, ptr = m + 1;
  solve(1, m, 1, m);

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

Compilation message

Joker.cpp:14:16: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+16' to '2147483647' [-Woverflow]
   14 | const int oo = 1e16;
      |                ^~~~
Joker.cpp: In function 'void solve(int, int, int, int)':
Joker.cpp:75:7: warning: unused variable 'mrk' [-Wunused-variable]
   75 |   int mrk = ptl;
      |       ^~~
Joker.cpp: In function 'int main()':
Joker.cpp:137:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |     freopen(task ".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:138:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  138 |     freopen(task ".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 4 ms 9816 KB Output is correct
4 Correct 4 ms 9816 KB Output is correct
5 Correct 4 ms 9820 KB Output is correct
6 Correct 4 ms 9852 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 5 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 9820 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 5 ms 9820 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 6 ms 9692 KB Output is correct
17 Correct 5 ms 9820 KB Output is correct
18 Correct 4 ms 9816 KB Output is correct
19 Correct 4 ms 9880 KB Output is correct
20 Correct 4 ms 9848 KB Output is correct
21 Correct 5 ms 9816 KB Output is correct
22 Correct 4 ms 9676 KB Output is correct
23 Correct 4 ms 9820 KB Output is correct
24 Correct 4 ms 9820 KB Output is correct
25 Correct 5 ms 9816 KB Output is correct
26 Correct 4 ms 9816 KB Output is correct
27 Correct 6 ms 9860 KB Output is correct
28 Correct 4 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 4 ms 9816 KB Output is correct
4 Correct 4 ms 9816 KB Output is correct
5 Correct 4 ms 9820 KB Output is correct
6 Correct 4 ms 9852 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 5 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 9820 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 5 ms 9820 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 6 ms 9692 KB Output is correct
17 Correct 5 ms 9820 KB Output is correct
18 Correct 4 ms 9816 KB Output is correct
19 Correct 4 ms 9880 KB Output is correct
20 Correct 4 ms 9848 KB Output is correct
21 Correct 5 ms 9816 KB Output is correct
22 Correct 4 ms 9676 KB Output is correct
23 Correct 4 ms 9820 KB Output is correct
24 Correct 4 ms 9820 KB Output is correct
25 Correct 5 ms 9816 KB Output is correct
26 Correct 4 ms 9816 KB Output is correct
27 Correct 6 ms 9860 KB Output is correct
28 Correct 4 ms 9820 KB Output is correct
29 Correct 5 ms 9820 KB Output is correct
30 Correct 5 ms 9820 KB Output is correct
31 Correct 6 ms 9980 KB Output is correct
32 Correct 6 ms 9820 KB Output is correct
33 Correct 6 ms 9820 KB Output is correct
34 Correct 6 ms 9936 KB Output is correct
35 Correct 5 ms 9816 KB Output is correct
36 Correct 5 ms 9820 KB Output is correct
37 Correct 5 ms 9820 KB Output is correct
38 Correct 6 ms 9820 KB Output is correct
39 Correct 6 ms 9952 KB Output is correct
40 Correct 5 ms 9820 KB Output is correct
41 Correct 6 ms 9892 KB Output is correct
42 Correct 5 ms 9856 KB Output is correct
43 Correct 5 ms 9820 KB Output is correct
44 Correct 6 ms 9820 KB Output is correct
45 Correct 8 ms 9844 KB Output is correct
46 Correct 5 ms 9820 KB Output is correct
47 Correct 5 ms 9820 KB Output is correct
48 Correct 5 ms 9820 KB Output is correct
49 Correct 5 ms 9820 KB Output is correct
50 Correct 5 ms 9820 KB Output is correct
51 Correct 7 ms 9820 KB Output is correct
52 Correct 5 ms 9820 KB Output is correct
53 Correct 7 ms 9820 KB Output is correct
54 Correct 5 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 137 ms 20632 KB Output is correct
4 Correct 236 ms 23180 KB Output is correct
5 Correct 150 ms 23656 KB Output is correct
6 Correct 144 ms 20716 KB Output is correct
7 Correct 169 ms 20744 KB Output is correct
8 Correct 152 ms 19488 KB Output is correct
9 Correct 177 ms 20056 KB Output is correct
10 Correct 231 ms 20932 KB Output is correct
11 Correct 182 ms 20636 KB Output is correct
12 Correct 187 ms 21732 KB Output is correct
13 Correct 125 ms 18628 KB Output is correct
14 Correct 163 ms 20004 KB Output is correct
15 Correct 209 ms 21204 KB Output is correct
16 Correct 230 ms 21304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 4 ms 9816 KB Output is correct
4 Correct 4 ms 9816 KB Output is correct
5 Correct 4 ms 9820 KB Output is correct
6 Correct 4 ms 9852 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 5 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 9820 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 5 ms 9820 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 6 ms 9692 KB Output is correct
17 Correct 5 ms 9820 KB Output is correct
18 Correct 4 ms 9816 KB Output is correct
19 Correct 4 ms 9880 KB Output is correct
20 Correct 4 ms 9848 KB Output is correct
21 Correct 5 ms 9816 KB Output is correct
22 Correct 4 ms 9676 KB Output is correct
23 Correct 4 ms 9820 KB Output is correct
24 Correct 4 ms 9820 KB Output is correct
25 Correct 5 ms 9816 KB Output is correct
26 Correct 4 ms 9816 KB Output is correct
27 Correct 6 ms 9860 KB Output is correct
28 Correct 4 ms 9820 KB Output is correct
29 Correct 137 ms 20632 KB Output is correct
30 Correct 236 ms 23180 KB Output is correct
31 Correct 150 ms 23656 KB Output is correct
32 Correct 144 ms 20716 KB Output is correct
33 Correct 169 ms 20744 KB Output is correct
34 Correct 152 ms 19488 KB Output is correct
35 Correct 177 ms 20056 KB Output is correct
36 Correct 231 ms 20932 KB Output is correct
37 Correct 182 ms 20636 KB Output is correct
38 Correct 187 ms 21732 KB Output is correct
39 Correct 125 ms 18628 KB Output is correct
40 Correct 163 ms 20004 KB Output is correct
41 Correct 209 ms 21204 KB Output is correct
42 Correct 230 ms 21304 KB Output is correct
43 Correct 136 ms 21028 KB Output is correct
44 Correct 224 ms 23304 KB Output is correct
45 Correct 173 ms 23308 KB Output is correct
46 Correct 146 ms 21144 KB Output is correct
47 Correct 170 ms 20948 KB Output is correct
48 Correct 205 ms 20640 KB Output is correct
49 Correct 236 ms 21664 KB Output is correct
50 Correct 175 ms 20588 KB Output is correct
51 Correct 193 ms 21500 KB Output is correct
52 Correct 178 ms 22356 KB Output is correct
53 Correct 125 ms 18892 KB Output is correct
54 Correct 189 ms 20684 KB Output is correct
55 Correct 209 ms 21444 KB Output is correct
56 Correct 240 ms 21760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 4 ms 9816 KB Output is correct
4 Correct 4 ms 9816 KB Output is correct
5 Correct 4 ms 9820 KB Output is correct
6 Correct 4 ms 9852 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 5 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 9820 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 5 ms 9820 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 6 ms 9692 KB Output is correct
17 Correct 5 ms 9820 KB Output is correct
18 Correct 4 ms 9816 KB Output is correct
19 Correct 4 ms 9880 KB Output is correct
20 Correct 4 ms 9848 KB Output is correct
21 Correct 5 ms 9816 KB Output is correct
22 Correct 4 ms 9676 KB Output is correct
23 Correct 4 ms 9820 KB Output is correct
24 Correct 4 ms 9820 KB Output is correct
25 Correct 5 ms 9816 KB Output is correct
26 Correct 4 ms 9816 KB Output is correct
27 Correct 6 ms 9860 KB Output is correct
28 Correct 4 ms 9820 KB Output is correct
29 Correct 5 ms 9820 KB Output is correct
30 Correct 5 ms 9820 KB Output is correct
31 Correct 6 ms 9980 KB Output is correct
32 Correct 6 ms 9820 KB Output is correct
33 Correct 6 ms 9820 KB Output is correct
34 Correct 6 ms 9936 KB Output is correct
35 Correct 5 ms 9816 KB Output is correct
36 Correct 5 ms 9820 KB Output is correct
37 Correct 5 ms 9820 KB Output is correct
38 Correct 6 ms 9820 KB Output is correct
39 Correct 6 ms 9952 KB Output is correct
40 Correct 5 ms 9820 KB Output is correct
41 Correct 6 ms 9892 KB Output is correct
42 Correct 5 ms 9856 KB Output is correct
43 Correct 5 ms 9820 KB Output is correct
44 Correct 6 ms 9820 KB Output is correct
45 Correct 8 ms 9844 KB Output is correct
46 Correct 5 ms 9820 KB Output is correct
47 Correct 5 ms 9820 KB Output is correct
48 Correct 5 ms 9820 KB Output is correct
49 Correct 5 ms 9820 KB Output is correct
50 Correct 5 ms 9820 KB Output is correct
51 Correct 7 ms 9820 KB Output is correct
52 Correct 5 ms 9820 KB Output is correct
53 Correct 7 ms 9820 KB Output is correct
54 Correct 5 ms 9820 KB Output is correct
55 Correct 121 ms 18776 KB Output is correct
56 Correct 194 ms 21912 KB Output is correct
57 Correct 128 ms 21152 KB Output is correct
58 Correct 117 ms 18780 KB Output is correct
59 Correct 174 ms 18516 KB Output is correct
60 Correct 194 ms 20100 KB Output is correct
61 Correct 150 ms 18516 KB Output is correct
62 Correct 167 ms 20308 KB Output is correct
63 Correct 119 ms 17492 KB Output is correct
64 Correct 178 ms 19124 KB Output is correct
65 Correct 208 ms 20572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9816 KB Output is correct
2 Correct 5 ms 9820 KB Output is correct
3 Correct 4 ms 9816 KB Output is correct
4 Correct 4 ms 9816 KB Output is correct
5 Correct 4 ms 9820 KB Output is correct
6 Correct 4 ms 9852 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 4 ms 9820 KB Output is correct
9 Correct 5 ms 9820 KB Output is correct
10 Correct 4 ms 9820 KB Output is correct
11 Correct 4 ms 9820 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 5 ms 9820 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 4 ms 9820 KB Output is correct
16 Correct 6 ms 9692 KB Output is correct
17 Correct 5 ms 9820 KB Output is correct
18 Correct 4 ms 9816 KB Output is correct
19 Correct 4 ms 9880 KB Output is correct
20 Correct 4 ms 9848 KB Output is correct
21 Correct 5 ms 9816 KB Output is correct
22 Correct 4 ms 9676 KB Output is correct
23 Correct 4 ms 9820 KB Output is correct
24 Correct 4 ms 9820 KB Output is correct
25 Correct 5 ms 9816 KB Output is correct
26 Correct 4 ms 9816 KB Output is correct
27 Correct 6 ms 9860 KB Output is correct
28 Correct 4 ms 9820 KB Output is correct
29 Correct 5 ms 9820 KB Output is correct
30 Correct 5 ms 9820 KB Output is correct
31 Correct 6 ms 9980 KB Output is correct
32 Correct 6 ms 9820 KB Output is correct
33 Correct 6 ms 9820 KB Output is correct
34 Correct 6 ms 9936 KB Output is correct
35 Correct 5 ms 9816 KB Output is correct
36 Correct 5 ms 9820 KB Output is correct
37 Correct 5 ms 9820 KB Output is correct
38 Correct 6 ms 9820 KB Output is correct
39 Correct 6 ms 9952 KB Output is correct
40 Correct 5 ms 9820 KB Output is correct
41 Correct 6 ms 9892 KB Output is correct
42 Correct 5 ms 9856 KB Output is correct
43 Correct 5 ms 9820 KB Output is correct
44 Correct 6 ms 9820 KB Output is correct
45 Correct 8 ms 9844 KB Output is correct
46 Correct 5 ms 9820 KB Output is correct
47 Correct 5 ms 9820 KB Output is correct
48 Correct 5 ms 9820 KB Output is correct
49 Correct 5 ms 9820 KB Output is correct
50 Correct 5 ms 9820 KB Output is correct
51 Correct 7 ms 9820 KB Output is correct
52 Correct 5 ms 9820 KB Output is correct
53 Correct 7 ms 9820 KB Output is correct
54 Correct 5 ms 9820 KB Output is correct
55 Correct 137 ms 20632 KB Output is correct
56 Correct 236 ms 23180 KB Output is correct
57 Correct 150 ms 23656 KB Output is correct
58 Correct 144 ms 20716 KB Output is correct
59 Correct 169 ms 20744 KB Output is correct
60 Correct 152 ms 19488 KB Output is correct
61 Correct 177 ms 20056 KB Output is correct
62 Correct 231 ms 20932 KB Output is correct
63 Correct 182 ms 20636 KB Output is correct
64 Correct 187 ms 21732 KB Output is correct
65 Correct 125 ms 18628 KB Output is correct
66 Correct 163 ms 20004 KB Output is correct
67 Correct 209 ms 21204 KB Output is correct
68 Correct 230 ms 21304 KB Output is correct
69 Correct 136 ms 21028 KB Output is correct
70 Correct 224 ms 23304 KB Output is correct
71 Correct 173 ms 23308 KB Output is correct
72 Correct 146 ms 21144 KB Output is correct
73 Correct 170 ms 20948 KB Output is correct
74 Correct 205 ms 20640 KB Output is correct
75 Correct 236 ms 21664 KB Output is correct
76 Correct 175 ms 20588 KB Output is correct
77 Correct 193 ms 21500 KB Output is correct
78 Correct 178 ms 22356 KB Output is correct
79 Correct 125 ms 18892 KB Output is correct
80 Correct 189 ms 20684 KB Output is correct
81 Correct 209 ms 21444 KB Output is correct
82 Correct 240 ms 21760 KB Output is correct
83 Correct 121 ms 18776 KB Output is correct
84 Correct 194 ms 21912 KB Output is correct
85 Correct 128 ms 21152 KB Output is correct
86 Correct 117 ms 18780 KB Output is correct
87 Correct 174 ms 18516 KB Output is correct
88 Correct 194 ms 20100 KB Output is correct
89 Correct 150 ms 18516 KB Output is correct
90 Correct 167 ms 20308 KB Output is correct
91 Correct 119 ms 17492 KB Output is correct
92 Correct 178 ms 19124 KB Output is correct
93 Correct 208 ms 20572 KB Output is correct
94 Correct 141 ms 21648 KB Output is correct
95 Correct 211 ms 24232 KB Output is correct
96 Correct 164 ms 24564 KB Output is correct
97 Correct 148 ms 21776 KB Output is correct
98 Correct 180 ms 21732 KB Output is correct
99 Correct 171 ms 21036 KB Output is correct
100 Correct 214 ms 22524 KB Output is correct
101 Correct 173 ms 21440 KB Output is correct
102 Correct 198 ms 22264 KB Output is correct
103 Correct 196 ms 22516 KB Output is correct
104 Correct 154 ms 20816 KB Output is correct
105 Correct 215 ms 21980 KB Output is correct
106 Correct 231 ms 22896 KB Output is correct
107 Correct 96 ms 25148 KB Output is correct
108 Correct 155 ms 21532 KB Output is correct
109 Correct 142 ms 21532 KB Output is correct
110 Correct 142 ms 21480 KB Output is correct
111 Correct 147 ms 21536 KB Output is correct
112 Correct 144 ms 21536 KB Output is correct
113 Correct 146 ms 21492 KB Output is correct
114 Correct 139 ms 21540 KB Output is correct
115 Correct 142 ms 21540 KB Output is correct
116 Correct 139 ms 21792 KB Output is correct