Problem p02852 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

#define REP(i, n) for (int i = 0; i < n; ++i)
#define ALL(name) name.begin(), name.end()
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 100010

int N, M;
string S;

vector<int> l_zero,
    r_zero; // i からみて 1 がないすぐ左／右の場所(自分が 0 なら自分)

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M >> S;

  // -1 チェック
  int cnt = 0;
  REP(i, N + 1) {
    if (S[i] == '1') {
      cnt++;
      if (cnt >= M) {
        printf("-1\n");
        return 0;
      }
    } else {
      cnt = 0;
    }
  }

  // l_zero, r_zero の作成
  l_zero.assign(N + 1, -1);
  r_zero.assign(N + 1, -1);
  int cur = 0;
  REP(i, N + 1) {
    if (S[i] == '0') {
      cur = i;
    }
    l_zero[i] = cur;
  }
  cur = N;
  for (int i = N; i >= 0; --i) {
    if (S[i] == '0') {
      cur = i;
    }
    r_zero[i] = cur;
  }

  // まず最短距離を出す
  int ans_dist = 0;
  vector<int> ans_pos;
  cur = 0;
  ans_pos.push_back(0);
  while (cur < N) {
    // 行けそうなら行く
    if (cur + M >= N) {
      cur = N;
      ans_dist++;
      break;
    }

    int next_cur = cur + M;
    if (S[next_cur] == '1') {
      next_cur = l_zero[next_cur];
    }
    cur = next_cur;
    ans_pos.push_back(cur);
    ans_dist++;
  }
  ans_pos.push_back(N);

  // ans_pos を左に寄せられないか調整
  reverse(ans_pos.begin(), ans_pos.end());
  vector<int> ans_r;
  REP(i, ans_pos.size() - 1) {
    // 現在の移動距離
    int cur_dist = ans_pos[i] - ans_pos[i + 1];
    // まだいける移動距離
    int sub_dist = M - cur_dist;

    int new_pos = ans_pos[i + 1] - sub_dist;
    new_pos = r_zero[new_pos];
    int new_dist = ans_pos[i] - new_pos;
    ans_r.push_back(new_dist);

    ans_pos[i + 1] = new_pos;
  }

  reverse(ans_r.begin(), ans_r.end());
  REP(i, ans_r.size()) {
    printf("%lld%c", ans_r[i], (i == ans_r.size() - 1) ? '\n' : ' ');
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

#define REP(i, n) for (int i = 0; i < n; ++i)
#define ALL(name) name.begin(), name.end()
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 100010

int N, M;
string S;

vector<int> l_zero,
    r_zero; // i からみて 1 がないすぐ左／右の場所(自分が 0 なら自分)

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M >> S;

  // -1 チェック
  int cnt = 0;
  REP(i, N + 1) {
    if (S[i] == '1') {
      cnt++;
      if (cnt >= M) {
        printf("-1\n");
        return 0;
      }
    } else {
      cnt = 0;
    }
  }

  // l_zero, r_zero の作成
  l_zero.assign(N + 1, -1);
  r_zero.assign(N + 1, -1);
  int cur = 0;
  REP(i, N + 1) {
    if (S[i] == '0') {
      cur = i;
    }
    l_zero[i] = cur;
  }
  cur = N;
  for (int i = N; i >= 0; --i) {
    if (S[i] == '0') {
      cur = i;
    }
    r_zero[i] = cur;
  }

  // まず最短距離を出す
  int ans_dist = 0;
  vector<int> ans_pos;
  cur = 0;
  ans_pos.push_back(0);
  while (cur < N) {
    // 行けそうなら行く
    if (cur + M >= N) {
      cur = N;
      ans_dist++;
      break;
    }

    int next_cur = cur + M;
    if (S[next_cur] == '1') {
      next_cur = l_zero[next_cur];
    }
    cur = next_cur;
    ans_pos.push_back(cur);
    ans_dist++;
  }
  ans_pos.push_back(N);

  // ans_pos を左に寄せられないか調整
  reverse(ans_pos.begin(), ans_pos.end());
  vector<int> ans_r;
  REP(i, ans_pos.size() - 1) {
    // 現在の移動距離
    int cur_dist = ans_pos[i] - ans_pos[i + 1];
    // まだいける移動距離
    int sub_dist = M - cur_dist;

    int new_pos = ans_pos[i + 1] - sub_dist;
    if (new_pos < 0) {
      new_pos = 0;
    }
    new_pos = r_zero[new_pos];
    int new_dist = ans_pos[i] - new_pos;
    ans_r.push_back(new_dist);

    ans_pos[i + 1] = new_pos;
  }

  reverse(ans_r.begin(), ans_r.end());
  REP(i, ans_r.size()) {
    printf("%lld%c", ans_r[i], (i == ans_r.size() - 1) ? '\n' : ' ');
  }

  return 0;
}