結果

問題 No.1170 Never Want to Walk
ユーザー kyo1
提出日時 2020-12-16 17:39:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 2,107 bytes
コンパイル時間 2,159 ms
コンパイル使用メモリ 199,976 KB
最終ジャッジ日時 2025-01-17 02:04:06
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

class DisjointSet {
 private:
  class Node {
    friend DisjointSet;

    std::size_t parent;
    std::size_t size;
    std::size_t left = 0, right = 0;

    Node(const std::size_t parent, const std::size_t size) : parent(parent), size(size) {}
  };

  std::vector<Node> nodes;

 public:
  explicit DisjointSet(const std::size_t n) : nodes(n, Node(n, 1)) {
    for (std::size_t i = 0; i < n; i++) {
      nodes[i].left = nodes[i].right = i;
    }
  }

  std::size_t size() const { return nodes.size(); }

  std::size_t size(const std::size_t x) { return nodes[root(x)].size; }

  std::size_t root(const std::size_t x) {
    if (nodes[x].parent == size()) return x;
    return nodes[x].parent = root(nodes[x].parent);
  }

  std::size_t left(const std::size_t x) { return nodes[root(x)].left; }

  std::size_t right(const std::size_t x) { return nodes[root(x)].right; }

  bool is_same(const std::size_t x, const std::size_t y) { return root(x) == root(y); }

  bool unite(const std::size_t x, const std::size_t y) {
    std::size_t rx = root(x), ry = root(y);
    if (rx == ry) return false;
    if (nodes[rx].size < nodes[ry].size) std::swap(rx, ry);
    nodes[rx].left = std::min(nodes[rx].left, nodes[ry].left);
    nodes[rx].right = std::max(nodes[rx].right, nodes[ry].right);
    nodes[rx].size += nodes[ry].size;
    nodes[ry].parent = rx;
    return true;
  }

  void unite_range(const std::size_t x, const std::size_t y) {
    if (x > y) return;
    while (right(x) < y) {
      unite(right(x), right(x) + 1);
    }
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, A, B;
  cin >> N >> A >> B;
  vector<int> X(N);
  for (auto &&e : X) {
    cin >> e;
  }
  DisjointSet ds(N);
  for (int i = 0; i < N; i++) {
    int a = lower_bound(X.begin(), X.end(), X[i] + A) - X.begin();
    int b = min(N - 1, static_cast<int>(upper_bound(X.begin(), X.end(), X[i] + B) - X.begin() - 1));
    ds.unite_range(a, b);
    if (a <= b) ds.unite(i, a);
  }
  for (int i = 0; i < N; i++) {
    cout << ds.size(i) << '\n';
  }
  return 0;
}
0