Problem p02840 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

ll a[225816], s[225816], l[225816], r[225816];
map<ll, int> mp;
vector<P> v[225816];

int main(void) {
  int n, i, j, p, m;
  bool flag;
  ll x, d, q, ans = 0ll;
  cin >> n >> a[0] >> d;
  for (i = 1; i < n; ++i) {
    a[i] = a[i - 1] + d;
  }
  if (d < 0) {
    reverse(a, a + n);
    d = -d;
  }
  for (i = 0; i < n; ++i) {
    a[i] *= 2;
    s[i + 1] = s[i] + a[i];
  }
  x = a[0];
  d *= 2;
  x %= d;
  x += d;
  x %= d;
  mp[0] = 0;
  v[0].push_back(P(-d, 1));
  v[0].push_back(P(0ll, -1));
  for (i = 1; i <= n; ++i) {
    q = x * i % d;
    if (mp.find(q) == mp.end()) {
      mp[q] = i;
    }
    p = mp[q];
    l[i] = s[i] - d;
    r[i] = s[n] - s[n - i];
    v[p].push_back(P(l[i], 1));
    v[p].push_back(P(r[i], -1));
  }
  for (i = 0; i <= n; ++i) {
    m = v[i].l_ength();
    sort(v[i].begin(), v[i].end());
    for (j = 1; j < m; ++j) {
      v[i][j].second += v[i][j - 1].second;
    }
    flag = false;
    q = 0ll;
    for (j = 0; j < m; ++j) {
      if ((!flag) && v[i][j].second) {
        q -= v[i][j].first;
        flag = true;
      } else if (flag && (!v[i][j].second)) {
        q += v[i][j].first;
        flag = false;
      }
    }
    ans += q / d;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

ll a[225816], s[225816], l[225816], r[225816];
map<ll, int> mp;
vector<P> v[225816];

int main(void) {
  int n, i, j, p, m;
  bool flag;
  ll x, d, q, ans = 0ll;
  cin >> n >> a[0] >> d;
  for (i = 1; i < n; ++i) {
    a[i] = a[i - 1] + d;
  }
  if (!d) {
    cout << (a[0] ? (n + 1) : 1) << endl;
    return 0;
  }
  if (d < 0) {
    reverse(a, a + n);
    d = -d;
  }
  for (i = 0; i < n; ++i) {
    a[i] *= 2;
    s[i + 1] = s[i] + a[i];
  }
  x = a[0];
  d *= 2;
  x %= d;
  x += d;
  x %= d;
  mp[0] = 0;
  v[0].push_back(P(-d, 1));
  v[0].push_back(P(0ll, -1));
  for (i = 1; i <= n; ++i) {
    q = x * i % d;
    if (mp.find(q) == mp.end()) {
      mp[q] = i;
    }
    p = mp[q];
    l[i] = s[i] - d;
    r[i] = s[n] - s[n - i];
    v[p].push_back(P(l[i], 1));
    v[p].push_back(P(r[i], -1));
  }
  for (i = 0; i <= n; ++i) {
    m = v[i].l_ength();
    sort(v[i].begin(), v[i].end());
    for (j = 1; j < m; ++j) {
      v[i][j].second += v[i][j - 1].second;
    }
    flag = false;
    q = 0ll;
    for (j = 0; j < m; ++j) {
      if ((!flag) && v[i][j].second) {
        q -= v[i][j].first;
        flag = true;
      } else if (flag && (!v[i][j].second)) {
        q += v[i][j].first;
        flag = false;
      }
    }
    ans += q / d;
  }
  cout << ans << endl;
  return 0;
}