Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define debug(x) cerr << #x << ": " << x << ", "
#define debugln(x) cerr << #x << ": " << x << '\n'

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

constexpr ll mod = 1e9 + 7;
const double PI = acos(-1.0);

vector<ll> dm(100001);
vector<ll> d(100001);
vector<ll> m(100001);

// return dm (s, e]
ll dm2(int s, int e) { return dm[e] - dm[s] - (d[s] * (m[e] - m[s])); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  string s;
  cin >> s;

  rep(i, n) {
    dm[i + 1] = dm[i];
    d[i + 1] = d[i];
    m[i + 1] = m[i];
    if (s[i] == 'D') {
      d[i + 1] += 1;
    } else if (s[i] == 'M') {
      dm[i + 1] += d[i];
      m[i + 1] += 1;
    }
    // debug(d[i]);debug(m[i]);debugln(dm[i]);
  }

  int q;
  cin >> q;
  while (q--) {
    int k;
    cin >> k;
    ll ans = 0;
    for (int i = 2; i <= n; i++) {
      if (s[i - 1] == 'C') {
        int s = max(0, i - k);
        // debug(s);debug(i-1);debugln(dm2(s,i-1));
        ans += dm2(s, i - 1);
      }
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define debug(x) cerr << #x << ": " << x << ", "
#define debugln(x) cerr << #x << ": " << x << '\n'

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

constexpr ll mod = 1e9 + 7;
const double PI = acos(-1.0);

vector<ll> dm(1000001);
vector<ll> d(1000001);
vector<ll> m(1000001);

// return dm (s, e]
ll dm2(int s, int e) { return dm[e] - dm[s] - (d[s] * (m[e] - m[s])); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  string s;
  cin >> s;

  rep(i, n) {
    dm[i + 1] = dm[i];
    d[i + 1] = d[i];
    m[i + 1] = m[i];
    if (s[i] == 'D') {
      d[i + 1] += 1;
    } else if (s[i] == 'M') {
      dm[i + 1] += d[i];
      m[i + 1] += 1;
    }
    // debug(d[i]);debug(m[i]);debugln(dm[i]);
  }

  int q;
  cin >> q;
  while (q--) {
    int k;
    cin >> k;
    ll ans = 0;
    for (int i = 2; i <= n; i++) {
      if (s[i - 1] == 'C') {
        int s = max(0, i - k);
        // debug(s);debug(i-1);debugln(dm2(s,i-1));
        ans += dm2(s, i - 1);
      }
    }
    cout << ans << endl;
  }
}