Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define Inf 1000000000000000000
#define nmax_def 110000
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  if (b == 0)
    return 0;
  ll x;
  while (1) {
    x = a % b;
    if (x == 0)
      return b;
    else {
      a = b;
      b = x;
    }
  }
}

int main() {
  ll N, X, D;
  cin >> N >> X >> D;
  if (D < 0) {
    X *= -1;
    D *= -1;
  }
  ll ans = 0;
  if (D == 0) {
    if (X == 0)
      ans = 1;
    else
      ans = N + 1;
  } else {
    ll iprev_dist = D / gcd(X, D);
    vector<pll> sumLR(N + 1); // 取る個数
    rep(i, N + 1) {
      sumLR[i].first = X * i + D * i * (i - 1) / 2;
      sumLR[i].second = sumLR[i].first + D * i * (N - i);
      if (sumLR[i].first > sumLR[i].second)
        swap(sumLR[i].first, sumLR[i].second);
      ans += (sumLR[i].second - sumLR[i].first) / D + 1;

      int iprev = i - iprev_dist;
      if (iprev >= 0) {
        pll sumLR_excluded = sumLR[iprev];
        sumLR_excluded.first = max(sumLR_excluded.first, sumLR[i].first);
        sumLR_excluded.second = min(sumLR_excluded.second, sumLR[i].second);
        if (sumLR_excluded.first <= sumLR_excluded.second)
          ans -= (sumLR_excluded.second - sumLR_excluded.first) / D + 1;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define Inf 1000000000000000000
#define nmax_def 110000
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  if (b == 0)
    return 0;
  ll x;
  while (1) {
    x = a % b;
    if (x == 0)
      return b;
    else {
      a = b;
      b = x;
    }
  }
}

int main() {
  ll N, X, D;
  cin >> N >> X >> D;
  if (D < 0) {
    X *= -1;
    D *= -1;
  }
  ll ans = 0;
  if (D == 0) {
    if (X == 0)
      ans = 1;
    else
      ans = N + 1;
  } else {
    ll iprev_dist = D / gcd(abs(X), D);
    vector<pll> sumLR(N + 1); // 取る個数
    rep(i, N + 1) {
      sumLR[i].first = X * i + D * i * (i - 1) / 2;
      sumLR[i].second = sumLR[i].first + D * i * (N - i);
      if (sumLR[i].first > sumLR[i].second)
        swap(sumLR[i].first, sumLR[i].second);
      ans += (sumLR[i].second - sumLR[i].first) / D + 1;

      int iprev = i - iprev_dist;
      if (iprev >= 0) {
        pll sumLR_excluded = sumLR[iprev];
        sumLR_excluded.first = max(sumLR_excluded.first, sumLR[i].first);
        sumLR_excluded.second = min(sumLR_excluded.second, sumLR[i].second);
        if (sumLR_excluded.first <= sumLR_excluded.second)
          ans -= (sumLR_excluded.second - sumLR_excluded.first) / D + 1;
      }
    }
  }
  cout << ans << endl;

  return 0;
}