#include <iostream>
#include <string>

using namespace std;

long long solve(const string& S, int k){
    int dst = 0;
    long long res = 0;
    long long cnt = 0;
    int m = 0;
    int c = 0;
    for(int i=0;i<S.size();i++){
        while(dst < S.size() && dst - i < k){
            if(S[dst] == 'M') ++m;
            if(S[dst] == 'C'){ ++c; cnt += m; }
            ++dst;
        }
        if(S[i] == 'D') res += cnt;
        if(S[i] == 'M') { cnt -= c; --m; }
        if(S[i] == 'C') --c;
    }
    return res;
}

int main(){
    int N; cin >> N;
    string S; cin >> S;
    int Q; cin >> Q;
    for(int i=0;i<Q;i++){
        int k; cin >> k;
        cout << solve(S, k) << endl;
    }
}
#include <bits/stdc++.h>
#include <atcoder/fenwicktree>
using namespace std;
using ll = long long;

int main() {
    int N, Q; cin >> N >> Q;
    atcoder::fenwick_tree<ll> bit(N+1);
    for (int i=1;i<=N;i++) {
        int a; cin >> a;
        bit.add(i, a);
    }
    for (int q=0; q<Q; q++) {
        int d, l, r; 
		cin >> d >> l >> r;
        if (d == 0) {
            bit.add(l+1, r);
        } else {
            cout << bit.sum(l+1, r+1) << "\n";
        }
    }
}