Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using lli = long long int;

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

  lli X, D;

  lli n, i, j, k;

  cin >> n >> X >> D;

  if (D < 0) {
    X = X + (n - 1) * D;
    D = -D;
  }
  /*
  cout << "n = " << n << "  X = " << X << "   D = " << D << endl;
  for(i = 0; i < n; ++i)
  {
      cout << X + D * i << " ";
  }
  cout << endl;
  */

  vector<pair<lli, lli>> seg;

  lli max_s, min_s;

  lli S, l, r;

  S = ((n - 1) * n) / 2;

  lli step = 2 * D;

  vector<tuple<lli, int, lli>> points;

  for (k = 0; k <= n; ++k) {
    min_s = (k * (k - 1)) / 2;
    max_s = n * k - ((k + 1) * k) / 2;

    l = D * (2 * min_s - S) + X * (2 * k - n);
    r = D * (2 * max_s - S) + X * (2 * k - n);

    // cout << "L = " << l << "  R = " << r << endl;

    seg.push_back({l, r});
    points.emplace_back(l, 0, k);
    points.emplace_back(r, 1, k);
  }

  sort(points.begin(), points.end());

  map<lli, int> rest;
  map<lli, lli> left;

  lli x, s, rst, ans;

  ans = 0;
  lli t;

  for (i = 0; i < points.size(); ++i) {
    tie(x, t, s) = points[i];

    if (t == 1) // right
    {
      l = seg[s].first;
      r = x;
      rst = ((l % step) + step) % step;

      rest[rst] -= 1;
      if (rest[rst] == 0) {
        ans += (r - left[rst]) / step + 1;
        rest.erase(rst);
      }

    } else {
      l = x;
      r = seg[s].second;
      rst = ((l % step) + step) % step;

      if (rest.count(rst) == 0) {
        rest[rst] = 1;
        left[rst] = l;
      } else {
        rest[rst] += 1;
      }
    }
  }

  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using lli = long long int;

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

  lli X, D;

  lli n, i, j, k;

  cin >> n >> X >> D;

  if (D < 0) {
    X = X + (n - 1) * D;
    D = -D;
  }

  if (D == 0) {
    if (X == 0) {
      cout << 1;
    } else {
      cout << n + 1;
    }
    return 0;
  }
  /*
  cout << "n = " << n << "  X = " << X << "   D = " << D << endl;
  for(i = 0; i < n; ++i)
  {
      cout << X + D * i << " ";
  }
  cout << endl;
  */

  vector<pair<lli, lli>> seg;

  lli max_s, min_s;

  lli S, l, r;

  S = ((n - 1) * n) / 2;

  lli step = 2 * D;

  vector<tuple<lli, int, lli>> points;

  for (k = 0; k <= n; ++k) {
    min_s = (k * (k - 1)) / 2;
    max_s = n * k - ((k + 1) * k) / 2;

    l = D * (2 * min_s - S) + X * (2 * k - n);
    r = D * (2 * max_s - S) + X * (2 * k - n);

    // cout << "L = " << l << "  R = " << r << endl;

    seg.push_back({l, r});
    points.emplace_back(l, 0, k);
    points.emplace_back(r, 1, k);
  }

  sort(points.begin(), points.end());

  map<lli, int> rest;
  map<lli, lli> left;

  lli x, s, rst, ans;

  ans = 0;
  lli t;

  for (i = 0; i < points.size(); ++i) {
    tie(x, t, s) = points[i];

    if (t == 1) // right
    {
      l = seg[s].first;
      r = x;
      rst = ((l % step) + step) % step;

      rest[rst] -= 1;
      if (rest[rst] == 0) {
        ans += (r - left[rst]) / step + 1;
        rest.erase(rst);
      }

    } else {
      l = x;
      r = seg[s].second;
      rst = ((l % step) + step) % step;

      if (rest.count(rst) == 0) {
        rest[rst] = 1;
        left[rst] = l;
      } else {
        rest[rst] += 1;
      }
    }
  }

  cout << ans;

  return 0;
}