Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y)                                                           \
  for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y)                                                          \
  for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(args...)                                                           \
  do {                                                                         \
    cout << "DEBUG: " << #args << " -> ";                                      \
    err(args);                                                                 \
  } while (0)
#else
#define dbg(...)
#endif
void err() { cout << endl; }
template <template <typename...> class T, typename t, typename... Args>
void err(T<t> a, Args... args) {
  for (auto x : a)
    cout << x << ' ';
  err(args...);
}
template <typename T, typename... Args> void err(T a, Args... args) {
  cout << a << ' ';
  err(args...);
}
// -----------------------------------------------------------------------------

const int N = 1E6 + 100;
int n;
char s[N];

LL solve(int K) {
  int l = 0, r = 1, mp = 0, cp = 0;
  LL pcount = 0, ans = 0;
  while (l < n) {
    while (r - l < K) {
      // add r
      if (s[r] == 'M')
        mp++;
      else if (s[r] == 'C') {
        cp++;
        pcount += 1 * mp;
      }
      r++;
    }

    if (s[l] == 'D')
      ans += pcount;

    l++;
    if (s[l] == 'M') {
      mp--;
      pcount -= 1 * cp;
    } else if (s[l] == 'C')
      cp--;
  }
  return ans;
}

int main() {
  int q;
  cin >> n >> s >> q;
  while (q--) {
    int k;
    cin >> k;
    cout << solve(k) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y)                                                           \
  for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y)                                                          \
  for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(args...)                                                           \
  do {                                                                         \
    cout << "DEBUG: " << #args << " -> ";                                      \
    err(args);                                                                 \
  } while (0)
#else
#define dbg(...)
#endif
void err() { cout << endl; }
template <template <typename...> class T, typename t, typename... Args>
void err(T<t> a, Args... args) {
  for (auto x : a)
    cout << x << ' ';
  err(args...);
}
template <typename T, typename... Args> void err(T a, Args... args) {
  cout << a << ' ';
  err(args...);
}
// -----------------------------------------------------------------------------

const int N = 1E6 + 100;
int n;
char s[N];

LL solve(int K) {
  int l = 0, r = 1, mp = 0, cp = 0;
  LL pcount = 0, ans = 0;
  while (l < n) {
    while (r < n && r - l < K) {
      // add r
      if (s[r] == 'M')
        mp++;
      else if (s[r] == 'C') {
        cp++;
        pcount += 1 * mp;
      }
      r++;
    }

    if (s[l] == 'D')
      ans += pcount;

    l++;
    if (s[l] == 'M') {
      mp--;
      pcount -= 1 * cp;
    } else if (s[l] == 'C')
      cp--;
  }
  return ans;
}

int main() {
  int q;
  cin >> n >> s >> q;
  while (q--) {
    int k;
    cin >> k;
    cout << solve(k) << endl;
  }
}