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;
  map<int, vector<pll>> imos;
  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;
    imos[(i * x) % d].emplace_back(l, 1);
    imos[(i * x) % d].emplace_back(r + 1, -1);
  }
  ll ret = 0;
  for (auto &im : imos) {
    sort(im.second.begin(), im.second.end());
    ll prev = -INF;
    ll depth = 0;
    for (auto &p : im.second) {
      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);
  if (b == 0)
    return 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;
  map<int, vector<pll>> imos;
  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;
    imos[(i * x) % d].emplace_back(l, 1);
    imos[(i * x) % d].emplace_back(r + 1, -1);
  }
  ll ret = 0;
  for (auto &im : imos) {
    sort(im.second.begin(), im.second.end());
    ll prev = -INF;
    ll depth = 0;
    for (auto &p : im.second) {
      if (depth > 0) {
        ret += p.first - prev;
      }
      prev = p.first;
      depth += p.second;
    }
  }
  cout << ret << "\n";
  return 0;
}