Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

ll mod = 1e9 + 7;
const ld error = 1e-5;
const ld PI = acosl(-1); // const ld PI = acosl(-1)

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define eq(x, y) (fabs((x) - (y)) < error)
#define bt(i) (1LL << (i))

#define debug(x) cerr << #x << " = " << (x) << "\n"
#define hoise cerr << "hoise - " << __LINE__ << "\n"
#define tham getchar()
mt19937 rng((unsigned)chrono::system_clock::now()
                .time_since_epoch()
                .count()); // mt199937_64 for ll

inline ll MOD(ll x, ll m = mod) {
  ll y = x % m;
  return (y >= 0) ? y : y + m;
}

const int inf = 1e9;
const ll infl = 1e18 + 1;
const int nmax = 80 + 5;
///===========================================  template
///=======================================================

ll sum(ll a, ll n) {
  ll b = a + n - 1;
  ll ans = (a + b) * (b - a + 1);
  return ans / 2;
}

int main() {
  FASTIO;

  ll n, x, d;
  cin >> n >> x >> d;

  if (d < 0) {
    x = x + (n - 1) * d;
    d = -d;
  }

  map<ll, int> rem;

  vector<vector<pair<ll, ll>>> ranges; /// 0 begin, 1 end
  for (int i = 0; i <= n; i++) {
    ll sum_x = x * i;
    ll r = MOD(sum_x, d);

    if (rem.find(r) == rem.end()) {
      rem[r] = ranges.size();
      ranges.emplace_back();
    }
    r = rem[r];

    ll sum_d_first = sum(0, i);
    ll sum_d_second = sum(n - i, i);

    //        debug(i);
    //        debug(sum_x);
    //        debug(sum_d_first);
    //        debug(sum_d_second);

    ranges[r].emplace_back(sum_x + sum_d_first * d, 0);
    ranges[r].emplace_back(sum_x + sum_d_second * d, 1);
    ////
    //        debug(sum_x + sum_d_first*d);
    //        debug(sum_x + sum_d_second*d);
  }

  for (int i = 0; i < ranges.size(); i++)
    sort(ranges[i].begin(), ranges[i].end());

  ll ans = 0;
  for (int r = 0; r < ranges.size(); r++) {
    ll last = 0;
    int cnt = 0;
    for (auto p : ranges[r]) {
      //            cout<<p.first<<" "<<p.second<<"\n";
      if (cnt == 0)
        last = p.first;
      cnt += (p.second == 0) ? 1 : -1;
      if (cnt == 0)
        ans += ((p.first - last) / d + 1);
    }
  }

  cout << ans << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

ll mod = 1e9 + 7;
const ld error = 1e-5;
const ld PI = acosl(-1); // const ld PI = acosl(-1)

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define eq(x, y) (fabs((x) - (y)) < error)
#define bt(i) (1LL << (i))

#define debug(x) cerr << #x << " = " << (x) << "\n"
#define hoise cerr << "hoise - " << __LINE__ << "\n"
#define tham getchar()
mt19937 rng((unsigned)chrono::system_clock::now()
                .time_since_epoch()
                .count()); // mt199937_64 for ll

inline ll MOD(ll x, ll m = mod) {
  ll y = x % m;
  return (y >= 0) ? y : y + m;
}

const int inf = 1e9;
const ll infl = 1e18 + 1;
const int nmax = 80 + 5;
///===========================================  template
///=======================================================

ll sum(ll a, ll n) {
  ll b = a + n - 1;
  ll ans = (a + b) * (b - a + 1);
  return ans / 2;
}

int main() {
  FASTIO;

  ll n, x, d;
  cin >> n >> x >> d;

  if (d == 0) {
    if (x == 0)
      cout << 1;
    else
      cout << n + 1;
    cout << "\n";
    return 0;
  }

  if (d < 0) {
    x = x + (n - 1) * d;
    d = -d;
  }

  map<ll, int> rem;

  vector<vector<pair<ll, ll>>> ranges; /// 0 begin, 1 end
  for (int i = 0; i <= n; i++) {
    ll sum_x = x * i;
    ll r = MOD(sum_x, d);

    if (rem.find(r) == rem.end()) {
      rem[r] = ranges.size();
      ranges.emplace_back();
    }
    r = rem[r];

    ll sum_d_first = sum(0, i);
    ll sum_d_second = sum(n - i, i);

    //        debug(i);
    //        debug(sum_x);
    //        debug(sum_d_first);
    //        debug(sum_d_second);

    ranges[r].emplace_back(sum_x + sum_d_first * d, 0);
    ranges[r].emplace_back(sum_x + sum_d_second * d, 1);
    ////
    //        debug(sum_x + sum_d_first*d);
    //        debug(sum_x + sum_d_second*d);
  }

  for (int i = 0; i < ranges.size(); i++)
    sort(ranges[i].begin(), ranges[i].end());

  ll ans = 0;
  for (int r = 0; r < ranges.size(); r++) {
    ll last = 0;
    int cnt = 0;
    for (auto p : ranges[r]) {
      //            cout<<p.first<<" "<<p.second<<"\n";
      if (cnt == 0)
        last = p.first;
      cnt += (p.second == 0) ? 1 : -1;
      if (cnt == 0)
        ans += ((p.first - last) / d + 1);
    }
  }

  cout << ans << "\n";

  return 0;
}