Problem p02852 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define INF 1000000009
#define LINF 1000000000000000009
#define double long double
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

template <class T> class segtree {
  int n;
  vector<T> data;
  T def;
  function<T(T, T)> operation;
  function<T(T, T)> update;
  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return def;
    if (a <= l && r <= b)
      return data[k];
    T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
    T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
    return operation(c1, c2);
  }

public:
  segtree(size_t _n, T _def, function<T(T, T)> _operation,
          function<T(T, T)> _update)
      : def(_def), operation(_operation), update(_update) {
    n = 1;
    while (n < _n) {
      n *= 2;
    }
    data = vector<T>(2 * n - 1, def);
  }
  void change(int i, T x) {
    i += n - 1;
    data[i] = update(data[i], x);
    while (i > 0) {
      i = (i - 1) / 2;
      data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
    }
  }
  T query(int a, int b) { return _query(a, b, 0, 0, n); }
  T operator[](int i) { return data[i + n - 1]; }
};

int main() {
  int n, m;
  string s;
  cin >> n >> m >> s;
  segtree<int> seg(
      n + 2, INF, [](int a, int b) { return min(a, b); },
      [](int a, int b) { return b; });
  vector<int> ans(n + 1);
  seg.change(n, 0);
  ans[0] = n;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      int c = seg.query(i + 1, min(i + m + 1, n + 1));
      seg.change(i, c + 1);
      ans[c + 1] = i;
    }
  }
  if (seg[0] >= INF) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = seg[0] - 1; i >= 0; i--) {
    cout << ans[i] - ans[i + 1] << ' ';
  }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define INF 1000000009
#define LINF 1000000000000000009
#define double long double
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

template <class T> class segtree {
  int n;
  vector<T> data;
  T def;
  function<T(T, T)> operation;
  function<T(T, T)> update;
  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return def;
    if (a <= l && r <= b)
      return data[k];
    T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
    T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
    return operation(c1, c2);
  }

public:
  segtree(size_t _n, T _def, function<T(T, T)> _operation,
          function<T(T, T)> _update)
      : def(_def), operation(_operation), update(_update) {
    n = 1;
    while (n < _n) {
      n *= 2;
    }
    data = vector<T>(2 * n - 1, def);
  }
  void change(int i, T x) {
    i += n - 1;
    data[i] = update(data[i], x);
    while (i > 0) {
      i = (i - 1) / 2;
      data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
    }
  }
  T query(int a, int b) { return _query(a, b, 0, 0, n); }
  T operator[](int i) { return data[i + n - 1]; }
};

int main() {
  int n, m;
  string s;
  cin >> n >> m >> s;
  segtree<int> seg(
      n + 2, INF, [](int a, int b) { return min(a, b); },
      [](int a, int b) { return b; });
  vector<int> ans(n + 1);
  seg.change(n, 0);
  ans[0] = n;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      int c = seg.query(i + 1, min(i + m + 1, n + 1));
      seg.change(i, c + 1);
      if (c < INF)
        ans[c + 1] = i;
    }
  }
  if (seg[0] >= INF) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = seg[0] - 1; i >= 0; i--) {
    cout << ans[i] - ans[i + 1] << ' ';
  }
  cout << endl;
}