Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

long long int gcd(long long int a, long long int b) {

  if ((a % b) == 0)
    return b;
  return gcd(b, a % b);
}

long long int kansu(long long int p, long long int q, long long int pp,
                    long long int qq) {
  //    cout << " " << p << " " << q << "   " << pp << " " << qq << endl;
  if (qq < p)
    return q - p + 1;
  if (q < pp)
    return q - p + 1;
  if (qq <= q && pp <= p)
    return q - qq;
  if (q <= qq && pp <= p)
    return 0;
  if (qq <= q)
    return (q - qq) + (pp - p);
  return pp - p;
}

int main() {

  long long int n, x, d;
  cin >> n >> x >> d;

  long long int gx = x;
  if (gx < 0)
    gx = -x;
  long long int gd = d;
  if (gd < 0)
    gd = -d;
  long long int g = gcd(gx, gd);
  if (g > 0) {
    gx = gx / g;
    gd = gd / g;
  }

  long long int p[214514] = {};
  long long int q[214514] = {};

  long long int ans = 0;

  for (long long int xx = 0; xx <= n; xx++) {
    p[xx] = (0 + xx - 1) * xx / 2;
    q[xx] = ((n - 1) + (n - 1 - (xx - 1))) * xx / 2;
    //        cout << p[xx] << " " << q[xx] << endl;
    if (x == 0) {
      if (d == 0) {
        cout << 1 << endl;
        return 0;
      } else {
        if (xx == n) {
          cout << q[xx] + 1 << endl;
          return 0;
        }
      }
    } else if (x < 0) {
      if (d == 0) {
        cout << n + 1 << endl;
        return 0;
      } else if (d < 0) {
        long long int y = xx - gd;
        if (y < 0) {
          ans += q[xx] - p[xx] + 1;
        } else {
          long long int pp = p[y] - gx;
          long long int qq = q[y] - gx;
          ans += kansu(p[xx], q[xx], pp, qq);
        }
      } else {
        long long int y = xx - gd;
        if (y < 0) {
          ans += q[xx] - p[xx] + 1;
        } else {
          long long int pp = p[y] + gx;
          long long int qq = q[y] + gx;
          ans += kansu(p[xx], q[xx], pp, qq);
        }
      }
    } else {
      if (d == 0) {
        cout << n + 1 << endl;
        return 0;
      } else if (d < 0) {
        long long int y = xx - gd;
        if (y < 0) {
          ans += q[xx] - p[xx] + 1;
        } else {
          long long int pp = p[y] + gx;
          long long int qq = q[y] + gx;
          ans += kansu(p[xx], q[xx], pp, qq);
        }
      } else {
        long long int y = xx - gd;
        if (y < 0) {
          ans += q[xx] - p[xx] + 1;
        } else {
          long long int pp = p[y] - gx;
          long long int qq = q[y] - gx;
          ans += kansu(p[xx], q[xx], pp, qq);
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

long long int gcd(long long int a, long long int b) {

  if ((a % b) == 0)
    return b;
  return gcd(b, a % b);
}

long long int kansu(long long int p, long long int q, long long int pp,
                    long long int qq) {
  //    cout << " " << p << " " << q << "   " << pp << " " << qq << endl;
  if (qq < p)
    return q - p + 1;
  if (q < pp)
    return q - p + 1;
  if (qq <= q && pp <= p)
    return q - qq;
  if (q <= qq && pp <= p)
    return 0;
  if (qq <= q)
    return (q - qq) + (pp - p);
  return pp - p;
}

int main() {

  long long int n, x, d;
  cin >> n >> x >> d;

  long long int gx = x;
  if (gx < 0)
    gx = -x;
  long long int gd = d;
  if (gd < 0)
    gd = -d;
  long long int g;
  if (x != 0 && d != 0) {
    g = gcd(gx, gd);
    gx = gx / g;
    gd = gd / g;
  }

  long long int p[214514] = {};
  long long int q[214514] = {};

  long long int ans = 0;

  for (long long int xx = 0; xx <= n; xx++) {
    p[xx] = (0 + xx - 1) * xx / 2;
    q[xx] = ((n - 1) + (n - 1 - (xx - 1))) * xx / 2;
    //        cout << p[xx] << " " << q[xx] << endl;
    if (x == 0) {
      if (d == 0) {
        cout << 1 << endl;
        return 0;
      } else {
        if (xx == n) {
          cout << q[xx] + 1 << endl;
          return 0;
        }
      }
    } else if (x < 0) {
      if (d == 0) {
        cout << n + 1 << endl;
        return 0;
      } else if (d < 0) {
        long long int y = xx - gd;
        if (y < 0) {
          ans += q[xx] - p[xx] + 1;
        } else {
          long long int pp = p[y] - gx;
          long long int qq = q[y] - gx;
          ans += kansu(p[xx], q[xx], pp, qq);
        }
      } else {
        long long int y = xx - gd;
        if (y < 0) {
          ans += q[xx] - p[xx] + 1;
        } else {
          long long int pp = p[y] + gx;
          long long int qq = q[y] + gx;
          ans += kansu(p[xx], q[xx], pp, qq);
        }
      }
    } else {
      if (d == 0) {
        cout << n + 1 << endl;
        return 0;
      } else if (d < 0) {
        long long int y = xx - gd;
        if (y < 0) {
          ans += q[xx] - p[xx] + 1;
        } else {
          long long int pp = p[y] + gx;
          long long int qq = q[y] + gx;
          ans += kansu(p[xx], q[xx], pp, qq);
        }
      } else {
        long long int y = xx - gd;
        if (y < 0) {
          ans += q[xx] - p[xx] + 1;
        } else {
          long long int pp = p[y] - gx;
          long long int qq = q[y] - gx;
          ans += kansu(p[xx], q[xx], pp, qq);
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}