Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P {
  long long x, y;
  bool operator<(const P &a) const {
    // if(y!=a.y)
    return y > a.y;
  };
};

int a, b, c, d, i, k, n, m, e, dx[10] = {1, 0, -1, 0, 1, 1, -1, -1},
                               dy[10] = {0, 1, 0, -1, 1, -1, 1, -1};
int o[200101];
int l[631010];
int j[200102];

long long x, y, z, mod = 1000000007;
// string r;
char r[1];

P u[1];
queue<int> q;

// stack<int> s;
set<int> s;
map<int, int> p;
vector<P> v[200101];

bool as(P a, P b) {
  if (a.x == b.x)
    return a.y < b.y;
  return a.x < b.x;
}

long long f(long long n, long long m) {
  if (m < n)
    return 0;
  long long x = (n + m) * (m - n + 1) / 2;
  return x * c;
}

int main() {
  scanf("%d %d %d", &a, &b, &c);
  d = abs(2 * c);
  for (long long t = 0; t <= a; t++) {
    y = t * b - (a - t) * b;
    int i;
    if (p[(y % d + d) % d])
      i = p[(y % d + d) % d];
    else
      e++, p[(y % d + d) % d] = i = e;
    v[i].push_back(
        {y - f(t, a - 1) + f(0, t - 1), y + f(a - t, a - 1) - f(0, a - t - 1)});
    if (v[i].back().x > v[i].back().y)
      swap(v[i][v[i].size() - 1].x, v[i][v[i].size() - 1].y);
    // printf("%lld %lld\n",v[i].back().x,v[i].back().y);
  }
  for (int t = 1; t <= e; t++) {
    sort(v[t].begin(), v[t].end(), as);
    z = v[t][0].x - 1;
    // x++;
    for (int i = 0; i < v[t].size(); i++)
      if (z < v[t][i].y) {
        if (z >= v[t][i].x)
          x += abs((v[t][i].y - z) / (2 * c));
        else
          x += abs((v[t][i].y - v[t][i].x) / (2 * c)) + 1;
        z = v[t][i].y;
      }
  }
  printf("%lld", x);
}


Fixed submission:
#include <bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;

struct P {
  long long x, y;
  bool operator<(const P &a) const {
    // if(y!=a.y)
    return y > a.y;
  };
};

int a, b, c, d, i, k, n, m, e, dx[10] = {1, 0, -1, 0, 1, 1, -1, -1},
                               dy[10] = {0, 1, 0, -1, 1, -1, 1, -1};
int o[200101];
int l[631010];
int j[200102];

long long x, y, z, mod = 1000000007;
// string r;
char r[1];

P u[1];
queue<int> q;

// stack<int> s;
set<int> s;
map<int, int> p;
vector<P> v[200101];

bool as(P a, P b) {
  if (a.x == b.x)
    return a.y < b.y;
  return a.x < b.x;
}

long long f(long long n, long long m) {
  if (m < n)
    return 0;
  long long x = (n + m) * (m - n + 1) / 2;
  return x * c;
}

int main() {
  scanf("%d %d %d", &a, &b, &c);
  if (c == 0) {
    if (b)
      printf("%d", a + 1);
    else
      puts("1");
    return 0;
  }
  d = abs(2 * c);
  for (long long t = 0; t <= a; t++) {
    y = t * b - (a - t) * b;
    int i;
    if (p[(y % d + d) % d])
      i = p[(y % d + d) % d];
    else
      e++, p[(y % d + d) % d] = i = e;
    v[i].push_back(
        {y - f(t, a - 1) + f(0, t - 1), y + f(a - t, a - 1) - f(0, a - t - 1)});
    if (v[i].back().x > v[i].back().y)
      swap(v[i][v[i].size() - 1].x, v[i][v[i].size() - 1].y);
    // printf("%lld %lld\n",v[i].back().x,v[i].back().y);
  }
  for (int t = 1; t <= e; t++) {
    sort(v[t].begin(), v[t].end(), as);
    z = v[t][0].x - 1;
    // x++;
    for (int i = 0; i < v[t].size(); i++)
      if (z < v[t][i].y) {
        if (z >= v[t][i].x)
          x += abs((v[t][i].y - z) / (2 * c));
        else
          x += abs((v[t][i].y - v[t][i].x) / (2 * c)) + 1;
        z = v[t][i].y;
      }
  }
  printf("%lld", x);
}