結果

問題 No.1170 Never Want to Walk
ユーザー polyesterpolyester
提出日時 2020-08-14 23:09:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 362 ms / 2,000 ms
コード長 1,982 bytes
コンパイル時間 1,411 ms
コンパイル使用メモリ 99,688 KB
実行使用メモリ 19,624 KB
最終ジャッジ日時 2024-04-18 23:02:29
合計ジャッジ時間 7,156 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 4 ms
6,944 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 3 ms
6,944 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 4 ms
6,940 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 3 ms
6,944 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 331 ms
18,796 KB
testcase_28 AC 337 ms
18,500 KB
testcase_29 AC 362 ms
19,352 KB
testcase_30 AC 346 ms
19,052 KB
testcase_31 AC 332 ms
18,604 KB
testcase_32 AC 331 ms
19,068 KB
testcase_33 AC 335 ms
18,832 KB
testcase_34 AC 352 ms
19,624 KB
testcase_35 AC 361 ms
19,236 KB
testcase_36 AC 344 ms
19,024 KB
testcase_37 AC 349 ms
18,972 KB
testcase_38 AC 361 ms
19,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

struct UnionFind {
private:
  vector<ll> child, tree;
  vector<vector<ll>> list;

public:
  //頂点数v
  UnionFind(ll v) {
    tree.resize(v);
    list.resize(v);
    for(ll i = 0; i < v; i++) {
      tree[i] = i;
      list[i].push_back(i);
    }
  }

  //木の根を求める
  ll root(ll x) {
    if(x == tree[x]) {
      for(ll i = 0; i < child.size(); i++) {
        tree[child[i]] = x;
      }
      child.clear();
      return x;
    } else {
      child.push_back(x);
      return x = root(tree[x]);
    }
  }
  // xの所属するグループの大きさ
  ll size(ll x) { return list[root(x)].size(); }

  vector<ll> nodes(ll no) { return list[root(no)]; }

  // xとyの集合を合わせる
  bool unit(ll x, ll y) {
    x = root(x);
    y = root(y);
    if(x == y) {
      return false;
    }
    if(list[x].size() < list[y].size()) {
      swap(x, y);
    }
    for(int no : list[y]) {
      list[x].emplace_back(no);
    }
    tree[y] = x;
    return true;
  }

  // xとyが同じグループに所属するか
  bool isUnit(ll x, ll y) { return root(x) == root(y); }
};
int main() {
  int n, a, b;
  cin >> n >> a >> b;
  vector<ll> x(n);
  for(int i = 0; i < n; i++) {
    cin >> x[i];
  }
  UnionFind uf(n);
  for(int i = 0; i < n; i++) {
    auto check1 = lower_bound(x.begin(), x.end(), x[i] + a);
    auto check2 = upper_bound(x.begin(), x.end(), x[i] + b);
    int l, r;
    if(check1 != x.end()) {
      l = distance(x.begin(), check1);
      r = distance(x.begin(), check2);
      for(int j = r - 1; j >= l; j--) {
        if(uf.isUnit(i, j)) {
          break;
        }
        uf.unit(i, j);
      }
    }
  }
  for(int i = 0; i < n; i++) {
    cout << uf.size(i) << endl;
  }
  return 0;
}
0