Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 510000;
ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << endl;
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << endl;
}
const int mod = 1e9 + 7;

int main() {
  ll n;
  string s;
  cin >> n >> s;
  ll q;
  cin >> q;
  while (q--) {
    ll k;
    cin >> k;
    ll m = 0, c = 0, cnt = 0;
    ll ans = 0;
    rep(i, k - 1) {
      if (s[i] == 'M')
        m++;
      if (s[i] == 'C')
        cnt += m, c++;
    }
    rep(i, n) {
      if (s[i + k - 1] == 'C')
        c++, cnt += m;
      if (s[i + k - 1] == 'M')
        m++;
      if (s[i] == 'D')
        ans += cnt;
      if (s[i] == 'M')
        m--, cnt -= c;
      if (s[i] == 'C')
        c--;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 510000;
ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << endl;
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << endl;
}
const int mod = 1e9 + 7;

int main() {
  ll n;
  string s;
  cin >> n >> s;
  ll q;
  cin >> q;
  while (q--) {
    ll k;
    cin >> k;
    ll m = 0, c = 0, cnt = 0;
    ll ans = 0;
    rep(i, k - 1) {
      if (s[i] == 'M')
        m++;
      if (s[i] == 'C')
        cnt += m, c++;
    }
    rep(i, n) {
      if (i + k <= n)
        if (s[i + k - 1] == 'C')
          c++, cnt += m;
      if (i + k <= n)
        if (s[i + k - 1] == 'M')
          m++;
      if (s[i] == 'D')
        ans += cnt;
      if (s[i] == 'M')
        m--, cnt -= c;
      if (s[i] == 'C')
        c--;
    }
    cout << ans << endl;
  }
}