Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

struct BIT {
  int n;
  VL bit;

  BIT() {}
  BIT(int x) {
    n = x;
    bit.assign(n + 1, 0);
  }

  void init(int x) {
    n = x;
    bit.assign(n + 1, 0);
  }

  void add(int i, ll x) {
    i++;
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }

  // [0, i]
  ll sum(int i) {
    i++;
    ll ret = 0;
    while (i > 0) {
      ret += bit[i];
      i -= i & -i;
    }
    return ret;
  }

  // [l, r)
  ll sum(int l, int r) {
    if (l >= r)
      return 0;
    return sum(r - 1) - sum(l - 1);
  }
};

int main() {
  int n;
  string s;
  cin >> n >> s;
  VI d, c(n);
  REP(i, n) if (s[i] == 'D') d.push_back(i);
  int cnt = 0;
  REP(i, n) {
    if (s[i] == 'D')
      cnt++;
    c[i] = cnt;
  }
  int m = d.size();
  int q;
  cin >> q;
  while (q--) {
    int k = in();
    ll ans = 0, sum = 0;
    BIT bit(m + 1);
    int j = 0;
    REP(i, n) {
      if (s[i] == 'M') {
        bit.add(0, 1);
        bit.add(c[i], -1);
        sum += c[i] - j;
      } else if (s[i] == 'C') {
        while (i - d[j] >= k) {
          sum -= bit.sum(j);
          j++;
        }
        ans += sum;
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

struct BIT {
  int n;
  VL bit;

  BIT() {}
  BIT(int x) {
    n = x;
    bit.assign(n + 1, 0);
  }

  void init(int x) {
    n = x;
    bit.assign(n + 1, 0);
  }

  void add(int i, ll x) {
    i++;
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }

  // [0, i]
  ll sum(int i) {
    i++;
    ll ret = 0;
    while (i > 0) {
      ret += bit[i];
      i -= i & -i;
    }
    return ret;
  }

  // [l, r)
  ll sum(int l, int r) {
    if (l >= r)
      return 0;
    return sum(r - 1) - sum(l - 1);
  }
};

int main() {
  int n;
  string s;
  cin >> n >> s;
  VI d, c(n);
  REP(i, n) if (s[i] == 'D') d.push_back(i);
  int cnt = 0;
  REP(i, n) {
    if (s[i] == 'D')
      cnt++;
    c[i] = cnt;
  }
  int m = d.size();
  int q;
  cin >> q;
  while (q--) {
    int k = in();
    ll ans = 0, sum = 0;
    BIT bit(m + 1);
    int j = 0;
    REP(i, n) {
      if (s[i] == 'M') {
        bit.add(0, 1);
        bit.add(c[i], -1);
        sum += c[i] - j;
      } else if (s[i] == 'C') {
        while (j < m && i - d[j] >= k) {
          sum -= bit.sum(j);
          j++;
        }
        ans += sum;
      }
    }
    cout << ans << endl;
  }
  return 0;
}