Submission #733855

# Submission time Handle Problem Language Result Execution time Memory
733855 2023-05-01T11:14:53 Z MilosMilutinovic Joker (BOI20_joker) C++14
100 / 100
537 ms 16396 KB
/**
 *    author:  wxhtzdy
 *    created: 01.05.2023 11:39:02
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> u(m), v(m);
  for (int i = 0; i < m; i++) {
    cin >> u[i] >> v[i];
    --u[i]; --v[i];
  }
  vector<int> pr(n), sz(n, 1), col(n);
  iota(pr.begin(), pr.end(), 0);
  bool bad = false;
  vector<tuple<int, int, bool, int, int, int, int>> stk;
  function<pair<int, int>(int)> root = [&](int x) {
    if (pr[x] == x) {
      return make_pair(x, col[x]);
    } else {
      auto p = root(pr[x]);
      return make_pair(p.first, col[x] ^ p.second);
    }
  };
  function<void(int, int)> Unite = [&](int x, int y) {
    assert(x != y);
    pair<int, int> xs = root(x);
    pair<int, int> ys = root(y);
    x = xs.first;
    y = ys.first;
    stk.emplace_back(x, y, bad, sz[x], sz[y], col[x], col[y]);
    if (x == y) {
      if (xs.second == ys.second) {
        bad = true;
      }  
    } else {
      if (sz[x] < sz[y]) {
        swap(x, y); 
      }
      sz[x] += sz[y];
      pr[y] = x;
      col[y] = (1 ^ xs.second ^ ys.second);
    }
  };
  auto Rollback = [&]() {
    auto op = stk.back();
    stk.pop_back();
    int x = get<0>(op);
    int y = get<1>(op);
    pr[x] = x;
    pr[y] = y;
    bad = get<2>(op);
    sz[x] = get<3>(op);
    sz[y] = get<4>(op);
    col[x] = get<5>(op);
    col[y] = get<6>(op);
  };
  for (int i = 0; i < m; i++) {
    Unite(u[i], v[i]);
  }        
  if (!bad) {
    while (q--) {
      int l, r;
      cin >> l >> r;
      --l; --r;
      cout << "NO" << '\n';
    }
    return 0;
  }
  for (int i = 0; i < m; i++) {
    Rollback();
  }     
  vector<int> f(m);
  function<void(int, int, int)> Solve = [&](int l, int r, int rr) {
    if (l > r) {
      return;
    }
    int mid = l + r >> 1;    
    for (int i = l; i < mid; i++) {
      Unite(u[i], v[i]);
    }
    f[mid] = rr;
    if (!bad) {
      for (int i = rr - 1; i >= mid; i--) {
        Unite(u[i], v[i]);
        f[mid] = i;
        if (bad) {
          break;  
        }
      }
      for (int i = f[mid]; i < rr; i++) {
        Rollback();
      }
    }
    for (int i = l; i < mid; i++) {
      Rollback();
    }    
    {         
      for (int i = f[mid]; i < rr; i++) {
        Unite(u[i], v[i]);
      }
      Solve(l, mid - 1, f[mid]);
      for (int i = f[mid]; i < rr; i++) {
        Rollback();
      }
    }
    {
      for (int i = l; i <= mid; i++) {
        Unite(u[i], v[i]);
      }
      Solve(mid + 1, r, rr);  
      for (int i = l; i <= mid; i++) {
        Rollback();
      }
    }
  };
  Solve(0, m - 1, m);       
  while (q--) {
    int l, r;
    cin >> l >> r;
    --l; --r;
    cout << (f[l] > r ? "YES" : "NO") << '\n';
  }
  return 0;
}

Compilation message

Joker.cpp: In lambda function:
Joker.cpp:84:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   84 |     int mid = l + r >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 388 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 388 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 3 ms 456 KB Output is correct
34 Correct 3 ms 516 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 3 ms 468 KB Output is correct
40 Correct 3 ms 460 KB Output is correct
41 Correct 3 ms 468 KB Output is correct
42 Correct 3 ms 468 KB Output is correct
43 Correct 3 ms 460 KB Output is correct
44 Correct 3 ms 468 KB Output is correct
45 Correct 3 ms 468 KB Output is correct
46 Correct 4 ms 468 KB Output is correct
47 Correct 3 ms 592 KB Output is correct
48 Correct 3 ms 468 KB Output is correct
49 Correct 4 ms 468 KB Output is correct
50 Correct 3 ms 468 KB Output is correct
51 Correct 3 ms 456 KB Output is correct
52 Correct 3 ms 468 KB Output is correct
53 Correct 3 ms 496 KB Output is correct
54 Correct 3 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 318 ms 10332 KB Output is correct
4 Correct 78 ms 11452 KB Output is correct
5 Correct 277 ms 15172 KB Output is correct
6 Correct 370 ms 13884 KB Output is correct
7 Correct 394 ms 14208 KB Output is correct
8 Correct 400 ms 12876 KB Output is correct
9 Correct 430 ms 13428 KB Output is correct
10 Correct 511 ms 14736 KB Output is correct
11 Correct 381 ms 13872 KB Output is correct
12 Correct 414 ms 15044 KB Output is correct
13 Correct 396 ms 12860 KB Output is correct
14 Correct 409 ms 13472 KB Output is correct
15 Correct 500 ms 14416 KB Output is correct
16 Correct 519 ms 15248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 388 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 318 ms 10332 KB Output is correct
30 Correct 78 ms 11452 KB Output is correct
31 Correct 277 ms 15172 KB Output is correct
32 Correct 370 ms 13884 KB Output is correct
33 Correct 394 ms 14208 KB Output is correct
34 Correct 400 ms 12876 KB Output is correct
35 Correct 430 ms 13428 KB Output is correct
36 Correct 511 ms 14736 KB Output is correct
37 Correct 381 ms 13872 KB Output is correct
38 Correct 414 ms 15044 KB Output is correct
39 Correct 396 ms 12860 KB Output is correct
40 Correct 409 ms 13472 KB Output is correct
41 Correct 500 ms 14416 KB Output is correct
42 Correct 519 ms 15248 KB Output is correct
43 Correct 368 ms 14192 KB Output is correct
44 Correct 91 ms 14412 KB Output is correct
45 Correct 429 ms 15496 KB Output is correct
46 Correct 377 ms 14324 KB Output is correct
47 Correct 383 ms 14264 KB Output is correct
48 Correct 478 ms 13876 KB Output is correct
49 Correct 515 ms 15240 KB Output is correct
50 Correct 396 ms 13960 KB Output is correct
51 Correct 436 ms 14912 KB Output is correct
52 Correct 437 ms 15504 KB Output is correct
53 Correct 408 ms 13120 KB Output is correct
54 Correct 469 ms 13960 KB Output is correct
55 Correct 484 ms 14792 KB Output is correct
56 Correct 519 ms 15540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 388 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 3 ms 456 KB Output is correct
34 Correct 3 ms 516 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 3 ms 468 KB Output is correct
40 Correct 3 ms 460 KB Output is correct
41 Correct 3 ms 468 KB Output is correct
42 Correct 3 ms 468 KB Output is correct
43 Correct 3 ms 460 KB Output is correct
44 Correct 3 ms 468 KB Output is correct
45 Correct 3 ms 468 KB Output is correct
46 Correct 4 ms 468 KB Output is correct
47 Correct 3 ms 592 KB Output is correct
48 Correct 3 ms 468 KB Output is correct
49 Correct 4 ms 468 KB Output is correct
50 Correct 3 ms 468 KB Output is correct
51 Correct 3 ms 456 KB Output is correct
52 Correct 3 ms 468 KB Output is correct
53 Correct 3 ms 496 KB Output is correct
54 Correct 3 ms 468 KB Output is correct
55 Correct 284 ms 12600 KB Output is correct
56 Correct 50 ms 14012 KB Output is correct
57 Correct 231 ms 13900 KB Output is correct
58 Correct 328 ms 12608 KB Output is correct
59 Correct 405 ms 12632 KB Output is correct
60 Correct 469 ms 13500 KB Output is correct
61 Correct 397 ms 12732 KB Output is correct
62 Correct 383 ms 13756 KB Output is correct
63 Correct 346 ms 11844 KB Output is correct
64 Correct 450 ms 13004 KB Output is correct
65 Correct 487 ms 13748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 388 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 3 ms 456 KB Output is correct
34 Correct 3 ms 516 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 2 ms 468 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 3 ms 468 KB Output is correct
40 Correct 3 ms 460 KB Output is correct
41 Correct 3 ms 468 KB Output is correct
42 Correct 3 ms 468 KB Output is correct
43 Correct 3 ms 460 KB Output is correct
44 Correct 3 ms 468 KB Output is correct
45 Correct 3 ms 468 KB Output is correct
46 Correct 4 ms 468 KB Output is correct
47 Correct 3 ms 592 KB Output is correct
48 Correct 3 ms 468 KB Output is correct
49 Correct 4 ms 468 KB Output is correct
50 Correct 3 ms 468 KB Output is correct
51 Correct 3 ms 456 KB Output is correct
52 Correct 3 ms 468 KB Output is correct
53 Correct 3 ms 496 KB Output is correct
54 Correct 3 ms 468 KB Output is correct
55 Correct 318 ms 10332 KB Output is correct
56 Correct 78 ms 11452 KB Output is correct
57 Correct 277 ms 15172 KB Output is correct
58 Correct 370 ms 13884 KB Output is correct
59 Correct 394 ms 14208 KB Output is correct
60 Correct 400 ms 12876 KB Output is correct
61 Correct 430 ms 13428 KB Output is correct
62 Correct 511 ms 14736 KB Output is correct
63 Correct 381 ms 13872 KB Output is correct
64 Correct 414 ms 15044 KB Output is correct
65 Correct 396 ms 12860 KB Output is correct
66 Correct 409 ms 13472 KB Output is correct
67 Correct 500 ms 14416 KB Output is correct
68 Correct 519 ms 15248 KB Output is correct
69 Correct 368 ms 14192 KB Output is correct
70 Correct 91 ms 14412 KB Output is correct
71 Correct 429 ms 15496 KB Output is correct
72 Correct 377 ms 14324 KB Output is correct
73 Correct 383 ms 14264 KB Output is correct
74 Correct 478 ms 13876 KB Output is correct
75 Correct 515 ms 15240 KB Output is correct
76 Correct 396 ms 13960 KB Output is correct
77 Correct 436 ms 14912 KB Output is correct
78 Correct 437 ms 15504 KB Output is correct
79 Correct 408 ms 13120 KB Output is correct
80 Correct 469 ms 13960 KB Output is correct
81 Correct 484 ms 14792 KB Output is correct
82 Correct 519 ms 15540 KB Output is correct
83 Correct 284 ms 12600 KB Output is correct
84 Correct 50 ms 14012 KB Output is correct
85 Correct 231 ms 13900 KB Output is correct
86 Correct 328 ms 12608 KB Output is correct
87 Correct 405 ms 12632 KB Output is correct
88 Correct 469 ms 13500 KB Output is correct
89 Correct 397 ms 12732 KB Output is correct
90 Correct 383 ms 13756 KB Output is correct
91 Correct 346 ms 11844 KB Output is correct
92 Correct 450 ms 13004 KB Output is correct
93 Correct 487 ms 13748 KB Output is correct
94 Correct 332 ms 15028 KB Output is correct
95 Correct 376 ms 16136 KB Output is correct
96 Correct 286 ms 16020 KB Output is correct
97 Correct 361 ms 14904 KB Output is correct
98 Correct 387 ms 14968 KB Output is correct
99 Correct 420 ms 14448 KB Output is correct
100 Correct 493 ms 16320 KB Output is correct
101 Correct 385 ms 14656 KB Output is correct
102 Correct 447 ms 15544 KB Output is correct
103 Correct 452 ms 16288 KB Output is correct
104 Correct 424 ms 14132 KB Output is correct
105 Correct 510 ms 15280 KB Output is correct
106 Correct 537 ms 16024 KB Output is correct
107 Correct 244 ms 16396 KB Output is correct
108 Correct 333 ms 14764 KB Output is correct
109 Correct 333 ms 14692 KB Output is correct
110 Correct 327 ms 14772 KB Output is correct
111 Correct 322 ms 14900 KB Output is correct
112 Correct 337 ms 14700 KB Output is correct
113 Correct 325 ms 14756 KB Output is correct
114 Correct 318 ms 14652 KB Output is correct
115 Correct 331 ms 14752 KB Output is correct
116 Correct 324 ms 14656 KB Output is correct