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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

inline void chk_max(int &a, int b) {
  if (a < b)
    a = b;
}

char s[MAXN];
int pre[MAXN], suf[MAXN];
ll dif[MAXN];
inline void update(int l, int r) {
  ++dif[l];
  --dif[r + 1];
}

vector<int> ans1, ans2;
bool vis[MAXN];

int main(void) {
  int n, d, c;
  scanf("%d%d%d%s", &n, &d, &c, s + 1);

  for (int i = 1; i <= n; ++i) {
    pre[i] = pre[i - 1];
    if (s[i] == 'x')
      continue;
    chk_max(pre[i], 1);
    if (i - c - 1 >= 1)
      chk_max(pre[i], pre[i - c - 1] + 1);
  }
  for (int i = n; i >= 1; --i) {
    suf[i] = suf[i + 1];
    if (s[i] == 'x')
      continue;
    chk_max(suf[i], 1);
    if (i + c + 1 <= n)
      chk_max(suf[i], suf[i + c + 1] + 1);
  }

  // for(int i=1; i<=n; ++i) printf("%d %d\\n",pre[i],suf[i]);

  for (int i = 1; i <= n; ++i)
    if (pre[i - 1] + suf[i + 1] < d)
      ans1.push_back(i);
  //			printf("%d\\n",i);

  for (int i = 1; i <= n; ++i) {
    if (pre[i - 1] >= d || suf[i + 1] >= d)
      update(i, i);
  }
  for (int i = 1; i + c - 1 <= n; ++i) {
    int j = i + c - 1;
    if (pre[i - 1] + suf[j + 1] >= d)
      update(i, j);
  }
  for (int i = 1; i <= n; ++i)
    dif[i] += dif[i - 1];

  for (int i = 1; i <= n; ++i)
    if (s[i] == 'o' && !dif[i])
      ans2.push_back(i);
  //			printf("%d\\n",i);

  if (ans1.size() == ans2.size()) {
    for (int i = 0; i < (int)ans2.size(); ++i)
      printf("%d\\n", ans2[i]);
    return 0;
  }

  if (ans1.size() > ans2.size())
    while (1)
      ;

  for (int i = 0; i < (int)ans2.size(); ++i)
    vis[ans2[i]] = 1;
  for (int i = 0; i < (int)ans1.size(); ++i)
    if (!vis[ans1[i]])
      return 114514;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

inline void chk_max(int &a, int b) {
  if (a < b)
    a = b;
}

char s[MAXN];
int pre[MAXN], suf[MAXN];
ll dif[MAXN];
inline void update(int l, int r) {
  ++dif[l];
  --dif[r + 1];
}

vector<int> ans1, ans2;

int main(void) {
  int n, d, c;
  scanf("%d%d%d%s", &n, &d, &c, s + 1);

  for (int i = 1; i <= n; ++i) {
    pre[i] = pre[i - 1];
    if (s[i] == 'x')
      continue;
    chk_max(pre[i], 1);
    if (i - c - 1 >= 1)
      chk_max(pre[i], pre[i - c - 1] + 1);
  }
  for (int i = n; i >= 1; --i) {
    suf[i] = suf[i + 1];
    if (s[i] == 'x')
      continue;
    chk_max(suf[i], 1);
    if (i + c + 1 <= n)
      chk_max(suf[i], suf[i + c + 1] + 1);
  }

  // for(int i=1; i<=n; ++i) printf("%d %d\\n",pre[i],suf[i]);

  for (int i = 1; i <= n; ++i)
    if (pre[i - 1] + suf[i + 1] < d)
      ans1.push_back(i);
  //			printf("%d\\n",i);

  for (int i = 1; i <= n; ++i) {
    if (pre[i - 1] >= d || suf[i + 1] >= d)
      update(i, i);
  }
  for (int i = 1; i + c - 1 <= n; ++i) {
    int j = i + c - 1;
    if (pre[i - 1] + suf[j + 1] >= d)
      update(i, j);
  }
  for (int i = 1; i <= n; ++i)
    dif[i] += dif[i - 1];

  for (int i = 1; i <= n; ++i)
    if (s[i] == 'o' && !dif[i])
      ans2.push_back(i);
  //			printf("%d\\n",i);

  if (ans1.size() == ans2.size()) {
    for (int i = 0; i < (int)ans2.size(); ++i)
      printf("%d\\n", ans2[i]);
    return 0;
  }
  if (ans1.size() > ans2.size())
    while (1)
      ;
  else
    return 114514;
  return 0;
}