Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
int main() {
  int n;
  cin >> n;
  ll x, d;
  cin >> x >> d;
  ll l[200020], r[200020];
  for (ll i = 0; i <= n; i++) {
    l[i] = (2 * i - n) * x - (ll)n * (n - 1) / 2 * d + i * (i - 1) * d;
    r[i] =
        (2 * i - n) * x + (ll)n * (n - 1) / 2 * d - (n - i) * (n - i - 1) * d;
    if (l[i] > r[i])
      swap(l[i], r[i]);
    // cout<<l[i]<<" "<<r[i]<<endl;
  }
  map<ll, vector<P>> mp;
  d = abs(d);
  for (int i = 0; i <= n; i++) {
    ll s = (l[i] % (2 * d) + 2 * d) % (2 * d);
    mp[s].push_back({l[i], r[i]});
  }
  for (auto &p : mp)
    sort(p.second.begin(), p.second.end());
  ll ans = 0;
  for (auto p : mp) {
    ll y = p.second[0].first, z = p.second[0].second;
    for (int i = 1; i < p.second.size(); i++) {
      if (z < p.second[i].first) {
        ans += (z - y) / (2 * d) + 1;
        y = p.second[i].first, z = p.second[i].second;
      } else {
        z = max(z, p.second[i].second);
      }
    }
    ans += (z - y) / (2 * d) + 1;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
int main() {
  int n;
  cin >> n;
  ll x, d;
  cin >> x >> d;
  if (d == 0) {
    if (x == 0) {
      cout << 1 << endl;
    } else {
      cout << n + 1 << endl;
    }
    return 0;
  }
  ll l[200020], r[200020];
  for (ll i = 0; i <= n; i++) {
    l[i] = (2 * i - n) * x - (ll)n * (n - 1) / 2 * d + i * (i - 1) * d;
    r[i] =
        (2 * i - n) * x + (ll)n * (n - 1) / 2 * d - (n - i) * (n - i - 1) * d;
    if (l[i] > r[i])
      swap(l[i], r[i]);
    // cout<<l[i]<<" "<<r[i]<<endl;
  }
  map<ll, vector<P>> mp;
  d = abs(d);
  for (int i = 0; i <= n; i++) {
    ll s = (l[i] % (2 * d) + 2 * d) % (2 * d);
    mp[s].push_back({l[i], r[i]});
  }
  for (auto &p : mp)
    sort(p.second.begin(), p.second.end());
  ll ans = 0;
  for (auto p : mp) {
    ll y = p.second[0].first, z = p.second[0].second;
    for (int i = 1; i < p.second.size(); i++) {
      if (z < p.second[i].first) {
        ans += (z - y) / (2 * d) + 1;
        y = p.second[i].first, z = p.second[i].second;
      } else {
        z = max(z, p.second[i].second);
      }
    }
    ans += (z - y) / (2 * d) + 1;
  }
  cout << ans << endl;
  return 0;
}