Problem p02721 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;

int N, M, Q, K, A, B, C, X, Y, Z;
string s, t;

int early[500005], late[500005], pt[500005];
int pref[500005], suff[500005];
int useful[500005], usable[500005];
deque<int> prefstuff;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K >> C;
  cin >> s;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'o') {
      if (i >= C)
        early[i + 1] = pref[i - C] + 1;
      else
        early[i + 1] = 1;
    }
    pref[i + 1] = max(pref[i], early[i + 1]);
  }
  Z = 0;
  for (int i = s.size(); i >= 0; --i) {
    if (s[i] == 'o') {
      late[i + 1] = suff[i + C + 2] + 1;
    }
    suff[i + 1] = max(suff[i + 2], late[i + 1]);
  }
  /*
  for (int i = 1; i <= N; ++i){
          cout << early[i] << ' ' << late[i] << '\\n';
  }
  cout << '\\n';

  for (int i = 1; i <= N; ++i){
          cout << pref[i] << ' ' << suff[i] << '\\n';
  }
  */
  for (int i = 1; i <= N; ++i) {
    if (suff[i + C + 1] + early[i] >= K)
      useful[i] = 1;
  }
  for (int i = C + 2; i < N + C + 2; ++i) {
    if (s[i - C - 2] == 'o' && useful[i - C - 1])
      prefstuff.push_back(i - C - 1);
    while (!prefstuff.empty() && early[prefstuff.front()] < K - suff[i]) {
      prefstuff.pop_front();
    }
    if (prefstuff.size() > 1) {
      for (auto it : prefstuff) {
        useful[it] = -1;
      }
      while (prefstuff.size() > 1)
        prefstuff.pop_front();
    }
  }
  for (int i = 1; i <= N; ++i) {
    if (early[i] == 0)
      continue;
    if (useful[i] == 1)
      cout << i << '\\n';
  }
}

Error: 0
1 
1 2 
6 
10 
10 11 


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;

int N, M, Q, K, A, B, C, X, Y, Z;
string s, t;

int early[500005], late[500005], pt[500005];
int pref[500005], suff[500005];
int useful[500005];
deque<int> prefstuff;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K >> C;
  cin >> s;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'o') {
      if (i >= C)
        early[i + 1] = pref[i - C + 1] + 1;
      else
        early[i + 1] = 1;
    }
    pref[i + 1] = max(pref[i], early[i + 1]);
  }
  Z = 0;
  for (int i = s.size(); i >= 0; --i) {
    if (s[i] == 'o') {
      late[i + 1] = suff[i + C + 1] + 1;
    }
    suff[i + 1] = max(suff[i + 2], late[i + 1]);
  }
  /*
  for (int i = 1; i <= N; ++i){
          cout << early[i] << ' ' << late[i] << '\\n';
  }
  cout << '\\n';

  for (int i = 1; i <= N; ++i){
          cout << pref[i] << ' ' << suff[i] << '\\n';
  }
  */
  for (int i = C + 2; i < N + C + 2; ++i) {
    prefstuff.push_back(i - C - 1);
    if (s[i - C - 2] == 'x')
      continue;
    while (!prefstuff.empty() && early[prefstuff.front()] < K - suff[i]) {
      prefstuff.pop_front();
    }
    for (auto it : prefstuff) {
      cerr << it << ' ';
    }
    cerr << '\\n';
    if (prefstuff.size() > 1) {
      for (auto it : prefstuff) {
        useful[it] = 0;
      }
      while (prefstuff.size() > 1)
        prefstuff.pop_front();
    } else {
      useful[prefstuff.front()] = 1;
    }
  }
  for (int i = 1; i <= N; ++i) {
    if (early[i] == 0)
      continue;
    if (useful[i])
      cout << i << '\\n';
  }
}