Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#ifndef DEBUG
#define NDEBUG
#endif
#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <limits>
#include <numeric>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;

// template {{{
// misc {{{
using Z = long long;
[[maybe_unused]] constexpr Z INF = numeric_limits<Z>::max() / 3;
// }}}

// debug {{{
template <class T>
using enable_if_sequence_t = enable_if_t<
    is_same<typename iterator_traits<typename remove_cv_t<
                remove_reference_t<T>>::iterator>::iterator_category,
            random_access_iterator_tag>{}>;
#ifdef NDEBUG
#define dump(x)
#define debug if (0)
template <class T> inline T &&ix(T &&value) { return value; }
template <class T, class Index, class... Indices,
          class = enable_if_sequence_t<T>>
inline auto &&ix(T &&container, Index index, Indices... indices) {
  return ix(container[index], indices...);
}
#else
#define dump(x)                                                                \
  do {                                                                         \
    cerr << "\e[33m[dump: " << __LINE__ << "] " << (#x) << " = " << x          \
         << "\e[0m" << endl;                                                   \
  } while (0);
#define debug
template <class T>
inline auto &&ix_impl([[maybe_unused]] int line,
                      [[maybe_unused]] const char *code, T &&value) {
  return value;
}
template <class T, class Index, class... Indices,
          class = enable_if_sequence_t<T>>
inline auto &&ix_impl(int line, const char *code, T &&container, Index index,
                      Indices... indices) {
  if (index < 0) {
    cerr << "\e[31mix: negative index at line " << line << ", ix(" << code
         << ")\e[0m" << endl;
    exit(1);
  }
  int size = container.size();
  if (index >= size) {
    cerr << "\e[31mix: index out of range at line " << line << ", ix(" << code
         << ")\n"
         << "index = " << index << " whereas size = " << size << "\e[0m"
         << endl;
    exit(1);
  }
  return ix_impl(line, code, container[index], indices...);
}
#define ix(...) ix_impl(__LINE__, #__VA_ARGS__, __VA_ARGS__)
#endif
// }}}

// IO {{{
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> false_type is_container_impl(...);
template <class T, class = typename T::iterator> true_type is_container_impl(T);
template <class T>
struct is_container : decltype(is_container_impl<T>(declval<T>())) {};
template <> struct is_container<string> : std::false_type {};
template <class Container, class = enable_if_t<is_container<Container>{}>>
istream &operator>>(istream &is, Container &cont) {
  for (auto &v : cont)
    is >> v;
  return is;
}
template <class Container, class = enable_if_t<is_container<Container>{}>>
ostream &operator<<(ostream &os, Container const &cont) {
  bool first = true;
  constexpr char sep =
      is_container<typename Container::value_type>{} ? '\n' : ' ';
  for (auto const &v : cont) {
    if (!first) {
      os << sep;
    }
    first = false;
    os << v;
  }
  return os;
}
// }}}
// }}}

// {{{ extgcd
template <class T> struct extgcd_solution {
  T gcd, x, y;
};

template <class T = Z> constexpr extgcd_solution<T> extgcd(T a, T b) {
  if (b == 0)
    return {a, 1, 0};
  if (a < b) {
    auto res = extgcd(b, a);
    return {res.gcd, res.y, res.x};
  }
  auto prev = extgcd(b, a % b);
  return {prev.gcd, prev.y, prev.x - prev.y * (a / b)};
}
// }}}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  Z N, X, D;
  cin >> N >> X >> D;
  Z g = extgcd(abs(X), abs(D)).gcd;
  X /= g;
  D /= g;
  if (D < 0) {
    X *= -1;
    D *= -1;
  }
  dump(X);
  dump(D);
  Z ans = 0;
  for (Z i = 0; i < D; i++) {
    if (i > N)
      break;
    dump(i);
    set<pair<Z, Z>> intervals;
    for (Z j = i; j <= N; j += D) {
      Z l = (j - 1) * j / 2, r = (N - j + N - 1) * j / 2;
      intervals.emplace(j * X + l * D, j * X + r * D + D);
    }
    dump(intervals);
    while (intervals.size() >= 2) {
      auto fst = *begin(intervals), snd = *next(begin(intervals));
      intervals.erase(begin(intervals));
      if (snd.first <= fst.second) {
        intervals.erase(begin(intervals));
        intervals.emplace(fst.first, max(fst.second, snd.second));
      } else {
        dump(fst);
        ans += (fst.second - fst.first) / D;
      }
    }
    dump(intervals);
    ans += (begin(intervals)->second - begin(intervals)->first) / D;
  }
  cout << ans << endl;
}


Fixed submission:
#ifndef DEBUG
#define NDEBUG
#endif
#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <limits>
#include <numeric>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;

