답안 #262789

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
262789 2020-08-13T08:59:37 Z quocnguyen1012 Joker (BOI20_joker) C++14
100 / 100
198 ms 13540 KB
#include <bits/stdc++.h>
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array
 
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
 
const int maxn = 2e5 + 5;
 
struct pdsu
{
  vector<int> lab, off;
  vector<ar<int, 4>> saved;
  void init(int n)
  {
    saved.clear();
    lab.assign(n + 5, -1);
    off.assign(n + 5, 0);
  }
  ii finds(int u)
  {
    int now = 0;
    while(lab[u] >= 0){
      now ^= off[u];
      u = lab[u];
    }
    return mp(u, now);
  }
  bool merges(int __u, int __v)
  {
    ii x = finds(__u), y = finds(__v);
    if(x.fi == y.fi){
      if(x.se == y.se){
        return false;
      }
      ///saved.pb({-1, -1, -1, -1});
      return true;
    }
    if(lab[x.fi] > lab[y.fi]) swap(x, y);
    saved.pb({x.fi, y.fi, lab[x.fi], lab[y.fi]});
    lab[x.fi] += lab[y.fi]; lab[y.fi] = x.fi;
    off[y.fi] = x.se ^ y.se ^ 1;
    return true;
  }
  void rollback(void)
  {
    auto a = saved.back(); saved.pop_back();
    lab[a[0]] = a[2], lab[a[1]] = a[3];
  }
  void dbg(void)
  {
    for(int i = 1; i <= (int)lab.size() - 5; ++i){
      cerr << finds(i).fi << ' ';
    }
  }
}dsu;
 
void restore(int sz)
{
  while(dsu.saved.size() > sz){
    dsu.rollback();
  }
}
 
int U[maxn], V[maxn], op[maxn];
int N, M, Q;
 
void dnc(int l, int r, int opl, int opr)
{
  if(l > r) return;
  auto add = [&](int i)
  {
    if(i == M + 1) return true;
    return dsu.merges(U[i], V[i]);
  };
  //cerr << l << ' ' << r << ' ' << opl << ' ' << opr << '\n';
 
  int mid = (l + r) / 2;
  int sz = dsu.saved.size();
  int res = -1;
  /// calculate mid
  for(int i = l; i < mid; ++i){
    if(!add(i)){
      res = opr; assert(res == M + 1);
      break;
    }
  }
 
  int sz2 = dsu.saved.size();
  if(res == -1){
    int i = opr; while(i > opl && add(i)) --i;
    res = i;
  }
  op[mid] = res;
 
 
  if(op[mid] != M + 1){
    restore(sz2);
    if(add(mid))
      dnc(mid + 1, r, op[mid], opr);
    else{
      for(int i = mid + 1; i <= r; ++i)
        op[i] = M + 1;
    }
  }
  else{
    for(int i = mid + 1; i <= r ; ++i)
      op[i] = M + 1;
  }
  restore(sz);
  for(int i = op[mid] + 1; i <= opr; ++i)
    assert(add(i));
  dnc(l, mid - 1, opl, op[mid]);
  restore(sz);
}
 
signed main(void){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  cin >> N >> M >> Q; dsu.init(N);
  for(int i = 1; i <= M; ++i){
    cin >> U[i] >> V[i];
    //op[i] = M + 1;
  }
  dnc(1, M, 1, M + 1);
  #ifdef LOCAL
  //for(int i = 1; i <= M; ++i) cerr << op[i] << ' ' ; cerr << '\n';
  for(int i = 1; i <= M; ++i){
    restore(0);
    bool ok = false;
    for(int j = 1; j <= i; ++j){
      if(!dsu.merges(U[j], V[j])){
        ok = true;
      }
    }
    if(ok){
     // cerr << M + 1 << ' ';
      if(op[i] != M + 1){
        cout << -1;
        return 0;
      }
      continue;
    }
    int res = M;
    while(res > 1){
      if(dsu.merges(U[res], V[res])){
        --res;
      }
      else break;
    }
    //cerr << res << ' ';
    if(op[i] != res){
      cout << -1;
      return 0;
    }
  }
  cout << 0;
  return 0;
  #endif // LOCAL
  while(Q--){
    int l, r; cin >> l >> r;
    if(r >= op[l]) cout << "NO\n";
    else cout << "YES\n";
  }
}

