Problem p02840 (C++)

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, X, D;
  cin >> N >> X >> D;

  if (X == 0) {
    if (D == 0) {
      cout << 1 << endl;
    } else {
      cout << (N - 1) * N / 2 + 1 << endl;
    }
    return 0;
  }
  if (D < 0) {
    X = X + (N - 1) * D;
    D = -D;
  }
  vector<pll> ps[202020];
  map<ll, ll> mp;
  ll idx = 0;
  ll lb = 0, ub = 0;
  REP(k, N + 1) {
    ll L = k * X + lb * D;
    ll R = k * X + ub * D;
    // dump(L)
    // dump(R)
    if (mp.find((k * X) % D) == mp.end()) {
      mp[(k * X) % D] = idx;
      ps[idx].push_back({L, R});
      idx++;
    } else {
      ps[mp[(k * X) % D]].push_back({L, R});
    }
    lb += k;
    ub += (N - 1 - k);
  }

  // cout << ps[0] << endl;

  ll res = 0;
  REP(i, idx) {
    sort(ps[i].begin(), ps[i].end());
    ll left = ps[i][0].first;
    ll right = ps[i][0].second;
    REP(j, ps[i].size()) {
      if (j == 0)
        continue;
      if (ps[i][j].first > right) {
        if (j > 0)
          res += ((right - left) / D + 1);
        left = ps[i][j].first;
        right = ps[i][j].second;
      } else {
        right = max(right, ps[i][j].second);
      }
      // dump(res)
    }
    res += ((right - left) / D + 1);
  }

  cout << res << endl;
  return 0;
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, X, D;
  cin >> N >> X >> D;

  if (X == 0) {
    if (D == 0) {
      cout << 1 << endl;
    } else {
      cout << (N - 1) * N / 2 + 1 << endl;
    }
    return 0;
  }
  if (D == 0) {
    cout << N + 1 << endl;
    return 0;
  }
  if (D < 0) {
    X = X + (N - 1) * D;
    D = -D;
  }
  vector<pll> ps[202020];
  map<ll, ll> mp;
  ll idx = 0;
  ll lb = 0, ub = 0;
  REP(k, N + 1) {
    ll L = k * X + lb * D;
    ll R = k * X + ub * D;
    // dump(L)
    // dump(R)
    if (mp.find((k * X) % D) == mp.end()) {
      mp[(k * X) % D] = idx;
      ps[idx].push_back({L, R});
      idx++;
    } else {
      ps[mp[(k * X) % D]].push_back({L, R});
    }
    lb += k;
    ub += (N - 1 - k);
  }

  // cout << ps[0] << endl;

  ll res = 0;
  REP(i, idx) {
    sort(ps[i].begin(), ps[i].end());
    ll left = ps[i][0].first;
    ll right = ps[i][0].second;
    REP(j, ps[i].size()) {
      if (j == 0)
        continue;
      if (ps[i][j].first > right) {
        if (j > 0)
          res += ((right - left) / D + 1);
        left = ps[i][j].first;
        right = ps[i][j].second;
      } else {
        right = max(right, ps[i][j].second);
      }
      // dump(res)
    }
    res += ((right - left) / D + 1);
  }

  cout << res << endl;
  return 0;
}