Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;

template <typename T> struct bit1 {
  T add(T x, T y) { return x + y; }
  int n;
  vector<T> t;
  void init(int n) {
    this->n = n;
    t.resize(n + 10);
  }
  T qry(int i) {
    T res = 0;
    for (; i > 0; i -= i & -i)
      res = add(res, t[i]);
    return res;
  }
  void upd(int i, T dx) {
    assert(i >= 1 && i <= n);
    for (; i <= n; i += i & -i)
      t[i] = add(t[i], dx);
  }
  T get(int i) { return qry(i) - qry(i - 1); }
};

int n; // 1e6
string s;

ll brute(int k) {
  ll res = 0;
  for (int a = 1; a <= n; a++) {
    for (int b = a + 1; b <= n; b++) {
      for (int c = b + 1; c <= n; c++) {
        if (c - a < k && s[a] == 'D' && s[b] == 'M' && s[c] == 'C') {
          res++;
        }
      }
    }
  }
  return res;
}

ll solve(int k) {

  bit1<ll> bit;
  bit.init(n);
  ll res = 0;

  auto inc = [&](int i) {
    bit.upd(1, 1);
    bit.upd(i + 1, -1);
  };

  auto get = [&](int i) { return bit.qry(i); };

  deque<int> D;
  ll sum = 0;

  for (int i = 1; i <= n; i++) {
    while (!D.empty() && i - D.front() >= k) {
      sum -= get(D.front());
      D.pop_front();
    }
    if (s[i] == 'D') {
      D.push_back(i);
    }
    if (s[i] == 'M') {
      inc(i);
      sum += D.size();
    }
    if (s[i] == 'C') {
      res += sum;
    }
  }

  return res;
}

int q; // 75
vector<int> k;

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

  cin >> n;
  cin >> s;
  s = "*" + s;

  cin >> q;
  k.resize(q);
  for (int i = 0; i < q; i++) {
    cin >> k[i];
  }

  for (int i : k) {
    // assert(solve(i) == brute(i));
    cout << solve(i) << "\n";
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;

template <typename T> struct bit1 {
  T add(T x, T y) { return x + y; }
  int n;
  vector<T> t;
  void init(int n) {
    this->n = n;
    t.resize(n + 10);
  }
  T qry(int i) {
    T res = 0;
    for (; i > 0; i -= i & -i)
      res = add(res, t[i]);
    return res;
  }
  void upd(int i, T dx) {
    assert(i >= 1 && i <= n);
    for (; i <= n; i += i & -i)
      t[i] = add(t[i], dx);
  }
  T get(int i) { return qry(i) - qry(i - 1); }
};

int n; // 1e6
string s;

ll brute(int k) {
  ll res = 0;
  for (int a = 1; a <= n; a++) {
    for (int b = a + 1; b <= n; b++) {
      for (int c = b + 1; c <= n; c++) {
        if (c - a < k && s[a] == 'D' && s[b] == 'M' && s[c] == 'C') {
          res++;
        }
      }
    }
  }
  return res;
}

ll solve(int k) {

  bit1<ll> bit;
  bit.init(n + 10);
  ll res = 0;

  auto inc = [&](int i) {
    bit.upd(1, 1);
    bit.upd(i + 1, -1);
  };

  auto get = [&](int i) { return bit.qry(i); };

  deque<int> D;
  ll sum = 0;

  for (int i = 1; i <= n; i++) {
    while (!D.empty() && i - D.front() >= k) {
      sum -= get(D.front());
      D.pop_front();
    }
    if (s[i] == 'D') {
      D.push_back(i);
    }
    if (s[i] == 'M') {
      inc(i);
      sum += D.size();
    }
    if (s[i] == 'C') {
      res += sum;
    }
  }

  return res;
}

int q; // 75
vector<int> k;

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

  cin >> n;
  cin >> s;
  s = "*" + s;

  cin >> q;
  k.resize(q);
  for (int i = 0; i < q; i++) {
    cin >> k[i];
  }

  for (int i : k) {
    // assert(solve(i) == brute(i));
    cout << solve(i) << "\n";
  }

  return 0;
}