Problem p02851 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <vector>

// lib/util.hpp
#ifndef UTIL_HPP
#define UTIL_HPP

typedef ::std::int_fast64_t i64;
typedef ::std::uint_fast64_t u64;
typedef ::std::int_fast32_t i32;
typedef ::std::uint_fast32_t u32;

namespace tools {
template <typename T>
void read(::std::istream &is, ::std::vector<T> &vector,
          const typename ::std::vector<T>::size_type size) {
  vector.reserve(size);
  ::std::copy_n(::std::istream_iterator<T>(is), size,
                ::std::back_inserter(vector));
}
template <typename T>
void read(::std::vector<T> &vector,
          const typename ::std::vector<T>::size_type size) {
  ::tools::read(::std::cin, vector, size);
}

template <typename T, ::std::size_t N>
void read(::std::istream &is, ::std::array<T, N> &array) {
  ::std::copy_n(std::istream_iterator<T>(is), N, array.begin());
}
template <typename T, ::std::size_t N> void read(::std::array<T, N> &array) {
  ::tools::read(::std::cin, array);
}
} // namespace tools

#endif

// main.cpp

int main() {
  i64 N, K;
  std::cin >> N >> K;
  std::vector<i64> A;
  tools::read(A, N);

  std::transform(A.begin(), A.end(), A.begin(),
                 [](const i64 &A_i) { return A_i - 1; });
  std::vector<i64> cumsum({0});
  std::partial_sum(A.begin(), A.end(), std::back_inserter(cumsum));
  std::transform(cumsum.begin(), cumsum.end(), cumsum.begin(),
                 [&K](const i64 &cumsum_i) { return cumsum_i % K; });

  std::queue<i64> queue;
  std::unordered_map<i64, i64> map;
  for (i64 i = 0; i < K; ++i) {
    map.emplace(i, 0);
  }

  i64 answer = 0;
  for (const i64 &cumsum_i : cumsum) {
    if (static_cast<i64>(queue.size()) >= K) {
      --map[queue.front()];
      queue.pop();
    }

    answer += map[cumsum_i];

    queue.push(cumsum_i);
    ++map[cumsum_i];
  }

  std::cout << answer << std::endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <vector>

// lib/util.hpp
#ifndef UTIL_HPP
#define UTIL_HPP

typedef ::std::int_fast64_t i64;
typedef ::std::uint_fast64_t u64;
typedef ::std::int_fast32_t i32;
typedef ::std::uint_fast32_t u32;

namespace tools {
template <typename T>
void read(::std::istream &is, ::std::vector<T> &vector,
          const typename ::std::vector<T>::size_type size) {
  vector.reserve(size);
  ::std::copy_n(::std::istream_iterator<T>(is), size,
                ::std::back_inserter(vector));
}
template <typename T>
void read(::std::vector<T> &vector,
          const typename ::std::vector<T>::size_type size) {
  ::tools::read(::std::cin, vector, size);
}

template <typename T, ::std::size_t N>
void read(::std::istream &is, ::std::array<T, N> &array) {
  ::std::copy_n(std::istream_iterator<T>(is), N, array.begin());
}
template <typename T, ::std::size_t N> void read(::std::array<T, N> &array) {
  ::tools::read(::std::cin, array);
}
} // namespace tools

#endif

// main.cpp

int main() {
  i64 N, K;
  std::cin >> N >> K;
  std::vector<i64> A;
  tools::read(A, N);

  std::transform(A.begin(), A.end(), A.begin(),
                 [](const i64 &A_i) { return A_i - 1; });
  std::vector<i64> cumsum({0});
  std::partial_sum(A.begin(), A.end(), std::back_inserter(cumsum));
  std::transform(cumsum.begin(), cumsum.end(), cumsum.begin(),
                 [&K](const i64 &cumsum_i) { return cumsum_i % K; });

  std::queue<i64> queue;
  std::unordered_map<i64, i64> map;

  i64 answer = 0;
  for (const i64 &cumsum_i : cumsum) {
    if (static_cast<i64>(queue.size()) >= K) {
      --map[queue.front()];
      queue.pop();
    }

    answer += map[cumsum_i];

    queue.push(cumsum_i);
    ++map[cumsum_i];
  }

  std::cout << answer << std::endl;
  return 0;
}