Problem p02721 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")

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

#define len(x) (int)((x).size())
#define endl '\n'
#define int long long
#define x first
#define y second
#define dbg(x) cout << #x << " = " << x << endl;

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}

const int MOD = 998244353, MAXN = 1e6 + 10, INF = 1e18 + 10, BASE = 37;

void solve() {
  int n, k, c;
  cin >> n >> k >> c;
  string s;
  cin >> s;

  if (n == 1) {
    if (s[0] == 'x') {
      cout << 0 << endl;
    } else {
      cout << 1 << endl;
    }
  }

  vector<int> a;
  for (int i = 0; i < len(s); ++i) {
    if (s[i] == 'o') {
      a.push_back(i);
    }
  }

  vector<int> f1(len(a) + 1, INF), f2(len(a) + 1, -INF);
  f1[1] = a[0], f2[1] = a[len(a) - 1];
  f1[0] = -1, f2[0] = a.back() + 1;

  int j = 1;
  for (int i = 1; i < len(a); ++i) {
    while (j < len(a) && a[j] - f1[i] <= c) {
      j++;
    }
    if (j == len(a))
      continue;

    f1[i + 1] = a[j];
  }

  int ll = 2;
  j = len(a) - 2;
  for (int i = 2; i <= len(a); ++i) {
    while (j >= 0 && f2[i - 1] - a[j] <= c) {
      j--;
    }
    if (j == -1)
      continue;

    f2[i] = a[j];
  }

  // for (int i = 0; i <= k; ++i) {
  //     cout << f1[i] << " ";
  // }
  // cout << endl;

  int ans = 0;

  vector<int> ps(MAXN);
  for (int i = 0; i <= k; ++i) {
    int l = f1[i] + 1, r = f2[k - i] - 1;
    // dbg(l << " " << r)
    if (i != 0 && i != k) {
      if (l <= r && l >= 0 && r - l + 1 >= c) {
        ps[l]++;
        ps[r + 1]--;
      }
    } else {
      if (l <= r && l >= 0) {
        ps[l]++;
        ps[r + 1]--;
      }
    }
  }

  for (int i = 1; i < MAXN; ++i) {
    ps[i] = ps[i - 1] + ps[i];
  }

  vector<int> aans;
  for (auto it : a) {
    if (!ps[it]) {
      aans.push_back(it + 1);
      // cout << it + 1 << endl;
    }
  }

  if (len(aans) == k && len(a) == k) {
    assert(k != 1);
  }

  if (len(aans) <= k) {
    for (auto it : aans) {
      cout << it << endl;
    }
  }
}

Fixed submission:
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")

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

#define len(x) (int)((x).size())
#define endl '\n'
#define int long long
#define x first
#define y second
#define dbg(x) cout << #x << " = " << x << endl;

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}

const int MOD = 998244353, MAXN = 1e6 + 10, INF = 1e18 + 10, BASE = 37;

void solve() {
  int n, k, c;
  cin >> n >> k >> c;
  string s;
  cin >> s;

  if (n == 1) {
    if (s[0] == 'x') {
      cout << 0 << endl;
    } else {
      cout << 1 << endl;
    }
  }

  vector<int> a;
  for (int i = 0; i < len(s); ++i) {
    if (s[i] == 'o') {
      a.push_back(i);
    }
  }

  vector<int> f1(len(a) + 1, INF), f2(len(a) + 1, -INF);
  f1[1] = a[0], f2[1] = a[len(a) - 1];
  f1[0] = -1, f2[0] = a.back() + 1;

  int j = 1;
  for (int i = 1; i < len(a); ++i) {
    while (j < len(a) && a[j] - f1[i] <= c) {
      j++;
    }
    if (j == len(a))
      continue;

    f1[i + 1] = a[j];
  }

  int ll = 2;
  j = len(a) - 2;
  for (int i = 2; i <= len(a); ++i) {
    while (j >= 0 && f2[i - 1] - a[j] <= c) {
      j--;
    }
    if (j == -1)
      continue;

    f2[i] = a[j];
  }

  // for (int i = 0; i <= k; ++i) {
  //     cout << f1[i] << " ";
  // }
  // cout << endl;

  int ans = 0;

  vector<int> ps(MAXN);
  for (int i = 0; i <= k; ++i) {
    int l = f1[i] + 1, r = f2[k - i] - 1;
    // dbg(l << " " << r)
    if (i != 0 && i != k) {
      if (l <= r && l >= 0 && r - l + 1 >= c) {
        ps[l]++;
        ps[r + 1]--;
      }
    } else {
      if (l <= r && l >= 0) {
        ps[l]++;
        ps[r + 1]--;
      }
    }
  }

  for (int i = 1; i < MAXN; ++i) {
    ps[i] = ps[i - 1] + ps[i];
  }

  vector<int> aans;
  for (auto it : a) {
    if (!ps[it]) {
      aans.push_back(it + 1);
      // cout << it + 1 << endl;
    }
  }

  if (len(aans) == k && len(a) == k) {
    if (k == 1)
      return;
  }

  if (len(aans) <= k) {
    for (auto it : aans) {
      cout << it << endl;
    }
  }
}