Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define loop(n)                                                                \
  for (int ngtkana_is_a_genius = 0; ngtkana_is_a_genius < int(n);              \
       ngtkana_is_a_genius++)
#define rep(i, begin, end) for (int i = int(begin); (i) < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto &a, auto b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};
auto cmx = [](auto &a, auto b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
  std::cerr << " " << head;
  debug_impl(tail...);
}
#ifndef STOPIT
#define debug(...)                                                             \
  do {                                                                         \
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";                \
    debug_impl(__VA_ARGS__);                                                   \
    std::cerr << std::noboolalpha;                                             \
  } while (false)
#else
#define debug(...) 0;
#endif

template <typename Container, typename Value = typename Container::value_type,
          std::enable_if_t<!std::is_same<Container, std::string>::value,
                           std::nullptr_t> = nullptr>
std::istream &operator>>(std::istream &is, Container &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <typename Container, typename Value = typename Container::value_type,
          std::enable_if_t<!std::is_same<Container, std::string>::value,
                           std::nullptr_t> = nullptr>
std::ostream &operator<<(std::ostream &os, Container const &v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); it++) {
    os << (it != v.begin() ? "," : "") << *it;
  }
  return os << "}";
}

template <template <typename...> class Tuple, typename... Args,
          std::size_t... Inds,
          std::size_t = std::tuple_size<Tuple<Args...>>::value>
std::istream &tuple_input_impl(std::istream &os, Tuple<Args...> &tuple,
                               std::integer_sequence<std::size_t, Inds...>) {
  (void)std::initializer_list<int>{((void)(os >> std::get<Inds>(tuple)), 0)...};
  return os;
}
template <template <typename...> class Tuple, typename... Args,
          std::size_t = std::tuple_size<Tuple<Args...>>::value>
std::istream &operator>>(std::istream &os, Tuple<Args...> &tuple) {
  return tuple_input_impl(os, tuple, std::index_sequence_for<Args...>());
}

template <template <typename...> class Tuple, typename... Args,
          std::size_t... Inds,
          std::size_t = std::tuple_size<Tuple<Args...>>::value>
std::ostream &tuple_output_impl(std::ostream &os, const Tuple<Args...> &tuple,
                                std::integer_sequence<std::size_t, Inds...>) {
  os << "(";
  (void)std::initializer_list<int>{
      ((void)(os << (Inds > 0 ? "," : "") << std::get<Inds>(tuple)), 0)...};
  return os << ")";
}
template <template <typename...> class Tuple, typename... Args,
          std::size_t = std::tuple_size<Tuple<Args...>>::value>
std::ostream &operator<<(std::ostream &os, const Tuple<Args...> &tuple) {
  return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>());
}

template <typename Value, typename BinaryOp> class segment_tree {
  int sz, n, N;
  BinaryOp op;
  Value id;
  std::vector<Value> table;

  auto &op_eq(Value &x, Value y) const { return x = op(x, y); }
  void merge(int u) { table.at(u) = op(table.at(2 * u), table.at(2 * u + 1)); }

public:
  segment_tree(int sz, BinaryOp op, Value id)
      : sz(sz), n(std::pow(2, int(std::log2(2 * sz - 1)))), N(n * 2), op(op),
        id(id), table(N, id) {}

  auto &at(int i) { return table.at(n + i); }
  auto &at(int i) const { return table.at(n + i); }
  auto collect() const {
    auto ret = std::vector<Value>(sz);
    for (auto i = 0; i < sz; i++) {
      ret.at(i) = at(i);
    }
    return ret;
  }
  auto query(int l, int r) const {
    auto const dfs = [&](auto &&f, int l, int r, int k, int L, int R) -> Value {
      return l <= L && R <= r   ? table.at(k)
             : R <= l || r <= L ? id
                                : op(f(f, l, r, 2 * k, L, (L + R) / 2),
                                     f(f, l, r, 2 * k + 1, (L + R) / 2, R));
    };
    return dfs(dfs, l, r, 1, 0, n);
  }
  void build_oneline(int i) {
    for (i += n, i /= 2; i > 0; i /= 2)
      merge(i);
  }
  void build() {
    for (auto i = n - 1; i > 0; i--)
      merge(i);
  }
};

template <typename Value, typename BinaryOp>
auto make_segment_tree(int sz, BinaryOp op, Value id) {
  return segment_tree<Value, BinaryOp>(sz, std::move(op), id);
}

