Language: C++

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <cassert>
#include <array>
#include <memory>
#include <random>
#include <functional>
#include <complex>
#ifdef DEBUG
#include "./Lib/debug.hpp"
#include "./Lib/Timer.hpp"
#include "./Lib/sample.hpp"
#else
#define dump(...)
#endif

/* (=＾o＾=) */
//#define int ll

/* macro */
#define FOR(i, b, e) for(ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for(ll i = (ll)(e-1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x,c) for(const auto& x:(c))
#define REPI2(it,b,e) for(auto it = (b); it != (e); ++it)
#define REPI(it,c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it,c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for(auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(),(x).end()
#define cauto const auto&
/* macro func */
template<class T>
inline void sort(T& t) { std::sort(ALL(t)); }
template<class T>
inline void rsort(T& t) { std::sort((t).rbegin(), (t).rend()); }
template<class T>
inline void unique(T& t) { (t).erase(unique((t).begin(), (t).end()), (t).end()); }
template<class T, class S>
inline bool chmax(T& t, const S& s) { if (s > t) { t = s; return true; } return false; }
template<class T, class S>
inline bool chmaxE(T& t, const S& s) { if (s >= t) { t = s; return true; } return false; }
template<class T, class S>
inline bool chmin(T& t, const S& s) { if (s < t) { t = s; return true; } return false; }
inline void BR() { std::cout << "\n"; }

/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template<class T>
using V = std::vector<T>;

/* using std */
using std::cout;
constexpr char endl = '\n';
using std::cin;
using std::pair;
using std::string;
using std::stack;
using std::queue;
using std::vector;
using std::list;
using std::map;
using std::unordered_map;
using std::multimap;
using std::unordered_multimap;
using std::set;
using std::unordered_set;
using std::unordered_multiset;
using std::multiset;
using std::bitset;
using std::priority_queue;
using std::tuple;

/* Initial processing  */
struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; }_Preprocessing;

/* Remove the source of the bug */
inline signed pow(signed, signed) { assert(false); return -1; }

/* define hash */
namespace std {
template <>	class hash<std::pair<ll, ll>> { public:	size_t operator()(const std::pair<ll, ll>& x) const { return hash<ll>()(1000000000 * x.first + x.second); } };
}

/* input */
template<class T> std::istream& operator >> (std::istream& is, vector<T>& vec) { for (T& x : vec) is >> x; return is; }

/* constant value */
constexpr ll MOD = 1000000007;
//constexpr ll MOD = 998244353;
//constexpr ll MOD = 2;

//=============================================================================================


template<class Operator> class SegmentTree {
	Operator Op;
	using typeNode = decltype(Op.unitNode);
	size_t length;
	size_t num;
	vector<typeNode> node;
	vector<pair<size_t, size_t>> range;
public:

	//unitで初期化
	SegmentTree(const size_t num) : num(num) {
		for (length = 1; length < num; length *= 2);
		node.resize(2 * length, Op.unitNode);
		range.resize(2 * length);
		for (int i = 0; i < length; ++i) range[i + length] = std::make_pair(i, i + 1);
		for (int i = length - 1; i >= 0; --i) range[i] = std::make_pair(range[(i << 1) + 0].first, range[(i << 1) + 1].second);
	}

	//vectorで初期化
	SegmentTree(const vector<typeNode>& vec) : num(vec.size()) {
		for (length = 1; length < vec.size(); length *= 2);
		node.resize(2 * length, Op.unitNode);
		for (int i = 0; i < vec.size(); ++i) node[i + length] = vec[i];
		for (int i = length - 1; i >= 0; --i) node[i] = Op.funcNode(node[(i << 1) + 0], node[(i << 1) + 1]);
		range.resize(2 * length);
		for (int i = 0; i < length; ++i) range[i + length] = make_pair(i, i + 1);
		for (int i = length - 1; i >= 0; --i) range[i] = make_pair(range[(i << 1) + 0].first, range[(i << 1) + 1].second);
	}

	//同じinitで初期化
	SegmentTree(const size_t num, const typeNode init) : num(num) {
		for (length = 1; length < num; length *= 2);
		node.resize(2 * length, init);
		range.resize(2 * length);
		for (int i = 0; i < length; ++i) range[i + length] = make_pair(i, i + 1);
		for (int i = length - 1; i >= 0; --i) range[i] = make_pair(range[(i << 1) + 0].first, range[(i << 1) + 1].second);
	}

	//[idx,idx+1)
	void update(size_t idx, const typeNode var) {
		if (idx < 0 || length <= idx) return;
		idx += length;
		node[idx] = Op.funcMerge(node[idx], var);
		while (idx >>= 1) node[idx] = Op.funcNode(node[(idx << 1) + 0], node[(idx << 1) + 1]);
	}

	//[l,r)
	typeNode get(int l, int r) {
		if (l < 0 || length <= l || r < 0 || length < r) return Op.unitNode;
		typeNode vl = Op.unitNode, vr = Op.unitNode;
		for (l += length, r += length; l < r; l >>= 1, r >>= 1) {
			if (l & 1) vl = Op.funcNode(vl, node[l++]);
			if (r & 1) vr = Op.funcNode(node[--r], vr);
		}
		return Op.funcNode(vl, vr);
	}

	//return [0,length]
	int PrefixBinarySearch(typeNode var) {
		if (!Op.funcCheck(node[1], var)) return num;
		typeNode ret = Op.unitNode;
		size_t idx = 2;
		for (; idx < 2 * length; idx <<= 1) {
			if (!Op.funcCheck(Op.funcNode(ret, node[idx]), var)) {
				ret = Op.funcNode(ret, node[idx]);
				idx++;
			}
		}
		return min((idx >> 1) - length, num);
	}

	//range[l,r) return [l,r]
	int BinarySearch(size_t l, size_t r, typeNode var) {
		if (l < 0 || length <= l || r < 0 || length < r) return -1;
		typeNode ret = Op.unitNode;
		size_t off = l;
		for (size_t idx = l + length; idx < 2 * length && off < r; ) {
			if (range[idx].second <= r && !Op.funcCheck(Op.funcNode(ret, node[idx]), var)) {
				ret = Op.funcNode(ret, node[idx]);
				off = range[idx++].second;
				if (!(idx & 1)) idx >>= 1;
			} else {
				idx <<= 1;
			}
		}
		return off;
	}

	void print() {
		cout << "{ " << get(0, 1);
		for (int i = 1; i < length; ++i) cout << ", " << get(i, i + 1);
		cout << " }" << endl;

		// for(int i = 1,j = 1; i < 2*length; ++i) {
		// 	cout << node[i] << " ";
		// 	if(i==((1<<j)-1) && ++j) cout << endl;
		// }
	}

};

