結果

問題 No.1170 Never Want to Walk
ユーザー rsk0315rsk0315
提出日時 2020-08-14 23:03:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 12,596 bytes
コンパイル時間 1,527 ms
コンパイル使用メモリ 113,360 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-04-18 22:55:08
合計ジャッジ時間 4,680 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 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 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 137 ms
11,264 KB
testcase_28 AC 123 ms
10,624 KB
testcase_29 AC 178 ms
12,160 KB
testcase_30 AC 136 ms
11,264 KB
testcase_31 AC 144 ms
11,648 KB
testcase_32 AC 65 ms
9,216 KB
testcase_33 AC 69 ms
9,216 KB
testcase_34 AC 66 ms
9,344 KB
testcase_35 AC 66 ms
9,472 KB
testcase_36 AC 64 ms
9,216 KB
testcase_37 AC 64 ms
9,344 KB
testcase_38 AC 67 ms
9,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "C.cpp"
#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <map>
#include <utility>

#line 1 "~/git/library/utility/io.cpp"



/**
 * @brief 入出力
 * @author えびちゃん
 */

#include <cstddef>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <tuple>
#line 15 "~/git/library/utility/io.cpp"

class format {
public:
  using size_type = size_t;

private:
  char const* M_fmt;
  size_type M_nvars = 0;
  size_type M_error = -1;

  void M_print(std::ostream& os, char const* pos) const {
    while (*pos) {
      if (*pos == '{' || *pos == '}') ++pos;
      os << *pos++;
    }
  }

  template <typename Tp, typename... Args>
  void M_print(std::ostream& os, char const* pos, Tp const& x, Args const&... xs) const {
    while (true) {
      if (*pos == '{') {
        if (pos[1] == '{') {
          ++pos;
          os << '{';
        } else {
          char const* next = M_print_formatted(os, pos, x);
          return M_print(os, next, xs...);
        }
      } else {
        if (*pos == '}') ++pos;
        os << *pos;
      }
      ++pos;
    }
    char const* next = M_print_formatted(os, pos, x);
    M_print(os, next, xs...);
  }

  template <typename Tp>
  char const* M_print_formatted(std::ostream& os, char const* pos, Tp const& x) const {
    // parse flags, preferably
    while (*++pos != '}') {}
    os << x;
    return ++pos;
  }

  void M_scan(std::istream& is, char const* pos) const {
    while (true) {
      if (*pos == '{' || *pos == '}') ++pos;
      if (isspace(*pos)) {
        while (isspace(is.peek())) is.get();
      } else {
        if (is.peek() == *pos) {
          ++pos;
          is.get();
        } else {
          return;
        }
      }
    }
  }

  template <typename Tp, typename... Args>
  void M_scan(std::istream& is, char const* pos, Tp& x, Args&&... xs) const {
    while (true) {
      if (*pos == '{') {
        if (pos[1] == '{') {
          if (is.peek() == '{') {
            ++pos;
            is.get();
          } else {
            return;
          }
        } else {
          char const* next = M_scan_formatted(is, pos, x);
          return M_scan(is, next, xs...);
        }
      } else if (isspace(*pos)) {
        while (isspace(is.peek())) is.get();
        ++pos;
      } else {
        if (*pos == '}') ++pos;
        if (is.peek() == *pos) {
          ++pos;
          is.get();
        } else {
          return;
        }
      }
    }
  }

  template <typename Tp>
  char const* M_scan_formatted(std::istream& is, char const* pos, Tp& x) const {
    // parse flags, preferably
    while (*++pos != '}') {}
    is >> x;
    return ++pos;
  }

public:
  constexpr format(char const* fmt): M_fmt(fmt) {
    bool opens = 0;
    size_type i = 0;
    for (; fmt[i]; ++i) {
      if (fmt[i] == '{') {
        if (fmt[i+1] == '{') { ++i; continue; }  // escaped
        if (opens) { M_error = i; return; }
        opens = true;
      } else if (fmt[i] == '}') {
        if (fmt[i+1] == '}') { ++i; continue; }  // escaped
        if (!opens) { M_error = i; return; }
        opens = false;
        ++M_nvars;
      }
    }
    if (opens) { M_error = i; return; }
  }

  template <typename... Args>
  void print_(std::ostream& os, Args const&... xs) const {
    M_print(os, M_fmt, xs...);
  }

  template <typename... Args>
  void scan_(std::istream& is, Args&&... xs) const {
    M_scan(is, M_fmt, xs...);
  }

  constexpr size_type count() const { return M_nvars; }
  constexpr size_type error() const { return M_error; }
};

#define VA_COUNT(...)                                           \
  std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value

#define fprint(os, fmt, ...) (void)({                           \
      constexpr format fmt_(fmt);                               \
      constexpr size_t lhs = fmt_.count();                      \
      constexpr size_t rhs = VA_COUNT(__VA_ARGS__);             \
      static_assert(lhs == rhs, "size mismatch");               \
      static_assert(fmt_.error()+1 == 0, "misformatted");       \
      fmt_.print_(os, ##__VA_ARGS__);                           \
    })

