Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

// greatest common divisor and least common multiple
// gcd is calculated by Euclidean Algorithm
// lcm = m * n / gcd(m,n)
template <typename T = int> T gcd(T a, T b) {
  if (a < b)
    return gcd(b, a);
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

template <typename T = int> T lcm(T m, T n) {
  if ((0 == m) || (0 == n))
    return 0;
  return ((m / gcd(m, n)) * n);
}

int main() {
  ll n, x, d;
  cin >> n >> x >> d;
  if (d == 0) {
    cout << (x == 0 ? 1 : n + 1) << "\n";
    return 0;
  }
  ll g = gcd(abs(x), abs(d));
  if (d < 0)
    g *= -1;
  x /= g;
  d /= g;
  vector<vector<pair<ll, int>>> imos(min(n + 1, d));
  for (ll i = 0; i <= n; ++i) {
    ll l = (0 + i - 1) * (i) / 2;
    ll r = (n - 1 + n - i) * (i) / 2;
    l += x * i / d;
    r += x * i / d;
    ll idx = i % d;
    imos[idx].emplace_back(l, 1);
    imos[idx].emplace_back(r + 1, -1);
  }
  ll ret = 0;
  for (int i = 0; i < min(n + 1, d); ++i) {
    sort(imos[i].begin(), imos[i].end());
    ll prev = -INF;
    ll depth = 0;
    for (auto &p : imos[i]) {
      if (depth > 0) {
        ret += p.first - prev;
      }
      prev = p.first;
      depth += p.second;
    }
  }
  cout << ret << "\n";
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

// greatest common divisor and least common multiple
// gcd is calculated by Euclidean Algorithm
// lcm = m * n / gcd(m,n)
template <typename T = int> T gcd(T a, T b) {
  if (a < b)
    return gcd(b, a);
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

template <typename T = int> T lcm(T m, T n) {
  if ((0 == m) || (0 == n))
    return 0;
  return ((m / gcd(m, n)) * n);
}

int main() {
  ll n, x, d;
  cin >> n >> x >> d;
  if (d == 0) {
    cout << (x == 0 ? 1 : n + 1) << "\n";
    return 0;
  }
  if (x == 0) {
    d = 1;
  } else {
    ll g = gcd(abs(x), abs(d));
    if (d < 0)
      g *= -1;
    x /= g;
    d /= g;
  }
  vector<vector<pair<ll, int>>> imos(min(n + 1, d));
  for (ll i = 0; i <= n; ++i) {
    ll l = (0 + i - 1) * (i) / 2;
    ll r = (n - 1 + n - i) * (i) / 2;
    l += x * i / d;
    r += x * i / d;
    ll idx = i % d;
    imos[idx].emplace_back(l, 1);
    imos[idx].emplace_back(r + 1, -1);
  }
  ll ret = 0;
  for (int i = 0; i < min(n + 1, d); ++i) {
    sort(imos[i].begin(), imos[i].end());
    ll prev = -INF;
    ll depth = 0;
    for (auto &p : imos[i]) {
      if (depth > 0) {
        ret += p.first - prev;
      }
      prev = p.first;
      depth += p.second;
    }
  }
  cout << ret << "\n";
  return 0;
}