Problem p02851 (C++)

Buggy submission (status: Runtime Error):
// #define STOPIT
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
#define rrep(i, n, m) for (int i = (n)-1; i >= (m); i--)
using namespace std;
using ll = long long;

void debug_impl() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
  cerr << " " << head;
  debug_impl(tail...);
}
#ifndef STOPIT
#define debug(...)                                                             \
  do {                                                                         \
    cerr << boolalpha << "[" << #__VA_ARGS__ << "]:";                          \
    debug_impl(__VA_ARGS__);                                                   \
    cerr << std::noboolalpha;                                                  \
  } while (false)
#else
#define debug(...)                                                             \
  {}
#endif

template <typename Container, typename Value = typename Container::value_type,
          enable_if_t<!is_same<Container, string>::value, nullptr_t> = nullptr>
istream &operator>>(istream &is, Container &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <typename Container, typename Value = typename Container::value_type,
          enable_if_t<!is_same<Container, string>::value, nullptr_t> = nullptr>
ostream &operator<<(ostream &os, Container const &v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); it++) {
    os << (it != v.begin() ? "," : "") << *it;
  }
  return os << "}";
}
int main() {
  // read input
  ll N, K;
  cin >> N >> K;
  vector<ll> value(N + 1, 0);
  rep(i, 0, N) {
    // read
    cin >> value[i + 1];

    // minus one
    --value[i + 1];

    // cumulative sum
    value[i + 1] += value[i];

    // take modulo
    value[i + 1] %= K;
  }

  vector<ll> cnts(K, 0);
  ll res = 0;
  int left = 0;
  int right = 1;
  while (left < N + 1) {
    // push to cnts
    while (right < N + 1 and right - left < K)
      ++cnts[value[right++]];

    // count
    // cout << left << ':' << value[left] << ' ' << cnts[value[left]] << endl;
    res += cnts[value[left]];

    // pop que
    // cout << left << ' ' << right << ' ' << res << endl;
    ++left;
    --cnts[value[left]];
  }

  cout << res << endl;
  return 0;
}


Fixed submission:
// #define STOPIT
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
#define rrep(i, n, m) for (int i = (n)-1; i >= (m); i--)
using namespace std;
using ll = long long;

void debug_impl() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
  cerr << " " << head;
  debug_impl(tail...);
}
#ifndef STOPIT
#define debug(...)                                                             \
  do {                                                                         \
    cerr << boolalpha << "[" << #__VA_ARGS__ << "]:";                          \
    debug_impl(__VA_ARGS__);                                                   \
    cerr << std::noboolalpha;                                                  \
  } while (false)
#else
#define debug(...)                                                             \
  {}
#endif

template <typename Container, typename Value = typename Container::value_type,
          enable_if_t<!is_same<Container, string>::value, nullptr_t> = nullptr>
istream &operator>>(istream &is, Container &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <typename Container, typename Value = typename Container::value_type,
          enable_if_t<!is_same<Container, string>::value, nullptr_t> = nullptr>
ostream &operator<<(ostream &os, Container const &v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); it++) {
    os << (it != v.begin() ? "," : "") << *it;
  }
  return os << "}";
}
int main() {
  // read input
  ll N, K;
  cin >> N >> K;
  vector<ll> value(N + 1, 0);
  rep(i, 0, N) {
    // read
    cin >> value[i + 1];

    // minus one
    --value[i + 1];

    // cumulative sum
    value[i + 1] += value[i];

    // take modulo
    value[i + 1] %= K;
  }

  unordered_map<int, int> cnts;
  ll res = 0;
  int left = 0;
  int right = 1;
  while (left < N + 1) {
    // push to cnts
    while (right < N + 1 and right - left < K)
      ++cnts[value[right++]];

    // count
    // cout << left << ':' << value[left] << ' ' << cnts[value[left]] << endl;
    res += cnts[value[left]];

    // pop que
    // cout << left << ' ' << right << ' ' << res << endl;
    ++left;
    --cnts[value[left]];
  }

  cout << res << endl;
  return 0;
}