답안 #1046179

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1046179 2024-08-06T11:11:49 Z juicy Collapse (JOI18_collapse) C++17
100 / 100
2826 ms 11848 KB
#include <bits/stdc++.h>

#include "collapse.h"

using namespace std;

template<class T, size_t D>
ostream &operator << (ostream &os, array<T, D> u) {
  os << "{";
  for (size_t i = 0; i < D; ++i) {
    os << (i ? ", " : "") << u[i];
  }
  return os << "}";
} 

template<class T>
ostream &operator << (ostream &os, vector<T> u) {
  os << "{";
  for (int i = 0; i < u.size(); ++i) {
    os << (i ? ", " : "") << u[i];
  }
  return os << "}";
} 

void __print() {
  cerr << "]\n";
}

template<class T, class... V>
void __print(T t, V... v) {
  cerr << t;
  if (sizeof...(v)) {
    cerr << ", ";
  }
  __print(v...);
}

#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; __print(x);
#else 
#define debug(...) 42
#endif

vector<int> simulateCollapse(int N, vector<int> T, vector<int> X, vector<int> Y, vector<int> W, vector<int> P) {
  const int S = 320;

  int C = X.size(), Q = W.size();

  int cnt = 0;
  vector<int> lab(N, -1);
  vector<array<int, 2>> his;
  auto init = [&]() {
    fill(lab.begin(), lab.end(), -1);
    cnt = 0;
  };
  auto find = [&](int u) -> int {
    while (lab[u] >= 0) {
      u = lab[u];
    }
    return u;
  };
  auto unite = [&](int u, int v, bool keep) {
    u = find(u), v = find(v);
    if (u == v) {
      if (keep) {
        his.push_back({-1, -1});
      }
      return;
    }
    if (lab[u] > lab[v]) {
      swap(u, v);
    }
    if (keep) {
      his.push_back({v, lab[v]});
    }
    ++cnt;
    lab[u] += lab[v];
    lab[v] = u;
  };
  auto restore = [&]() {
    while (his.size()) {
      auto e = his.back(); his.pop_back();
      if (~e[0]) {
        --cnt;
        int &p = lab[e[0]];
        lab[p] -= e[1];
        p = e[1];
      }
    }
  };
  vector<array<int, 2>> comp;
  for (int i = 0; i < C; ++i) {
    if (X[i] > Y[i]) {
      swap(X[i], Y[i]);
    }
    comp.push_back({X[i], Y[i]});
  }
  sort(comp.begin(), comp.end());
  comp.erase(unique(comp.begin(), comp.end()), comp.end());
  int M = comp.size();
  vector<int> pos(C);
  for (int i = 0; i < C; ++i) {
    T[i] ^= 1;
    pos[i] = lower_bound(comp.begin(), comp.end(), array<int, 2>{X[i], Y[i]}) - comp.begin();
  }
  vector<array<int, 3>> queries;
  vector<int> res(Q);
  for (int i = 0; i < Q; ++i) {
    queries.push_back({P[i], W[i], i});
    res[i] = N;
  }
  sort(queries.begin(), queries.end(), [&](const auto &u, const auto &v) {
    return u[1] < v[1];
  });
  vector<bool> tog(M), cur(M), nw(M);
  int ptr = 0;
  for (int i = 0; i < C; i += S) {
    int l = i, r = min(C - 1, i + S - 1);
    for (int j = l; j <= r; ++j) {
      tog[pos[j]] = 1;
    }
    vector<int> add;
    vector<array<int, 2>> cands;
    for (int j = 0; j < M; ++j) {
      if (!tog[j] && cur[j]) {
        cands.push_back(comp[j]);
      }
      if (tog[j]) {
        add.push_back(j);
      }
    }
    vector<array<int, 3>> ord;
    while (ptr < Q && queries[ptr][1] <= r) {
      ord.push_back(queries[ptr++]);
    }
    sort(ord.begin(), ord.end());
    sort(cands.begin(), cands.end(), [&](const auto &u, const auto &v) {
      return u[1] < v[1];
    });
    init();
    int k = 0;
    for (auto [x, d, id] : ord) {
      while (k < cands.size() && cands[k][1] <= x) {
        unite(cands[k][0], cands[k][1], 0);
        ++k;
      }
      for (int j = l; j <= d; ++j) {
        nw[pos[j]] = T[j];
      }
      for (int id : add) {
        if (nw[id] && comp[id][1] <= x) {
          unite(comp[id][0], comp[id][1], 1);
        }
        nw[id] = cur[id];
      }
      res[id] -= cnt;
      restore();
    }
    init();
    reverse(ord.begin(), ord.end());
    sort(cands.rbegin(), cands.rend());
    k = 0;
    for (auto [x, d, id] : ord) {
      while (k < cands.size() && cands[k][0] > x) {
        unite(cands[k][0], cands[k][1], 0);
        ++k;
      }
      for (int j = l; j <= d; ++j) {
        nw[pos[j]] = T[j];
      }
      for (int id : add) {
        if (nw[id] && comp[id][0] > x) {
          unite(comp[id][0], comp[id][1], 1);
        }
        nw[id] = cur[id];
      }
      res[id] -= cnt;
      restore();
    }
    for (int j = l; j <= r; ++j) {
      tog[pos[j]] = 0;
      cur[pos[j]] = T[j];
      nw[pos[j]] = cur[pos[j]];
    }
  }
  return res;
}

Compilation message

collapse.cpp: In function 'std::vector<int> simulateCollapse(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:143:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  143 |       while (k < cands.size() && cands[k][1] <= x) {
      |              ~~^~~~~~~~~~~~~~