// template {{{
// misc {{{
using Z = long long;
[[maybe_unused]] constexpr Z INF = numeric_limits<Z>::max() / 3;
// }}}

// debug {{{
template <class T>
using enable_if_sequence_t = enable_if_t<
    is_same<typename iterator_traits<typename remove_cv_t<
                remove_reference_t<T>>::iterator>::iterator_category,
            random_access_iterator_tag>{}>;
#ifdef NDEBUG
#define dump(x)
#define debug if (0)
template <class T> inline T &&ix(T &&value) { return value; }
template <class T, class Index, class... Indices,
          class = enable_if_sequence_t<T>>
inline auto &&ix(T &&container, Index index, Indices... indices) {
  return ix(container[index], indices...);
}
#else
#define dump(x)                                                                \
  do {                                                                         \
    cerr << "\e[33m[dump: " << __LINE__ << "] " << (#x) << " = " << x          \
         << "\e[0m" << endl;                                                   \
  } while (0);
#define debug
template <class T>
inline auto &&ix_impl([[maybe_unused]] int line,
                      [[maybe_unused]] const char *code, T &&value) {
  return value;
}
template <class T, class Index, class... Indices,
          class = enable_if_sequence_t<T>>
inline auto &&ix_impl(int line, const char *code, T &&container, Index index,
                      Indices... indices) {
  if (index < 0) {
    cerr << "\e[31mix: negative index at line " << line << ", ix(" << code
         << ")\e[0m" << endl;
    exit(1);
  }
  int size = container.size();
  if (index >= size) {
    cerr << "\e[31mix: index out of range at line " << line << ", ix(" << code
         << ")\n"
         << "index = " << index << " whereas size = " << size << "\e[0m"
         << endl;
    exit(1);
  }
  return ix_impl(line, code, container[index], indices...);
}
#define ix(...) ix_impl(__LINE__, #__VA_ARGS__, __VA_ARGS__)
#endif
// }}}

// IO {{{
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> false_type is_container_impl(...);
template <class T, class = typename T::iterator> true_type is_container_impl(T);
template <class T>
struct is_container : decltype(is_container_impl<T>(declval<T>())) {};
template <> struct is_container<string> : std::false_type {};
template <class Container, class = enable_if_t<is_container<Container>{}>>
istream &operator>>(istream &is, Container &cont) {
  for (auto &v : cont)
    is >> v;
  return is;
}
template <class Container, class = enable_if_t<is_container<Container>{}>>
ostream &operator<<(ostream &os, Container const &cont) {
  bool first = true;
  constexpr char sep =
      is_container<typename Container::value_type>{} ? '\n' : ' ';
  for (auto const &v : cont) {
    if (!first) {
      os << sep;
    }
    first = false;
    os << v;
  }
  return os;
}
// }}}
// }}}

// {{{ extgcd
template <class T> struct extgcd_solution {
  T gcd, x, y;
};

template <class T = Z> constexpr extgcd_solution<T> extgcd(T a, T b) {
  if (b == 0)
    return {a, 1, 0};
  if (a < b) {
    auto res = extgcd(b, a);
    return {res.gcd, res.y, res.x};
  }
  auto prev = extgcd(b, a % b);
  return {prev.gcd, prev.y, prev.x - prev.y * (a / b)};
}
// }}}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  Z N, X, D;
  cin >> N >> X >> D;
  if (D == 0) {
    cout << (X == 0 ? 1 : N + 1) << endl;
    return 0;
  }
  Z g = extgcd(abs(X), abs(D)).gcd;
  X /= g;
  D /= g;
  if (D < 0) {
    X *= -1;
    D *= -1;
  }
  dump(X);
  dump(D);
  Z ans = 0;
  for (Z i = 0; i < D; i++) {
    if (i > N)
      break;
    dump(i);
    set<pair<Z, Z>> intervals;
    for (Z j = i; j <= N; j += D) {
      Z l = (j - 1) * j / 2, r = (N - j + N - 1) * j / 2;
      intervals.emplace(j * X + l * D, j * X + r * D + D);
    }
    dump(intervals);
    while (intervals.size() >= 2) {
      auto fst = *begin(intervals), snd = *next(begin(intervals));
      intervals.erase(begin(intervals));
      if (snd.first <= fst.second) {
        intervals.erase(begin(intervals));
        intervals.emplace(fst.first, max(fst.second, snd.second));
      } else {
        dump(fst);
        ans += (fst.second - fst.first) / D;
      }
    }
    dump(intervals);
    ans += (begin(intervals)->second - begin(intervals)->first) / D;
  }
  cout << ans << endl;
}