結果

問題 No.1170 Never Want to Walk
ユーザー kyo1kyo1
提出日時 2020-12-16 17:39:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 2,107 bytes
コンパイル時間 2,181 ms
コンパイル使用メモリ 208,572 KB
実行使用メモリ 10,204 KB
最終ジャッジ日時 2023-10-20 09:42:35
合計ジャッジ時間 6,697 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 3 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 64 ms
9,940 KB
testcase_28 AC 63 ms
9,940 KB
testcase_29 AC 66 ms
10,204 KB
testcase_30 AC 65 ms
10,204 KB
testcase_31 AC 64 ms
9,940 KB
testcase_32 AC 56 ms
9,940 KB
testcase_33 AC 61 ms
9,940 KB
testcase_34 AC 57 ms
10,204 KB
testcase_35 AC 58 ms
10,204 KB
testcase_36 AC 56 ms
9,940 KB
testcase_37 AC 56 ms
9,940 KB
testcase_38 AC 58 ms
10,204 KB
権限があれば一括ダウンロードができます

ソースコード

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