Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define x first
#define y second
#define endl '\n'
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define bit __builtin_popcount
#define all(x) x.begin(), x.end()
#define show(x) cout << (#x) << " is " << (x) << endl

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using indexed_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef vector<vector<int>> matrix;

const ll INF = 1e18 + 123;
const ld EPS = 1e-9;
const int inf = 1e9 + 123;
const int MOD = 1e9 + 7;
const int N = 5e5 + 123;
const int M = 1e6 + 123;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

char s[N];

int main() {
  int n, q, k;
  scanf("%d", &n);
  scanf("%s", s);
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &k);
    ll ans = 0;
    ll d = 0, m = 0, cnt = 0;
    for (int i = 0; i < k; i++) {
      if (s[i] == 'D') {
        d++;
      }
      if (s[i] == 'M') {
        m += d;
        cnt++;
      }
      if (s[i] == 'C') {
        ans += m;
      }
    }

    for (int i = k; i < n; i++) {
      if (s[i - k] == 'D') {
        m -= cnt;
        d--;
      }
      if (s[i - k] == 'M') {
        cnt--;
      }
      if (s[i] == 'D') {
        d++;
      }
      if (s[i] == 'M') {
        m += d;
        cnt++;
      }
      if (s[i] == 'C') {
        ans += m;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define x first
#define y second
#define endl '\n'
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define bit __builtin_popcount
#define all(x) x.begin(), x.end()
#define show(x) cout << (#x) << " is " << (x) << endl

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using indexed_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef vector<vector<int>> matrix;

const ll INF = 1e18 + 123;
const ld EPS = 1e-9;
const int inf = 1e9 + 123;
const int MOD = 1e9 + 7;
const int N = 5e5 + 123;
const int M = 1e6 + 123;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

char s[M];

int main() {
  int n, q, k;
  scanf("%d", &n);
  scanf("%s", s);
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &k);
    ll ans = 0;
    ll d = 0, m = 0, cnt = 0;
    for (int i = 0; i < k; i++) {
      if (s[i] == 'D') {
        d++;
      }
      if (s[i] == 'M') {
        m += d;
        cnt++;
      }
      if (s[i] == 'C') {
        ans += m;
      }
    }

    for (int i = k; i < n; i++) {
      if (s[i - k] == 'D') {
        m -= cnt;
        d--;
      }
      if (s[i - k] == 'M') {
        cnt--;
      }
      if (s[i] == 'D') {
        d++;
      }
      if (s[i] == 'M') {
        m += d;
        cnt++;
      }
      if (s[i] == 'C') {
        ans += m;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}