Problem p02840 (C++)

Buggy submission (status: Runtime Error):
//----------------------------templates
#pragma GCC optimize("Ofast")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
//----------------------------
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define int ll

#define FOR(i, j, n) for (int i = (int)(j); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPS(i, n) for (int i = 1; i <= (int)(n); i++)
#define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REPNS(i, n) for (int i = (int)(n); i > 0; i--)

#define I(n) scanf("%lld", &(n))
#define LL(n) scanf("%lld", &(n))
#define pb(n) push_back((n))
#define mp(i, j) make_pair((i), (j))
#define eb(i, j) emplace_back((i), (j))
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())

#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef vector<vvi> vvvi;

const int mod = 1000000007;

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

int n, x, d, ret;
signed main() {
  cin >> n >> x >> d;
  map<int, vpi> rem;

  REP(i, n + 1) {
    int ama = x * i % d;
    if (ama < 0)
      ama += d;
    int sho = (x * i - ama) / d;

    // 0, 1, ..., i-1
    int mini = i * (i - 1) / 2;
    // (n-i), ..., n-1
    int maxi = i * (2 * n - 1 - i) / 2;

    rem[ama].eb(mini + sho, maxi + sho + 1);
    // cerr << ama << " : [" << mini+sho << ", " << maxi+sho+1 << ")\n";
  }
  for (auto &p : rem) {
    auto event = p.second;
    sort(all(event));
    int lev = sz(event);
    int mini = INT64_MIN + 2;
    int maxi = INT64_MIN + 2;
    for (auto ev : event) {
      if (ev.first > maxi) {
        ret += maxi - mini;
        mini = ev.first;
        maxi = ev.second;
      } else {
        maxi = max(maxi, ev.second);
      }
    }
    ret += maxi - mini;
  }
  cout << ret << endl;
}

Fixed submission:
//----------------------------templates
#pragma GCC optimize("Ofast")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
//----------------------------
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define int ll

#define FOR(i, j, n) for (int i = (int)(j); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPS(i, n) for (int i = 1; i <= (int)(n); i++)
#define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REPNS(i, n) for (int i = (int)(n); i > 0; i--)

#define I(n) scanf("%lld", &(n))
#define LL(n) scanf("%lld", &(n))
#define pb(n) push_back((n))
#define mp(i, j) make_pair((i), (j))
#define eb(i, j) emplace_back((i), (j))
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())

#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef vector<vvi> vvvi;

const int mod = 1000000007;

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

int n, x, d, ret;
signed main() {
  cin >> n >> x >> d;
  map<int, vpi> rem;
  if (!d) {
    cout << (x ? n + 1 : 1) << endl;
    return 0;
  }

  REP(i, n + 1) {
    int ama = x * i % d;
    if (ama < 0)
      ama += d;
    int sho = (x * i - ama) / d;

    // 0, 1, ..., i-1
    int mini = i * (i - 1) / 2;
    // (n-i), ..., n-1
    int maxi = i * (2 * n - 1 - i) / 2;

    rem[ama].eb(mini + sho, maxi + sho + 1);
    // cerr << ama << " : [" << mini+sho << ", " << maxi+sho+1 << ")\n";
  }
  for (auto &p : rem) {
    auto event = p.second;
    sort(all(event));
    int lev = sz(event);
    int mini = INT64_MIN + 2;
    int maxi = INT64_MIN + 2;
    for (auto ev : event) {
      if (ev.first > maxi) {
        ret += maxi - mini;
        mini = ev.first;
        maxi = ev.second;
      } else {
        maxi = max(maxi, ev.second);
      }
    }
    ret += maxi - mini;
  }
  cout << ret << endl;
}