Problem p02840 (C++)

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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9 + 7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define POSSIBLE cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define pii pair<int32, int32>
#define pll pair<int64, int64>
#define pb(a) push_back(a)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int64 n, x, d;
  cin >> n >> x >> d;
  map<int32, vector<pll>> mp;
  REP(k, n + 1) {
    int32 idx = ((k * x) % d + d) % d;
    mp[idx].emplace_back(((k * x - idx) / d + k * (k - 1) / 2),
                         ((k * x - idx) / d + k * (2 * n - k - 1) / 2));
  }
  int64 ans = 0;
  for (auto p : mp) {
    // cout << p.first << endl;
    // for(auto q : p.second){
    //   cout << q.first << " " << q.second << endl;
    // }
    // cout << endl;
    vector<pll> evt;
    for (auto q : p.second) {
      evt.emplace_back(q.first, 1);
      evt.emplace_back(q.second + 1, -1);
    }
    sort(ALL(evt));
    int32 cur = 0;
    int32 pre = -INF;
    for (auto q : evt) {
      if (cur)
        ans += q.first - pre;
      pre = q.first;
      cur += q.second;
    }
  }
  ANS(ans);
  return 0;
}

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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9 + 7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define POSSIBLE cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define pii pair<int32, int32>
#define pll pair<int64, int64>
#define pb(a) push_back(a)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int64 n, x, d;
  cin >> n >> x >> d;

  if (d == 0) {
    if (x != 0) {
      ANS(n + 1);
    } else {
      ANS(1);
    }
    return 0;
  }

  map<int32, vector<pll>> mp;
  REP(k, n + 1) {
    int32 idx = ((k * x) % d + d) % d;
    mp[idx].emplace_back(((k * x - idx) / d + k * (k - 1) / 2),
                         ((k * x - idx) / d + k * (2 * n - k - 1) / 2));
  }
  int64 ans = 0;
  for (auto p : mp) {
    // cout << p.first << endl;
    // for(auto q : p.second){
    //   cout << q.first << " " << q.second << endl;
    // }
    // cout << endl;
    vector<pll> evt;
    for (auto q : p.second) {
      evt.emplace_back(q.first, 1);
      evt.emplace_back(q.second + 1, -1);
    }
    sort(ALL(evt));
    int32 cur = 0;
    int32 pre = -INF;
    for (auto q : evt) {
      if (cur)
        ans += q.first - pre;
      pre = q.first;
      cur += q.second;
    }
  }
  ANS(ans);
  return 0;
}