Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

#define PI 3.1415926535897932384626433832795

struct solve {
  solve() {}
  ll operator()(ll n, ll x, ll d) {
    if (d < 0)
      x = -x, d = -d;

    ll ans = 0;
    map<int, vpll> m;
    for (ll i = 0; i <= n; i++) {
      ll base = x * i;
      ll s = i * (i - 1) / 2;
      ll e = (n - 1) * i - i * (i - 1) / 2;
      int r = base % d;
      if (r < 0)
        r += d;
      m[r].push_back(make_pair((base - r) / d + s, (base - r) / d + e));
    }

    for (auto &mm : m) {
      auto &v = mm.second;
      // cout << "r=" << r << "  v.size()=" << v.size() << "\n";
      sort(v.begin(), v.end());
      int vs = v.size();
      ll s, e;
      tie(s, e) = v[0];
      for (int i = 1; i < vs; i++) {
        ll cs, ce;
        tie(cs, ce) = v[i];
        if (e + 1 < cs) {
          ans += e - s + 1;
          tie(s, e) = v[i];
        } else {
          max_u(e, ce);
        }
      }
      ans += e - s + 1;
    }
    return ans;
  }
};

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

  for (;;) {
    int n, x, d;
    cin >> n >> x >> d;
    if (cin.fail())
      break;

    cout << solve()(n, x, d) << "\n";
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

#define PI 3.1415926535897932384626433832795

struct solve {
  solve() {}
  ll operator()(ll n, ll x, ll d) {
    if (d == 0) {
      if (x == 0)
        return 1;
      return n + 1;
    }
    if (d < 0)
      x = -x, d = -d;

    ll ans = 0;
    map<int, vpll> m;
    for (ll i = 0; i <= n; i++) {
      ll base = x * i;
      ll s = i * (i - 1) / 2;
      ll e = (n - 1) * i - i * (i - 1) / 2;
      int r = base % d;
      if (r < 0)
        r += d;
      m[r].push_back(make_pair((base - r) / d + s, (base - r) / d + e));
    }

    for (auto &mm : m) {
      auto &v = mm.second;
      // cout << "r=" << r << "  v.size()=" << v.size() << "\n";
      sort(v.begin(), v.end());
      int vs = v.size();
      ll s, e;
      tie(s, e) = v[0];
      for (int i = 1; i < vs; i++) {
        ll cs, ce;
        tie(cs, ce) = v[i];
        if (e + 1 < cs) {
          ans += e - s + 1;
          tie(s, e) = v[i];
        } else {
          max_u(e, ce);
        }
      }
      ans += e - s + 1;
    }
    return ans;
  }
};

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

  for (;;) {
    int n, x, d;
    cin >> n >> x >> d;
    if (cin.fail())
      break;

    cout << solve()(n, x, d) << "\n";
  }
  return 0;
}