Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ld long double
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

/* Some Libraries */

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, x, d;
  cin >> n >> x >> d;
  if (d == 0) {
    cout << n + 1 << endl;
  } else if (d < 0) {
    d = -d;
    x = -x;
  }
  unordered_map<ll, int> Map;
  vector<vector<pll>> rs;
  int idx = 0;
  FOR(i, 0, n) {
    ll X = x * i;
    ll m = X % d;
    ll os = X / d;
    if (!Map.count(m)) {
      rs.pb(vector<pll>());
      Map[m] = idx++;
    }
    ll L = (ll)(i - 1) * i / 2 + os;
    ll R = (2 * n - i - 1) * i / 2 + os;
    rs[Map[m]].emplace_back(L, R);
  }
  ll ans = 0;
  for (auto r : rs) {
    sort(all(r));
    ll now = r[0].fi;
    for (auto e : r) {
      if (now > e.se)
        continue;
      if (now >= e.fi) {
        ans += e.se - now + 1;
      } else {
        ans += e.se - e.fi + 1;
      }
      now = e.se + 1;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ld long double
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

/* Some Libraries */

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, x, d;
  cin >> n >> x >> d;
  if (d == 0) {
    if (x != 0)
      cout << n + 1 << endl;
    if (x == 0)
      cout << 1 << endl;
    return 0;
  } else if (d < 0) {
    d = -d;
    x = -x;
  }
  unordered_map<ll, int> Map;
  vector<vector<pll>> rs;
  int idx = 0;
  FOR(i, 0, n) {
    ll X = x * i;
    ll m = X % d;
    ll os = X / d;
    if (!Map.count(m)) {
      rs.pb(vector<pll>());
      Map[m] = idx++;
    }
    ll L = (ll)(i - 1) * i / 2 + os;
    ll R = (2 * n - i - 1) * i / 2 + os;
    rs[Map[m]].emplace_back(L, R);
  }
  ll ans = 0;
  for (auto r : rs) {
    sort(all(r));
    ll now = r[0].fi;
    for (auto e : r) {
      if (now > e.se)
        continue;
      if (now >= e.fi) {
        ans += e.se - now + 1;
      } else {
        ans += e.se - e.fi + 1;
      }
      now = e.se + 1;
    }
  }
  cout << ans << endl;
  return 0;
}