Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  do {                                                                         \
    cout << "0" << endl;                                                       \
    return 0;                                                                  \
  } while (false)
#define MM << " " <<
#define Endl endl

vector<int> compress(vector<int> D) {
  map<int, int> G;
  vector<int> E, r;
  int s = D.size();
  for (int i = 0; i < s; i++) {
    E.push_back(D[i]);
  }
  sort(E.begin(), E.end());
  int t = 0;
  for (int i = 0; i < s; i++) {
    if (i != 0 && E[i] != E[i - 1]) {
      t++;
    }
    G[E[i]] = t;
  }
  for (int i = 0; i < s; i++) {
    r.push_back(G[D[i]]);
  }
  return r;
}

signed main(void) {
  int N, K;
  int A[214514], B[214514] = {};
  cin >> N >> K;
  REP(i, N) { cin >> A[i]; }
  B[0] = 0;
  REP(i, N) {
    B[i + 1] = B[i] + A[i];
    B[i + 1] %= K;
  }
  REP(i, N + 1) {
    B[i] += (K - (i % K));
    // B[i]++;
    B[i] %= K;
  }
  // REP(i,N+1){cout << B[i] << " ";}cout << endl;
  vector<int> D;
  REP(i, N + 1) { D.push_back(B[i]); }
  D = compress(D);
  vector<int> G[10];
  int Dmx = 0;
  REP(i, D.size()) {
    G[D[i]].push_back(i);
    Dmx = max(Dmx, D[i]);
  }
  int Ans = 0;
  REP(j, Dmx + 1) {
    int l = 0, r = 1;
    int num = 0;
    while (l < G[j].size()) {
      if (G[j][r] - G[j][l] < K && r != G[j].size()) {
        r++;
      } else {
        num += (r - l - 1);
        l++;
      }
    }
    Ans += num;
  }
  cout << Ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  do {                                                                         \
    cout << "0" << endl;                                                       \
    return 0;                                                                  \
  } while (false)
#define MM << " " <<
#define Endl endl

vector<int> compress(vector<int> D) {
  map<int, int> G;
  vector<int> E, r;
  int s = D.size();
  for (int i = 0; i < s; i++) {
    E.push_back(D[i]);
  }
  sort(E.begin(), E.end());
  int t = 0;
  for (int i = 0; i < s; i++) {
    if (i != 0 && E[i] != E[i - 1]) {
      t++;
    }
    G[E[i]] = t;
  }
  for (int i = 0; i < s; i++) {
    r.push_back(G[D[i]]);
  }
  return r;
}

signed main(void) {
  int N, K;
  int A[214514], B[214514] = {};
  cin >> N >> K;
  REP(i, N) { cin >> A[i]; }
  B[0] = 0;
  REP(i, N) {
    B[i + 1] = B[i] + A[i];
    B[i + 1] %= K;
  }
  REP(i, N + 1) {
    B[i] += (K - (i % K));
    // B[i]++;
    B[i] %= K;
  }
  // REP(i,N+1){cout << B[i] << " ";}cout << endl;
  vector<int> D;
  REP(i, N + 1) { D.push_back(B[i]); }
  D = compress(D);
  vector<int> G[214514];
  int Dmx = 0;
  REP(i, D.size()) {
    G[D[i]].push_back(i);
    Dmx = max(Dmx, D[i]);
  }
  int Ans = 0;
  REP(j, Dmx + 1) {
    int l = 0, r = 1;
    int num = 0;
    while (l < G[j].size()) {
      if (G[j][r] - G[j][l] < K && r != G[j].size()) {
        r++;
      } else {
        num += (r - l - 1);
        l++;
      }
    }
    Ans += num;
  }
  cout << Ans << endl;
  return 0;
}