Problem p02840 (C++)

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

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define rer(i, l, u) for (int(i) = (int)(l); (i) <= (int)(u); ++(i))
#define reu(i, l, u) for (int(i) = (int)(l); (i) < (int)(u); ++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r, v) auto r = (v)
#else
#define aut(r, v) __typeof(v) r = (v)
#endif
#define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it)
#define ktya(x) sort(all(x))
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define all(aa) (aa).begin(), (aa).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset(m, v, sizeof(m))
#define INF 1e18
#define INFLL 1000000000000000007LL
#define SIZE 200105
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define PI (acos(-1))
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef pair<int, ll> pill;
typedef pair<ll, int> plli;
typedef pair<double, int> pdi;
template <typename T, typename U> inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
typedef complex<double> P;
typedef pair<pill, int> PA;
// ll MOD;
ll MOD = 1000000007;
// ll MOD=998244353;
typedef ll Weight;
struct Edge {
  int src, dst;
  ll weight;
};
int N;
ll X, D;
ll gcdl(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcdl(b, a % b);
}
vector<pair<ll, ll>> datas[214514];
int main() {
  cin >> N >> X >> D;
  ll A = X - D;
  ll G = gcdl(abs(A), abs(D));
  ll x = D / G, y = A / G; // y*D=x*A
  if (x < 0) {
    x = -x;
    y = -y;
  }
  if (x < N) {
    ll ans = 0;
    ll lef = 0, rig = 0;
    datas[0].pb(mp((ll)0, (ll)0));
    rep(i, N) {
      lef += (ll)(i + 1);
      rig += (ll)(N - i);
      int anum = i + 1;
      int sho = anum / x;
      datas[anum % x].pb(mp(lef + y * sho, rig + y * sho));
    }
    rep(i, x) {
      sort(all(datas[i]));
      ll lef = datas[i][0].first, rig = datas[i][0].second;
      rep(j, datas[i].size()) {
        ll cl = datas[i][j].first, cr = datas[i][j].second;
        if (cr < lef || rig < cl) {
          ans += rig - lef + 1;
          lef = cl;
          rig = cr;
        } else {
          lef = min(lef, cl);
          rig = max(rig, cr);
        }
      }
      ans += rig - lef + 1;
    }
    cout << ans << endl;
  } else {
    ll ans = 1;
    ll lef = 0, rig = 0;
    rep(i, N) {
      lef += (ll)(i + 1);
      rig += (ll)(N - i);
      ans += (rig - lef + 1);
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define rer(i, l, u) for (int(i) = (int)(l); (i) <= (int)(u); ++(i))
#define reu(i, l, u) for (int(i) = (int)(l); (i) < (int)(u); ++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r, v) auto r = (v)
#else
#define aut(r, v) __typeof(v) r = (v)
#endif
#define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it)
#define ktya(x) sort(all(x))
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define all(aa) (aa).begin(), (aa).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset(m, v, sizeof(m))
#define INF 1e18
#define INFLL 1000000000000000007LL
#define SIZE 200105
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define PI (acos(-1))
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef pair<int, ll> pill;
typedef pair<ll, int> plli;
typedef pair<double, int> pdi;
template <typename T, typename U> inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
typedef complex<double> P;
typedef pair<pill, int> PA;
// ll MOD;
ll MOD = 1000000007;
// ll MOD=998244353;
typedef ll Weight;
struct Edge {
  int src, dst;
  ll weight;
};
int N;
ll X, D;
ll gcdl(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcdl(b, a % b);
}
vector<pair<ll, ll>> datas[214514];
int main() {
  cin >> N >> X >> D;
  if (D == 0) {
    if (X != 0)
      cout << N + 1 << endl;
    else
      cout << 1 << endl;
    return 0;
  }
  ll A = X - D;
  ll G = gcdl(abs(A), abs(D));
  ll x = D / G, y = A / G; // y*D=x*A
  if (x < 0) {
    x = -x;
    y = -y;
  }
  if (x < N) {
    ll ans = 0;
    ll lef = 0, rig = 0;
    datas[0].pb(mp((ll)0, (ll)0));
    rep(i, N) {
      lef += (ll)(i + 1);
      rig += (ll)(N - i);
      int anum = i + 1;
      int sho = anum / x;
      datas[anum % x].pb(mp(lef + y * sho, rig + y * sho));
    }
    rep(i, x) {
      sort(all(datas[i]));
      ll lef = datas[i][0].first, rig = datas[i][0].second;
      rep(j, datas[i].size()) {
        ll cl = datas[i][j].first, cr = datas[i][j].second;
        if (cr < lef || rig < cl) {
          ans += rig - lef + 1;
          lef = cl;
          rig = cr;
        } else {
          lef = min(lef, cl);
          rig = max(rig, cr);
        }
      }
      ans += rig - lef + 1;
    }
    cout << ans << endl;
  } else {
    ll ans = 1;
    ll lef = 0, rig = 0;
    rep(i, N) {
      lef += (ll)(i + 1);
      rig += (ll)(N - i);
      ans += (rig - lef + 1);
    }
    cout << ans << endl;
  }
}