Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourth;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, t, s, p, q, last, first, cnt, sum[3][1000000], sum2[1000000],
    ans, dp, a[400000], nxt[400000];
string str, ss;
bool f[200][200];
char c;
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<int>> vec;
vector<edge> ed;
vector<LL> v[3];
map<Pll, LL> ma;
set<LL> st;

void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

int main() {
  cin >> n;
  cin >> str;
  rep(i, 3) { v[i].push_back(-1); }
  rep(i, 3) {
    rep(j, n) {
      LL cur = 0;
      if (i == 0 && str[j] == 'D') {
        cur++;
      }
      if (i == 1 && str[j] == 'M') {
        cur++;
      }
      if (i == 2 && str[j] == 'C') {
        cur++;
      }
      if (cur == 1)
        v[i].push_back(j);
      sum[i][j + 1] += sum[i][j] + cur;
    }
  }
  rep(i, 3) { v[i].push_back(INF); }
  cin >> q;
  rep(i, q) {
    LL x;
    ans = 0;
    cin >> x;
    rep(i, n) {
      sum2[i] = 0;
      nxt[i] = 0;
    }
    rep(j, v[0].size()) {
      if (j == 0 || j == v[0].size() - 1)
        continue;

      LL it =
          upper_bound(v[2].begin(), v[2].end(), v[0][j] + x - 1) - v[2].begin();
      sum2[v[2][it - 1]]++;
      sum2[v[0][j]]--;
      nxt[v[0][j]] = v[2][it - 1];
    }
    LL cur = 0;
    Rrep(j, n) {
      sum2[j] += sum2[j + 1];
      if (str[j] == 'M') {
        ans += cur;
      }
      if (str[j] == 'C') {
        cur += sum2[j];
      }
      if (str[j] == 'D') {
        cur -= sum[2][nxt[j] + 1] - sum[2][j];
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourth;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, t, s, p, q, last, first, cnt, sum[3][1100000], sum2[1100000],
    ans, dp, a[1100000], nxt[1100000];
string str, ss;
bool f[200][200];
char c;
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<int>> vec;
vector<edge> ed;
vector<LL> v[3];
map<Pll, LL> ma;
set<LL> st;

void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

int main() {
  cin >> n;
  cin >> str;
  rep(i, 3) { v[i].push_back(-1); }
  rep(i, 3) {
    rep(j, n) {
      LL cur = 0;
      if (i == 0 && str[j] == 'D') {
        cur++;
      }
      if (i == 1 && str[j] == 'M') {
        cur++;
      }
      if (i == 2 && str[j] == 'C') {
        cur++;
      }
      if (cur == 1)
        v[i].push_back(j);
      sum[i][j + 1] += sum[i][j] + cur;
    }
  }
  rep(i, 3) { v[i].push_back(INF); }
  cin >> q;
  rep(i, q) {
    LL x;
    ans = 0;
    cin >> x;
    rep(i, n) {
      sum2[i] = 0;
      nxt[i] = 0;
    }
    rep(j, v[0].size()) {
      if (j == 0 || j == v[0].size() - 1)
        continue;

      LL it =
          upper_bound(v[2].begin(), v[2].end(), v[0][j] + x - 1) - v[2].begin();
      sum2[v[2][it - 1]]++;
      sum2[v[0][j]]--;
      nxt[v[0][j]] = v[2][it - 1];
    }
    LL cur = 0;
    Rrep(j, n) {
      sum2[j] += sum2[j + 1];
      if (str[j] == 'M') {
        ans += cur;
      }
      if (str[j] == 'C') {
        cur += sum2[j];
      }
      if (str[j] == 'D') {
        cur -= sum[2][nxt[j] + 1] - sum[2][j];
      }
    }
    cout << ans << endl;
  }
  return 0;
}