Problem p03216 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
#define N 1000010
char getc() {
  char c = getchar();
  while ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9'))
    c = getchar();
  return c;
}
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
int n, q, m, a[N], b[N], c[N];
char s[N];
int main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
  const char LL[] = "%I64d\n";
#else
  const char LL[] = "%lld\n";
#endif
  n = read();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    if (s[i] == 'D')
      a[i] = 0;
    else if (s[i] == 'M')
      a[i] = 1;
    else if (s[i] == 'C')
      a[i] = 2;
    else
      a[i] = -1;
  q = read();
  while (q--) {
    m = read();
    int hb = 1, tb = 0, hc = 1, t = 0, tc = 0;
    ll ans = 0, cur = 0;
    for (int i = 1; i <= n; i++)
      if (a[i] == 2)
        b[++tb] = i;
    for (int i = 1; i <= n; i++)
      if (a[i] == 1)
        ans += cur;
      else if (a[i] == 0) {
        c[++tc] = i;
        while (t < tb && b[t + 1] - i + 1 <= m)
          t++;
        cur += t - hb + 1;
      } else if (a[i] == 2) {
        while (hc <= tc && i - c[hc] + 1 > m)
          hc++;
        cur -= tc - hc + 1;
        hb++;
      }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
#define N 1000010
char getc() {
  char c = getchar();
  while ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9'))
    c = getchar();
  return c;
}
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
int n, q, m, a[N], b[N], c[N];
char s[N];
int main() {
  n = read();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    if (s[i] == 'D')
      a[i] = 0;
    else if (s[i] == 'M')
      a[i] = 1;
    else if (s[i] == 'C')
      a[i] = 2;
    else
      a[i] = -1;
  q = read();
  while (q--) {
    m = read();
    int hb = 1, tb = 0, hc = 1, t = 0, tc = 0;
    ll ans = 0, cur = 0;
    for (int i = 1; i <= n; i++)
      if (a[i] == 2)
        b[++tb] = i;
    for (int i = 1; i <= n; i++)
      if (a[i] == 1)
        ans += cur;
      else if (a[i] == 0) {
        c[++tc] = i;
        while (t < tb && b[t + 1] - i + 1 <= m)
          t++;
        cur += t - hb + 1;
      } else if (a[i] == 2) {
        while (hc <= tc && i - c[hc] + 1 > m)
          hc++;
        cur -= tc - hc + 1;
        hb++;
      }
    cout << ans << endl;
  }
  return 0;
}