template<class typeNode> struct nodeHogePointUpdate {
	typeNode unitNode = typeNode{0,0,0};
	typeNode funcNode(const typeNode& xl, const typeNode& xr) { return {xl[0] + xr[0],xl[1] + xr[1],xl[2] + xr[2] + xl[0] * xr[1]}; }
	typeNode funcMerge(const typeNode& l, const typeNode& r) { return r; }
};
/*
		return {xl[0] + xr[0],xl[1] + xr[1],xl[2] + xr[2] + xl[0] * xr[1]};
*/
signed main() {
	ll n;
	cin >> n;
	string s;
	cin >> s;
	using A = std::array<ll, 3>;
	auto tree = SegmentTree<nodeHogePointUpdate<A>>(n);

	REP(i, n) {
		if (s[i] == 'M') {
			tree.update(i, A{1,0,0});
		}
		if (s[i] == 'C') {
			tree.update(i, A{0,1,0});
		}
	}

	ll q;
	cin >> q;
	REP(_, q) {
		ll k;
		cin >> k;

		ll ans = 0;
		REP(i, n)if (s[i] == 'D') {
			ans += tree.get(i, std::min(n, i + k))[2];
		}
		cout << ans << endl;
	}
}




Compiler output:
a.cc: In constructor 'SegmentTree<Operator>::SegmentTree(const std::vector<decltype (Op.unitNode)>&)':
a.cc:141:70: error: there are no arguments to 'make_pair' that depend on a template parameter, so a declaration of 'make_pair' must be available [-fpermissive]
  141 |                 for (int i = 0; i < length; ++i) range[i + length] = make_pair(i, i + 1);
      |                                                                      ^~~~~~~~~
a.cc:141:70: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc:142:66: error: there are no arguments to 'make_pair' that depend on a template parameter, so a declaration of 'make_pair' must be available [-fpermissive]
  142 |                 for (int i = length - 1; i >= 0; --i) range[i] = make_pair(range[(i << 1) + 0].first, range[(i << 1) + 1].second);
      |                                                                  ^~~~~~~~~
a.cc: In constructor 'SegmentTree<Operator>::SegmentTree(size_t, typeNode)':
a.cc:150:70: error: there are no arguments to 'make_pair' that depend on a template parameter, so a declaration of 'make_pair' must be available [-fpermissive]
  150 |                 for (int i = 0; i < length; ++i) range[i + length] = make_pair(i, i + 1);
      |                                                                      ^~~~~~~~~
a.cc:151:66: error: there are no arguments to 'make_pair' that depend on a template parameter, so a declaration of 'make_pair' must be available [-fpermissive]
  151 |                 for (int i = length - 1; i >= 0; --i) range[i] = make_pair(range[(i << 1) + 0].first, range[(i << 1) + 1].second);
      |                                                                  ^~~~~~~~~
a.cc: In member function 'int SegmentTree<Operator>::PrefixBinarySearch(typeNode)':
a.cc:184:24: error: there are no arguments to 'min' that depend on a template parameter, so a declaration of 'min' must be available [-fpermissive]
  184 |                 return min((idx >> 1) - length, num);
      |                        ^~~