結果

問題 No.1170 Never Want to Walk
ユーザー polyesterpolyester
提出日時 2020-08-14 23:07:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,920 bytes
コンパイル時間 1,342 ms
コンパイル使用メモリ 97,064 KB
実行使用メモリ 24,708 KB
最終ジャッジ日時 2024-04-18 23:00:52
合計ジャッジ時間 7,342 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 6 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 7 ms
5,376 KB
testcase_25 AC 8 ms
5,376 KB
testcase_26 AC 5 ms
5,376 KB
testcase_27 AC 374 ms
18,792 KB
testcase_28 AC 365 ms
18,316 KB
testcase_29 AC 381 ms
19,328 KB
testcase_30 AC 377 ms
18,852 KB
testcase_31 AC 370 ms
18,372 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

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 = l; j < r; j++) {
        uf.unit(i, j);
      }
    }
  }
  for(int i = 0; i < n; i++) {
    cout << uf.size(i) << endl;
  }
  return 0;
}
0