collapse.cpp:164:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |       while (k < cands.size() && cands[k][0] > x) {
      |              ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1012 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 3 ms 600 KB Output is correct
4 Correct 3 ms 860 KB Output is correct
5 Correct 5 ms 860 KB Output is correct
6 Correct 19 ms 860 KB Output is correct
7 Correct 2 ms 720 KB Output is correct
8 Correct 2 ms 856 KB Output is correct
9 Correct 9 ms 1012 KB Output is correct
10 Correct 16 ms 860 KB Output is correct
11 Correct 22 ms 856 KB Output is correct
12 Correct 19 ms 1000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 6048 KB Output is correct
2 Correct 28 ms 6100 KB Output is correct
3 Correct 108 ms 9412 KB Output is correct
4 Correct 46 ms 6352 KB Output is correct
5 Correct 202 ms 9668 KB Output is correct
6 Correct 189 ms 5100 KB Output is correct
7 Correct 2156 ms 10884 KB Output is correct
8 Correct 240 ms 10440 KB Output is correct
9 Correct 25 ms 6860 KB Output is correct
10 Correct 39 ms 6868 KB Output is correct
11 Correct 229 ms 5716 KB Output is correct
12 Correct 251 ms 10956 KB Output is correct
13 Correct 1022 ms 10952 KB Output is correct
14 Correct 2383 ms 11820 KB Output is correct
15 Correct 1715 ms 11840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 6096 KB Output is correct
2 Correct 30 ms 6096 KB Output is correct
3 Correct 38 ms 6100 KB Output is correct
4 Correct 50 ms 6348 KB Output is correct
5 Correct 210 ms 5776 KB Output is correct
6 Correct 219 ms 4844 KB Output is correct
7 Correct 1383 ms 9540 KB Output is correct
8 Correct 2595 ms 11144 KB Output is correct
9 Correct 26 ms 6864 KB Output is correct
10 Correct 294 ms 5744 KB Output is correct
11 Correct 2329 ms 11824 KB Output is correct
12 Correct 2747 ms 11836 KB Output is correct
13 Correct 2474 ms 11828 KB Output is correct
14 Correct 2721 ms 11848 KB Output is correct
15 Correct 2428 ms 11784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1012 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 3 ms 600 KB Output is correct
4 Correct 3 ms 860 KB Output is correct
5 Correct 5 ms 860 KB Output is correct
6 Correct 19 ms 860 KB Output is correct
7 Correct 2 ms 720 KB Output is correct
8 Correct 2 ms 856 KB Output is correct
9 Correct 9 ms 1012 KB Output is correct
10 Correct 16 ms 860 KB Output is correct
11 Correct 22 ms 856 KB Output is correct
12 Correct 19 ms 1000 KB Output is correct
13 Correct 23 ms 6048 KB Output is correct
14 Correct 28 ms 6100 KB Output is correct
15 Correct 108 ms 9412 KB Output is correct
16 Correct 46 ms 6352 KB Output is correct
17 Correct 202 ms 9668 KB Output is correct
18 Correct 189 ms 5100 KB Output is correct
19 Correct 2156 ms 10884 KB Output is correct
20 Correct 240 ms 10440 KB Output is correct
21 Correct 25 ms 6860 KB Output is correct
22 Correct 39 ms 6868 KB Output is correct
23 Correct 229 ms 5716 KB Output is correct
24 Correct 251 ms 10956 KB Output is correct
25 Correct 1022 ms 10952 KB Output is correct
26 Correct 2383 ms 11820 KB Output is correct
27 Correct 1715 ms 11840 KB Output is correct
28 Correct 23 ms 6096 KB Output is correct
29 Correct 30 ms 6096 KB Output is correct
30 Correct 38 ms 6100 KB Output is correct
31 Correct 50 ms 6348 KB Output is correct
32 Correct 210 ms 5776 KB Output is correct
33 Correct 219 ms 4844 KB Output is correct
34 Correct 1383 ms 9540 KB Output is correct
35 Correct 2595 ms 11144 KB Output is correct
36 Correct 26 ms 6864 KB Output is correct
37 Correct 294 ms 5744 KB Output is correct
38 Correct 2329 ms 11824 KB Output is correct
39 Correct 2747 ms 11836 KB Output is correct
40 Correct 2474 ms 11828 KB Output is correct
41 Correct 2721 ms 11848 KB Output is correct
42 Correct 2428 ms 11784 KB Output is correct
43 Correct 261 ms 9924 KB Output is correct
44 Correct 2243 ms 10844 KB Output is correct
45 Correct 313 ms 10432 KB Output is correct
46 Correct 2550 ms 11136 KB Output is correct
47 Correct 26 ms 6864 KB Output is correct
48 Correct 33 ms 6868 KB Output is correct
49 Correct 258 ms 5580 KB Output is correct
50 Correct 262 ms 6144 KB Output is correct
51 Correct 319 ms 11208 KB Output is correct
52 Correct 747 ms 10952 KB Output is correct
53 Correct 665 ms 10952 KB Output is correct
54 Correct 1177 ms 10948 KB Output is correct
55 Correct 1016 ms 10952 KB Output is correct
56 Correct 1474 ms 10952 KB Output is correct
57 Correct 2041 ms 11656 KB Output is correct
58 Correct 2204 ms 11756 KB Output is correct
59 Correct 2288 ms 11832 KB Output is correct
60 Correct 2826 ms 11832 KB Output is correct