Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-9;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

ll n, x, d;

ll f(ll m, ll t) {
  if (m >= 0)
    return m / d;
  return -((-m - 1) / d) - 1;
}

int main() {
  cin >> n >> x >> d;
  if (d == 0) {
    if (x == 0)
      cout << 1 << endl;
    else
      cout << n + 1 << endl;
  }
  if (d < 0) {
    d *= -1;
    x *= -1;
  }
  ll mn = 0, mx = 0, id = 0;
  map<ll, ll> mp;
  vector<vpll> a;
  for (int i = 0; i <= n; i++) {
    int t;
    if (mn >= 0)
      t = mn % d;
    else
      t = (mn - mn / d * d + d) % d;
    assert(0 <= t && t < d);
    if (mp.find(t) == mp.end()) {
      mp[t] = id++;
      a.push_back(vpll());
    }
    //		cout<<t<<' '<<mn<<' '<<mx<<' '<<f(mn,t)<<' '<<f(mx,t)<<endl;
    a[mp[t]].push_back({f(mn, t), f(mx, t)});
    mn += x + d * i;
    mx += x + d * (n - i - 1);
  }
  ll res = 0, m = a.size();
  for (int i = 0; i < m; i++) {
    sort(a[i].begin(), a[i].end());
    mx = -INF;
    ll tmp = 0;
    //		set<int> st;
    for (auto p : a[i]) {
      //			cout<<p.first<<' '<<p.second<<endl;
      if (mx < p.first) {
        tmp += p.second - p.first + 1;
        mx = p.second;
      } else if (mx < p.second) {
        tmp += p.second - mx;
        mx = p.second;
      }
      //			for(int j=p.first;j<=p.second;j++) st.insert(j);
      //			cout<<mx<<' '<<tmp<<endl;
    }
    //		cout<<tmp<<endl;
    //		cout<<st.size()<<endl;
    res += tmp;
  }
  //	cout<<st.size()<<endl;
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-9;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

ll n, x, d;

ll f(ll m, ll t) {
  if (m >= 0)
    return m / d;
  return -((-m - 1) / d) - 1;
}

int main() {
  cin >> n >> x >> d;
  if (d == 0) {
    if (x == 0)
      cout << 1 << endl;
    else
      cout << n + 1 << endl;
    return 0;
  }
  if (d < 0) {
    d *= -1;
    x *= -1;
  }
  ll mn = 0, mx = 0, id = 0;
  map<ll, ll> mp;
  vector<vpll> a;
  for (int i = 0; i <= n; i++) {
    int t;
    if (mn >= 0)
      t = mn % d;
    else
      t = (mn - mn / d * d + d) % d;
    assert(0 <= t && t < d);
    if (mp.find(t) == mp.end()) {
      mp[t] = id++;
      a.push_back(vpll());
    }
    //		cout<<t<<' '<<mn<<' '<<mx<<' '<<f(mn,t)<<' '<<f(mx,t)<<endl;
    a[mp[t]].push_back({f(mn, t), f(mx, t)});
    mn += x + d * i;
    mx += x + d * (n - i - 1);
  }
  ll res = 0, m = a.size();
  for (int i = 0; i < m; i++) {
    sort(a[i].begin(), a[i].end());
    mx = -INF;
    ll tmp = 0;
    //		set<int> st;
    for (auto p : a[i]) {
      //			cout<<p.first<<' '<<p.second<<endl;
      if (mx < p.first) {
        tmp += p.second - p.first + 1;
        mx = p.second;
      } else if (mx < p.second) {
        tmp += p.second - mx;
        mx = p.second;
      }
      //			for(int j=p.first;j<=p.second;j++) st.insert(j);
      //			cout<<mx<<' '<<tmp<<endl;
    }
    //		cout<<tmp<<endl;
    //		cout<<st.size()<<endl;
    res += tmp;
  }
  //	cout<<st.size()<<endl;
  cout << res << endl;
}