#define fprintln(os, fmt, ...) (void)({         \
      fprint(os, fmt, ##__VA_ARGS__);           \
      os << '\n';;                              \
    })

#define print(...) fprint(std::cout, ##__VA_ARGS__)
#define println(...) fprintln(std::cout, ##__VA_ARGS__)
#define eprint(...) fprint(std::cerr, ##__VA_ARGS__)
#define eprintln(...) fprintln(std::cerr, ##__VA_ARGS__)

#define fscan(is, fmt, ...) (void)({                            \
      constexpr format fmt_(fmt);                               \
      constexpr size_t lhs = fmt_.count();                      \
      constexpr size_t rhs = VA_COUNT(__VA_ARGS__);             \
      static_assert(lhs == rhs, "size mismatch");               \
      static_assert(fmt_.error()+1 == 0, "misformatted");       \
      fmt_.scan_(is, ##__VA_ARGS__);                            \
    })

#define scan(...) fscan(std::cin, __VA_ARGS__)

__attribute__((constructor))
void ioinit() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cerr.tie(nullptr);
  std::cout << std::fixed << std::setprecision(16);
}


#line 1 "~/git/library/utility/make/vector.cpp"



/**
 * @brief 多次元 vector の作成
 * @author えびちゃん
 */

#line 10 "~/git/library/utility/make/vector.cpp"
#include <type_traits>
#include <vector>

namespace detail {
  template <typename Tp, size_t Nb>
  auto make_vector(std::vector<size_t>& sizes, Tp const& x) {
    if constexpr (Nb == 1) {
      return std::vector(sizes[0], x);
    } else {
      size_t size = sizes[Nb-1];
      sizes.pop_back();
      return std::vector(size, make_vector<Tp, Nb-1>(sizes, x));
    }
  }
}  // detail::

template <typename Tp, size_t Nb>
auto make_vector(size_t const(&sizes)[Nb], Tp const& x = Tp()) {
  std::vector<size_t> s(Nb);
  for (size_t i = 0; i < Nb; ++i) s[i] = sizes[Nb-i-1];
  return detail::make_vector<Tp, Nb>(s, x);
}


#line 1 "~/git/library/DataStructure/disjoint_set.cpp"



/**
 * @brief 素集合データ構造
 * @author えびちゃん
 */

#line 13 "~/git/library/DataStructure/disjoint_set.cpp"

class disjoint_set {
public:
  using size_type = size_t;

private:
  mutable std::vector<intmax_t> M_c;

public:
  disjoint_set() = default;

  explicit disjoint_set(size_type n): M_c(n, -1) {}

  void reset() { M_c.assign(M_c.size(), -1); }

  size_type representative(size_type v) const {
    if (M_c[v] < 0) return v;
    return (M_c[v] = representative(M_c[v]));
  }

  bool unite(size_type u, size_type v) {
    u = representative(u);
    v = representative(v);
    if (u == v) return false;
    if (-M_c[u] > -M_c[v]) std::swap(u, v);
    M_c[v] += M_c[u];
    M_c[u] = v;
    return true;
  }

  bool equivalent(size_type u, size_type v) const {
    return (representative(u) == representative(v));
  }

  size_type size() const noexcept { return M_c.size(); }
  size_type count(size_type v) const {
    return -M_c[representative(v)];
  }
};


#line 1 "~/git/library/DataStructure/integral_intervals.cpp"



/**
 * @brief 整数の区間の集合
 * @author えびちゃん
 */

#line 10 "~/git/library/DataStructure/integral_intervals.cpp"
#include <set>
#line 12 "~/git/library/DataStructure/integral_intervals.cpp"

template <typename Tp>
class integral_intervals {
public:
  using size_type = size_t;
  using value_type = Tp;
  using interval_type = std::pair<value_type, value_type>;

private:
  std::set<interval_type> M_intervals;
  size_type M_size = 0;

public:
  integral_intervals() = default;
  integral_intervals(integral_intervals const&) = default;
  integral_intervals(integral_intervals&&) = default;

  integral_intervals& operator =(integral_intervals const&) = default;
  integral_intervals& operator =(integral_intervals&&) = default;

  template <typename InputIt>
  integral_intervals(InputIt first, InputIt last) { assign(first, last); }
  integral_intervals(std::initializer_list<interval_type> il) { assign(il.begin(), il.end()); }

  template <typename InputIt>
  void assign(InputIt first, InputIt last) {
    while (first != last) {
      insert(first->first, first->second);
      ++first;
    }
  }

  void insert(value_type x) { value_type y = x; insert(x, ++y); }
  void erase(value_type x) { value_type y = x; erase(x, ++y); }