int main() {
  std::cin.tie(0);
  std::cin.sync_with_stdio(false);
  int n, m;
  std::cin >> n >> m;
  std::vector<std::tuple<int, int, int>> data(n);
  rep(i, 0, n) {
    int l, r;
    std::cin >> l >> r;
    r++;
    data.at(i) = std::make_tuple(r - l, l, r);
  }
  std::sort(all(data));
  std::reverse(all(data));
  // debug(data);

  int N = n + 2;
  auto seg = make_segment_tree<int>(N, std::plus<int>{}, 0);
  int rest = n;
  rep(d, 1, m + 1) {
    assert(rest == (int)data.size());
    int ans = rest;
    for (int j = d; j <= m; j += d) {
      ans += seg.query(0, j + 1);
    }
    while (!data.empty()) {
      int dif, l, r;
      std::tie(dif, l, r) = data.back();
      assert(d <= dif);
      if (d < dif)
        break;
      data.pop_back();
      rest--;
      seg.at(r)--;
      seg.at(l)++;
      seg.build_oneline(r);
      seg.build_oneline(l);
    }
    // debug(seg.collect());
    std::cout << ans << std::endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define loop(n)                                                                \
  for (int ngtkana_is_a_genius = 0; ngtkana_is_a_genius < int(n);              \
       ngtkana_is_a_genius++)
#define rep(i, begin, end) for (int i = int(begin); (i) < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto &a, auto b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};
auto cmx = [](auto &a, auto b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
  std::cerr << " " << head;
  debug_impl(tail...);
}
#ifndef STOPIT
#define debug(...)                                                             \
  do {                                                                         \
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";                \
    debug_impl(__VA_ARGS__);                                                   \
    std::cerr << std::noboolalpha;                                             \
  } while (false)
#else
#define debug(...) 0;
#endif

template <typename Container, typename Value = typename Container::value_type,
          std::enable_if_t<!std::is_same<Container, std::string>::value,
                           std::nullptr_t> = nullptr>
std::istream &operator>>(std::istream &is, Container &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <typename Container, typename Value = typename Container::value_type,
          std::enable_if_t<!std::is_same<Container, std::string>::value,
                           std::nullptr_t> = nullptr>
std::ostream &operator<<(std::ostream &os, Container const &v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); it++) {
    os << (it != v.begin() ? "," : "") << *it;
  }
  return os << "}";
}

template <template <typename...> class Tuple, typename... Args,
          std::size_t... Inds,
          std::size_t = std::tuple_size<Tuple<Args...>>::value>
std::istream &tuple_input_impl(std::istream &os, Tuple<Args...> &tuple,
                               std::integer_sequence<std::size_t, Inds...>) {
  (void)std::initializer_list<int>{((void)(os >> std::get<Inds>(tuple)), 0)...};
  return os;
}
template <template <typename...> class Tuple, typename... Args,
          std::size_t = std::tuple_size<Tuple<Args...>>::value>
std::istream &operator>>(std::istream &os, Tuple<Args...> &tuple) {
  return tuple_input_impl(os, tuple, std::index_sequence_for<Args...>());
}

template <template <typename...> class Tuple, typename... Args,
          std::size_t... Inds,
          std::size_t = std::tuple_size<Tuple<Args...>>::value>
std::ostream &tuple_output_impl(std::ostream &os, const Tuple<Args...> &tuple,
                                std::integer_sequence<std::size_t, Inds...>) {
  os << "(";
  (void)std::initializer_list<int>{
      ((void)(os << (Inds > 0 ? "," : "") << std::get<Inds>(tuple)), 0)...};
  return os << ")";
}
template <template <typename...> class Tuple, typename... Args,
          std::size_t = std::tuple_size<Tuple<Args...>>::value>
std::ostream &operator<<(std::ostream &os, const Tuple<Args...> &tuple) {
  return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>());
}

template <typename Value, typename BinaryOp> class segment_tree {
  int sz, n, N;
  BinaryOp op;
  Value id;
  std::vector<Value> table;

  auto &op_eq(Value &x, Value y) const { return x = op(x, y); }
  void merge(int u) { table.at(u) = op(table.at(2 * u), table.at(2 * u + 1)); }

public:
  segment_tree(int sz, BinaryOp op, Value id)
      : sz(sz), n(std::pow(2, int(std::log2(2 * sz - 1)))), N(n * 2), op(op),
        id(id), table(N, id) {}

  auto &at(int i) { return table.at(n + i); }
  auto &at(int i) const { return table.at(n + i); }
  auto collect() const {
    auto ret = std::vector<Value>(sz);
    for (auto i = 0; i < sz; i++) {
      ret.at(i) = at(i);
    }
    return ret;
  }
  auto query(int l, int r) const {
    auto const dfs = [&](auto &&f, int l, int r, int k, int L, int R) -> Value {
      return l <= L && R <= r   ? table.at(k)
             : R <= l || r <= L ? id
                                : op(f(f, l, r, 2 * k, L, (L + R) / 2),
                                     f(f, l, r, 2 * k + 1, (L + R) / 2, R));
    };
    return dfs(dfs, l, r, 1, 0, n);
  }
  void build_oneline(int i) {
    for (i += n, i /= 2; i > 0; i /= 2)
      merge(i);
  }
  void build() {
    for (auto i = n - 1; i > 0; i--)
      merge(i);
  }
};

template <typename Value, typename BinaryOp>
auto make_segment_tree(int sz, BinaryOp op, Value id) {
  return segment_tree<Value, BinaryOp>(sz, std::move(op), id);
}

int main() {
  std::cin.tie(0);
  std::cin.sync_with_stdio(false);
  int n, m;
  std::cin >> n >> m;
  std::vector<std::tuple<int, int, int>> data(n);
  rep(i, 0, n) {
    int l, r;
    std::cin >> l >> r;
    r++;
    data.at(i) = std::make_tuple(r - l, l, r);
  }
  std::sort(all(data));
  std::reverse(all(data));
  // debug(data);

  int N = m + 2;
  auto seg = make_segment_tree<int>(N, std::plus<int>{}, 0);
  int rest = n;
  rep(d, 1, m + 1) {
    assert(rest == (int)data.size());
    int ans = rest;
    for (int j = d; j <= m; j += d) {
      ans += seg.query(0, j + 1);
    }
    while (!data.empty()) {
      int dif, l, r;
      std::tie(dif, l, r) = data.back();
      assert(d <= dif);
      if (d < dif)
        break;
      data.pop_back();
      rest--;
      seg.at(r)--;
      seg.at(l)++;
      seg.build_oneline(r);
      seg.build_oneline(l);
    }
    // debug(seg.collect());
    std::cout << ans << std::endl;
  }
  return 0;
}