Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr ll MOD = 1e9 + 7;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
vector<P> vs[200010];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, x, d;
  cin >> n >> x >> d;
  ll y = x - d;
  if (d == 0) {
    if (y == 0) {
      cout << 1 << endl;
    } else {
      cout << n + 1 << endl;
    }
  }
  if (d < 0) {
    y *= -1LL;
    d *= -1LL;
  }
  ll res = 0;
  vector<ll> v;
  for (ll k = 0; k <= n; k++) {
    v.push_back((k * y) % d);
  }
  sort(all(v));
  v.erase(unique(all(v)), v.end());
  for (ll k = 0; k <= n; k++) {
    int id = lower_bound(all(v), (k * y) % d) - v.begin();
    ll l = k * y / d + k * (k + 1LL) / 2LL,
       r = k * y / d + k * (n - k + 1LL + n) / 2LL;
    vs[id].push_back(P(l, r));
    //  cout << id << " " << l << " " << r << endl;
  }
  for (int i = 0; i < v.size(); i++) {
    vector<ll> vv;
    for (auto &p : vs[i]) {
      vv.push_back(p.first);
      vv.push_back(p.second + 1);
    }
    sort(all(vv));
    vv.erase(unique(all(vv)), vv.end());
    vector<ll> sum(vv.size() + 1);
    for (auto &p : vs[i]) {
      int l = lower_bound(all(vv), p.first) - vv.begin();
      int r = lower_bound(all(vv), p.second + 1) - vv.begin();
      sum[l]++;
      sum[r]--;
    }
    for (int i = 1; i < vv.size(); i++) {
      sum[i] += sum[i - 1];
    }
    for (int i = 0; i + 1 < vv.size(); i++) {
      if (sum[i] > 0) {
        res += vv[i + 1] - vv[i];
      }
    }
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr ll MOD = 1e9 + 7;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
vector<P> vs[200010];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, x, d;
  cin >> n >> x >> d;
  ll y = x - d;
  if (d == 0) {
    if (y == 0) {
      cout << 1 << endl;
    } else {
      cout << n + 1 << endl;
    }
    return 0;
  }
  if (d < 0) {
    y *= -1LL;
    d *= -1LL;
  }
  ll res = 0;
  vector<ll> v;
  for (ll k = 0; k <= n; k++) {
    v.push_back((k * y) % d);
  }
  sort(all(v));
  v.erase(unique(all(v)), v.end());
  for (ll k = 0; k <= n; k++) {
    int id = lower_bound(all(v), (k * y) % d) - v.begin();
    ll l = k * y / d + k * (k + 1LL) / 2LL,
       r = k * y / d + k * (n - k + 1LL + n) / 2LL;
    vs[id].push_back(P(l, r));
    //  cout << id << " " << l << " " << r << endl;
  }
  for (int i = 0; i < v.size(); i++) {
    vector<ll> vv;
    for (auto &p : vs[i]) {
      vv.push_back(p.first);
      vv.push_back(p.second + 1);
    }
    sort(all(vv));
    vv.erase(unique(all(vv)), vv.end());
    vector<ll> sum(vv.size() + 1);
    for (auto &p : vs[i]) {
      int l = lower_bound(all(vv), p.first) - vv.begin();
      int r = lower_bound(all(vv), p.second + 1) - vv.begin();
      sum[l]++;
      sum[r]--;
    }
    for (int i = 1; i < vv.size(); i++) {
      sum[i] += sum[i - 1];
    }
    for (int i = 0; i + 1 < vv.size(); i++) {
      if (sum[i] > 0) {
        res += vv[i + 1] - vv[i];
      }
    }
  }
  cout << res << endl;
}