Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rint register int
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
// until dimension reduction.

using namespace std;
typedef long long LL;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()

const int INF = 0x3f3f3f3f, N = 200010;
const LL INFL = 0x3f3f3f3f3f3f3f3fll;

int n, k, b[N << 1], cc, sum[N], a[N], ps1[N], ps2[N];
int rt[N << 1], nd;
int ss[N * 31], ls[N * 31], rs[N * 31];
void upd(int &p, int l, int r, int v) {
  if (!p)
    p = ++nd;
  if (l == r) {
    ++ss[p];
    return;
  }
  int mid = (l + r) >> 1;
  (v <= mid) ? upd(ls[p], l, mid, v) : upd(rs[p], mid + 1, r, v);
  ss[p] = ss[ls[p]] + ss[rs[p]];
}
int qrange(int p, int l, int r, int ql, int qr) {
  if (!p || l > r)
    return 0;
  if (ql <= l && r <= qr)
    return ss[p];
  int mid = (l + r) >> 1, res = 0;
  if (ql <= mid)
    res += qrange(ls[p], l, mid, ql, qr);
  if (qr > mid)
    res += qrange(rs[p], mid + 1, r, ql, qr);
  return res;
}
signed main() {
  freopen("test.in", "r", stdin);
  scanf("%d%d", &n, &k);
  for (rint i = 1; i <= n; i++)
    scanf("%d", &a[i]), sum[i] = (sum[i - 1] + a[i]) % k;
  for (rint r = 0; r <= n; r++) {
    b[++cc] = sum[r] - r;
    b[++cc] = sum[r] - r + k;
  }

  sort(b + 1, b + cc + 1);
  int m = unique(b + 1, b + cc + 1) - (b + 1);
  for (rint i = 0; i <= n; i++) {
    ps1[i] = lower_bound(b + 1, b + m + 1, sum[i] - i) - b;
    ps2[i] = lower_bound(b + 1, b + m + 1, sum[i] - i + k) - b;
  }
  upd(rt[ps1[0]], 1, k, sum[0] + 1);
  LL ans = 0;
  for (rint r = 1; r <= n; r++) {
    ans += qrange(rt[ps1[r]], 1, k, 1, sum[r] + 1) +
           qrange(rt[ps2[r]], 1, k, sum[r] + 2, k);
    upd(rt[ps1[r]], 1, k, sum[r] + 1);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rint register int
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
// until dimension reduction.

using namespace std;
typedef long long LL;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()

const int INF = 0x3f3f3f3f, N = 200010;
const LL INFL = 0x3f3f3f3f3f3f3f3fll;

int n, k, b[N << 1], cc, sum[N], a[N], ps1[N], ps2[N];
int rt[N << 1], nd;
int ss[N * 31], ls[N * 31], rs[N * 31];
void upd(int &p, int l, int r, int v) {
  if (!p)
    p = ++nd;
  if (l == r) {
    ++ss[p];
    return;
  }
  int mid = (l + r) >> 1;
  (v <= mid) ? upd(ls[p], l, mid, v) : upd(rs[p], mid + 1, r, v);
  ss[p] = ss[ls[p]] + ss[rs[p]];
}
int qrange(int p, int l, int r, int ql, int qr) {
  if (!p || l > r)
    return 0;
  if (ql <= l && r <= qr)
    return ss[p];
  int mid = (l + r) >> 1, res = 0;
  if (ql <= mid)
    res += qrange(ls[p], l, mid, ql, qr);
  if (qr > mid)
    res += qrange(rs[p], mid + 1, r, ql, qr);
  return res;
}
signed main() {
  //	freopen ("test.in", "r", stdin);
  scanf("%d%d", &n, &k);
  for (rint i = 1; i <= n; i++)
    scanf("%d", &a[i]), sum[i] = (sum[i - 1] + a[i]) % k;
  for (rint r = 0; r <= n; r++) {
    b[++cc] = sum[r] - r;
    b[++cc] = sum[r] - r + k;
  }

  sort(b + 1, b + cc + 1);
  int m = unique(b + 1, b + cc + 1) - (b + 1);
  for (rint i = 0; i <= n; i++) {
    ps1[i] = lower_bound(b + 1, b + m + 1, sum[i] - i) - b;
    ps2[i] = lower_bound(b + 1, b + m + 1, sum[i] - i + k) - b;
  }
  upd(rt[ps1[0]], 1, k, sum[0] + 1);
  LL ans = 0;
  for (rint r = 1; r <= n; r++) {
    ans += qrange(rt[ps1[r]], 1, k, 1, sum[r] + 1) +
           qrange(rt[ps2[r]], 1, k, sum[r] + 2, k);
    upd(rt[ps1[r]], 1, k, sum[r] + 1);
  }
  cout << ans << endl;
  return 0;
}