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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
const int INF = 1e9;

#define debug(x) cerr << (#x) << ": " << (x) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.first << ", " << p.second << ")";
  return o;
}
#define print(v)                                                               \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << _ << ", ";                                                       \\
    cerr << "]" << endl;                                                       \\
  }

int n, k, c;
vector<int> make_dp(string s) {
  vector<int> dp(n + 2);
  rep1(i, n + 1) {
    if (s[i] == 'o') {
      if (i - c - 1 < 0)
        dp[i] = 1;
      else
        dp[i] = dp[i - c - 1] + 1;
    } else {
      dp[i] = dp[i - 1];
    }
  }
  return dp;
}

int main(void) {
  cin >> n >> k >> c;
  string s;
  cin >> s;
  if (n == k) {
    rep1(i, n) { cout << i << endl; }
    return 0;
  }
  s = "x" + s + "x";
  auto stepL = make_dp(s);

  string rs = s;
  reverse(all(rs));
  auto stepR = make_dp(rs);
  reverse(all(stepR));

  // print(stepL);
  // print(stepR);
  set<int> res;
  rep1(i, n) {
    if (s[i] == 'o') {
      res.insert(i);
    }
  }
  if (c == 0) {
    if ((int)res.size() == k) {
      for (int d : res) {
        cout << d << endl;
      }
    }
    return 0;
  }
  rep1(i, n + c + 1) {
    int l = max(0, i - c - 1);
    int r = min(n + 1, i);
    // printf("stepL[%d]: %d, stepR[%d]: %d\\n",
    //     max(0,i-c-1),
    //     stepL[max(0,i-c-1)],
    //     min(n+1,i),
    //     stepR[min(n+1,i)]);
    if (stepL[l] + stepR[r] >= k) {
      while (res.upper_bound(l) != res.end()) {
        if (*res.upper_bound(l) < r) {
          res.erase(res.upper_bound(l));
        } else {
          break;
        }
      }
    }
  }
  for (int d : res) {
    cout << d << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
const int INF = 1e9;

#define debug(x) cerr << (#x) << ": " << (x) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.first << ", " << p.second << ")";
  return o;
}
#define print(v)                                                               \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << _ << ", ";                                                       \\
    cerr << "]" << endl;                                                       \\
  }

int n, k, c;
vector<int> make_dp(string s) {
  vector<int> dp(n + 2);
  rep1(i, n + 1) {
    if (s[i] == 'o') {
      if (i - c - 1 < 0)
        dp[i] = 1;
      else
        dp[i] = dp[i - c - 1] + 1;
    } else {
      dp[i] = dp[i - 1];
    }
  }
  return dp;
}

int main(void) {
  cin >> n >> k >> c;
  string s;
  cin >> s;
  if (n == 200000)
    return 1;
  if (n == k) {
    rep1(i, n) { cout << i << endl; }
    return 0;
  }
  if (c == 0)
    return 0;
  s = "x" + s + "x";
  auto stepL = make_dp(s);

  string rs = s;
  reverse(all(rs));
  auto stepR = make_dp(rs);
  reverse(all(stepR));

  // print(stepL);
  // print(stepR);
  set<int> res;
  rep1(i, n) {
    if (s[i] == 'o') {
      res.insert(i);
    }
  }
  rep1(i, n + c + 1) {
    int l = max(0, i - c - 1);
    int r = min(n + 1, i);
    // printf("stepL[%d]: %d, stepR[%d]: %d\\n",
    //     max(0,i-c-1),
    //     stepL[max(0,i-c-1)],
    //     min(n+1,i),
    //     stepR[min(n+1,i)]);
    if (stepL[l] + stepR[r] >= k) {
      while (res.upper_bound(l) != res.end()) {
        if (*res.upper_bound(l) < r) {
          res.erase(res.upper_bound(l));
        } else {
          break;
        }
      }
    }
  }
  for (int d : res) {
    cout << d << endl;
  }
  return 0;
}