  void insert(value_type lb, value_type ub) {
    if (lb >= ub) return;
    if (M_intervals.empty()) {
      M_size += ub - lb;
      M_intervals.emplace(lb, ub);
      return;
    }

    auto it = M_intervals.upper_bound({lb, lb});
    if (it != M_intervals.begin() && !(std::prev(it)->second < lb)) {
      auto pit = std::prev(it);
      if (!(pit->second < ub)) return;
      lb = pit->first;
      M_size -= pit->second - pit->first;
      M_intervals.erase(pit);
    }
    while (it != M_intervals.end() && !(ub < it->first)) {
      if (ub < it->second) ub = it->second;
      M_size -= it->second - it->first;
      it = M_intervals.erase(it);
    }
    M_size += ub - lb;
    M_intervals.emplace(lb, ub);
  }

  void erase(value_type lb, value_type ub) {
    if (M_intervals.empty()) return;

    auto it = M_intervals.upper_bound({lb, lb});
    if (it != M_intervals.begin() && !(std::prev(it)->second < lb)) {
      auto pit = std::prev(it);
      if (!(pit->second < ub)) {
        // [ ...* [ ...+ ) ...* )
        --it;
        value_type lb0 = it->first;
        value_type ub0 = it->second;
        M_size -= it->second - it->first;
        M_intervals.erase(it);
        if (lb0 < lb) {
          M_size += lb - lb0;
          M_intervals.emplace(lb0, lb);
        }
        if (ub < ub0) {
          M_size += ub0 - ub;
          M_intervals.emplace(ub, ub0);
        }
        return;
      }

      // [ ...+ )      [ ...+ )*
      //      [ ...+ ) <- erase this
      value_type lb0 = pit->first;
      M_size -= pit->second - pit->first;
      M_size += lb - lb0;
      M_intervals.erase(pit);
      M_intervals.emplace(lb0, lb);
    }

    while (it != M_intervals.end() && !(ub < it->first)) {
      if (ub < it->second) {
        value_type ub0 = it->second;
        M_size -= it->second - it->first;
        M_size += ub0 - ub;
        M_intervals.erase(it);
        M_intervals.emplace(ub, ub0);
        break;
      }
      M_size -= it->second - it->first;
      it = M_intervals.erase(it);
    }
  }

  interval_type range(value_type x) const {
    if (M_intervals.empty()) return {x, x};
    auto it = M_intervals.upper_bound({x, x});

    if (it != M_intervals.end())
      if (!(x < it->first) && x < it->second) return *it;

    if (it == M_intervals.begin() || !(x < (--it)->second)) return {x, x};
    return *it;
  }

  bool contains(value_type x) const { return (range(x).second != x); }
  value_type mex() const { return range(0).second; }

  bool empty() const noexcept { return (M_size == 0); }
  size_type size() const { return M_size; }
  size_type count() const { return M_intervals.size(); }

  auto begin() const { return M_intervals.begin(); }
  auto end() const { return M_intervals.end(); }
};


#line 11 "C.cpp"

int main() {
  size_t n;
  int64_t a, b;
  scan("{} {} {}", n, a, b);
  a *= 2;
  b *= 2;

  std::vector<int64_t> x(n);
  for (auto& xi: x) scan("{}", xi), xi *= 2;

  integral_intervals<int64_t> seg;
  for (size_t i = 0; i < n; ++i) {
    auto il = std::lower_bound(x.begin(), x.end(), x[i]+a);
    auto ir = std::upper_bound(x.begin(), x.end(), x[i]+b);
    if (il == x.end()) continue;
    --ir; 
    seg.insert(*il, *ir+1);
  }

  disjoint_set ds(n+n);
  {
    size_t j = 0;
    for (auto [s, e]: seg) {
      // eprintln("seg [{}, {})", s, e);
      size_t il = std::lower_bound(x.begin(), x.end(), s-b) - x.begin();
      size_t ir = std::lower_bound(x.begin(), x.end(), e-a) - x.begin();
      // eprintln("[{}, {}) to {}", il, ir, j+n);
      for (size_t i = il; i < ir; ++i) ds.unite(i, j+n);
      ++j;
    }
  }
  {
    std::map<std::pair<int64_t, int64_t>, size_t> ss;
    for (auto se: seg) ss[se];
    size_t m = n;
    for (auto& [d, e]: ss) e = m++;
    for (size_t i = 0; i < n; ++i) {
      auto tmp = seg.range(x[i]);
      if (ss.count(tmp) == 0) continue;
      // eprintln("{} -- {}", ss.at(tmp), i);
      ds.unite(ss.at(tmp), i);
    }
  }

  std::vector<int> res(n+n, 0);
  for (size_t i = 0; i < n+n; ++i) {
    if (ds.representative(i) == i) res[i] = ds.count(i);
  }
  for (size_t i = 0; i < n; ++i) {
    --res[ds.representative(n+i)];
  }
  for (size_t i = 0; i < n; ++i) {
    println("{}", res[ds.representative(i)]);
  }
}
0