Compilation message

Joker.cpp: In function 'void restore(int)':
Joker.cpp:66:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   66 |   while(dsu.saved.size() > sz){
      |         ~~~~~~~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 416 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 416 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 640 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 2 ms 512 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 2 ms 512 KB Output is correct
39 Correct 2 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 2 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 2 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 2 ms 512 KB Output is correct
47 Correct 2 ms 512 KB Output is correct
48 Correct 2 ms 512 KB Output is correct
49 Correct 2 ms 512 KB Output is correct
50 Correct 2 ms 512 KB Output is correct
51 Correct 2 ms 512 KB Output is correct
52 Correct 2 ms 512 KB Output is correct
53 Correct 3 ms 512 KB Output is correct
54 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 144 ms 9836 KB Output is correct
4 Correct 185 ms 11748 KB Output is correct
5 Correct 136 ms 12004 KB Output is correct
6 Correct 119 ms 9892 KB Output is correct
7 Correct 127 ms 9848 KB Output is correct
8 Correct 137 ms 8300 KB Output is correct
9 Correct 156 ms 9180 KB Output is correct
10 Correct 191 ms 10476 KB Output is correct
11 Correct 149 ms 9580 KB Output is correct
12 Correct 171 ms 10604 KB Output is correct
13 Correct 140 ms 7792 KB Output is correct
14 Correct 138 ms 8876 KB Output is correct
15 Correct 184 ms 10128 KB Output is correct
16 Correct 186 ms 10860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 416 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 144 ms 9836 KB Output is correct
30 Correct 185 ms 11748 KB Output is correct
31 Correct 136 ms 12004 KB Output is correct
32 Correct 119 ms 9892 KB Output is correct
33 Correct 127 ms 9848 KB Output is correct
34 Correct 137 ms 8300 KB Output is correct
35 Correct 156 ms 9180 KB Output is correct
36 Correct 191 ms 10476 KB Output is correct
37 Correct 149 ms 9580 KB Output is correct
38 Correct 171 ms 10604 KB Output is correct
39 Correct 140 ms 7792 KB Output is correct
40 Correct 138 ms 8876 KB Output is correct
41 Correct 184 ms 10128 KB Output is correct
42 Correct 186 ms 10860 KB Output is correct
43 Correct 174 ms 10084 KB Output is correct
44 Correct 183 ms 12260 KB Output is correct
45 Correct 198 ms 12644 KB Output is correct
46 Correct 119 ms 10220 KB Output is correct
47 Correct 121 ms 10220 KB Output is correct
48 Correct 164 ms 9580 KB Output is correct
49 Correct 192 ms 10860 KB Output is correct
50 Correct 151 ms 9580 KB Output is correct
51 Correct 158 ms 10616 KB Output is correct
52 Correct 166 ms 11116 KB Output is correct
53 Correct 142 ms 8048 KB Output is correct
54 Correct 180 ms 9764 KB Output is correct
55 Correct 177 ms 10476 KB Output is correct
56 Correct 196 ms 11116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 416 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 640 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 2 ms 512 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 2 ms 512 KB Output is correct
39 Correct 2 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 2 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 2 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 2 ms 512 KB Output is correct
47 Correct 2 ms 512 KB Output is correct
48 Correct 2 ms 512 KB Output is correct
49 Correct 2 ms 512 KB Output is correct
50 Correct 2 ms 512 KB Output is correct
51 Correct 2 ms 512 KB Output is correct
52 Correct 2 ms 512 KB Output is correct
53 Correct 3 ms 512 KB Output is correct
54 Correct 2 ms 512 KB Output is correct
55 Correct 106 ms 7532 KB Output is correct
56 Correct 147 ms 10220 KB Output is correct
57 Correct 94 ms 10092 KB Output is correct
58 Correct 103 ms 7532 KB Output is correct
59 Correct 124 ms 7404 KB Output is correct
60 Correct 155 ms 8168 KB Output is correct
61 Correct 123 ms 7280 KB Output is correct
62 Correct 123 ms 8556 KB Output is correct
63 Correct 98 ms 6000 KB Output is correct
64 Correct 155 ms 7660 KB Output is correct
65 Correct 152 ms 8304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 416 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 640 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 1 ms 512 KB Output is correct
34 Correct 2 ms 512 KB Output is correct
35 Correct 2 ms 512 KB Output is correct
36 Correct 2 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 2 ms 512 KB Output is correct
39 Correct 2 ms 512 KB Output is correct
40 Correct 2 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 2 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 2 ms 512 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 2 ms 512 KB Output is correct
47 Correct 2 ms 512 KB Output is correct
48 Correct 2 ms 512 KB Output is correct
49 Correct 2 ms 512 KB Output is correct
50 Correct 2 ms 512 KB Output is correct
51 Correct 2 ms 512 KB Output is correct
52 Correct 2 ms 512 KB Output is correct
53 Correct 3 ms 512 KB Output is correct
54 Correct 2 ms 512 KB Output is correct
55 Correct 144 ms 9836 KB Output is correct
56 Correct 185 ms 11748 KB Output is correct
57 Correct 136 ms 12004 KB Output is correct
58 Correct 119 ms 9892 KB Output is correct
59 Correct 127 ms 9848 KB Output is correct
60 Correct 137 ms 8300 KB Output is correct
61 Correct 156 ms 9180 KB Output is correct
62 Correct 191 ms 10476 KB Output is correct
63 Correct 149 ms 9580 KB Output is correct
64 Correct 171 ms 10604 KB Output is correct
65 Correct 140 ms 7792 KB Output is correct
66 Correct 138 ms 8876 KB Output is correct
67 Correct 184 ms 10128 KB Output is correct
68 Correct 186 ms 10860 KB Output is correct
69 Correct 174 ms 10084 KB Output is correct
70 Correct 183 ms 12260 KB Output is correct
71 Correct 198 ms 12644 KB Output is correct
72 Correct 119 ms 10220 KB Output is correct
73 Correct 121 ms 10220 KB Output is correct
74 Correct 164 ms 9580 KB Output is correct
75 Correct 192 ms 10860 KB Output is correct
76 Correct 151 ms 9580 KB Output is correct
77 Correct 158 ms 10616 KB Output is correct
78 Correct 166 ms 11116 KB Output is correct
79 Correct 142 ms 8048 KB Output is correct
80 Correct 180 ms 9764 KB Output is correct
81 Correct 177 ms 10476 KB Output is correct
82 Correct 196 ms 11116 KB Output is correct
83 Correct 106 ms 7532 KB Output is correct
84 Correct 147 ms 10220 KB Output is correct
85 Correct 94 ms 10092 KB Output is correct
86 Correct 103 ms 7532 KB Output is correct
87 Correct 124 ms 7404 KB Output is correct
88 Correct 155 ms 8168 KB Output is correct
89 Correct 123 ms 7280 KB Output is correct
90 Correct 123 ms 8556 KB Output is correct
91 Correct 98 ms 6000 KB Output is correct
92 Correct 155 ms 7660 KB Output is correct
93 Correct 152 ms 8304 KB Output is correct
94 Correct 158 ms 10784 KB Output is correct
95 Correct 196 ms 13284 KB Output is correct
96 Correct 153 ms 13036 KB Output is correct
97 Correct 129 ms 10860 KB Output is correct
98 Correct 137 ms 10860 KB Output is correct
99 Correct 160 ms 9840 KB Output is correct
100 Correct 179 ms 12012 KB Output is correct
101 Correct 161 ms 10344 KB Output is correct
102 Correct 175 ms 11212 KB Output is correct
103 Correct 187 ms 11984 KB Output is correct
104 Correct 153 ms 9328 KB Output is correct
105 Correct 185 ms 10988 KB Output is correct
106 Correct 197 ms 11756 KB Output is correct
107 Correct 124 ms 13540 KB Output is correct
108 Correct 150 ms 10672 KB Output is correct
109 Correct 159 ms 10604 KB Output is correct
110 Correct 152 ms 10604 KB Output is correct
111 Correct 153 ms 10604 KB Output is correct
112 Correct 152 ms 10604 KB Output is correct
113 Correct 166 ms 10860 KB Output is correct
114 Correct 157 ms 10608 KB Output is correct
115 Correct 155 ms 10560 KB Output is correct
116 Correct 173 ms 10732 KB Output is correct