Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ls p << 1
#define rs p << 1 | 1
#define rgi register int
#define ll long long
#define pii pair<int, int>
#define mkp make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
const int maxn = 100005;
int n, m, a[maxn], b[maxn];
struct sgt {
  int p, l, r;
  ll val, add;
} t[maxn << 2];
void build(int p, int l, int r) {
  t[p].l = l, t[p].r = r;
  if (l == r) {
    t[p].val = a[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(ls, l, mid);
  build(rs, mid + 1, r);
  t[p].val = t[ls].val + t[rs].val;
}
inline void spread(int p) {
  if (t[p].add) {
    t[ls].val += t[p].add * (t[ls].r - t[ls].l + 1);
    t[rs].val += t[p].add * (t[rs].r - t[rs].l + 1);
    t[ls].add += t[p].add;
    t[rs].add += t[p].add;
    t[p].add = 0;
  }
}
void change(int p, int l, int r, ll x) {
  if (l <= t[p].l && t[p].r <= r) {
    t[p].add += x;
    t[p].val += x * (t[p].r - t[p].l + 1);
    return;
  }
  spread(p);
  int mid = (t[p].l + t[p].r) >> 1;
  if (mid >= l)
    change(ls, l, r, x);
  if (mid < r)
    change(rs, l, r, x);
  t[p].val = t[ls].val + t[rs].val;
}
ll sum(int p, int l, int r) {
  if (l <= t[p].l && t[p].r <= r)
    return t[p].val;
  spread(p);
  int mid = (t[p].l + t[p].r) >> 1;
  ll ret = 0;
  if (mid >= l)
    ret += sum(ls, l, r);
  if (mid < r)
    ret += sum(rs, l, r);
  return ret;
}
bool check() {
  for (int i = 1; i <= n; ++i)
    if (b[i] != n)
      return 0;
  return 1;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  build(1, 1, n);
  memset(a, 0, sizeof(a));
  for (int i = 1; i <= m; ++i) {
    for (int i = 1; i <= n; ++i)
      b[i] = sum(1, i, i);
    if (check())
      break;
    build(1, 1, n);
    for (int i = 1; i <= n; ++i) {
      int l = max(i - b[i], 1), r = min(i + b[i], n);
      change(1, l, r, 1);
    }
  }
  for (int i = 1; i <= n; ++i)
    cout << sum(1, i, i) << " ";
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ls p << 1
#define rs p << 1 | 1
#define rgi register int
#define ll long long
#define pii pair<int, int>
#define mkp make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
const int maxn = 200005;
int n, m, a[maxn], b[maxn];
struct sgt {
  int p, l, r;
  ll val, add;
} t[maxn << 2];
void build(int p, int l, int r) {
  t[p].l = l, t[p].r = r;
  if (l == r) {
    t[p].val = a[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(ls, l, mid);
  build(rs, mid + 1, r);
  t[p].val = t[ls].val + t[rs].val;
}
inline void spread(int p) {
  if (t[p].add) {
    t[ls].val += t[p].add * (t[ls].r - t[ls].l + 1);
    t[rs].val += t[p].add * (t[rs].r - t[rs].l + 1);
    t[ls].add += t[p].add;
    t[rs].add += t[p].add;
    t[p].add = 0;
  }
}
void change(int p, int l, int r, ll x) {
  if (l <= t[p].l && t[p].r <= r) {
    t[p].add += x;
    t[p].val += x * (t[p].r - t[p].l + 1);
    return;
  }
  spread(p);
  int mid = (t[p].l + t[p].r) >> 1;
  if (mid >= l)
    change(ls, l, r, x);
  if (mid < r)
    change(rs, l, r, x);
  t[p].val = t[ls].val + t[rs].val;
}
ll sum(int p, int l, int r) {
  if (l <= t[p].l && t[p].r <= r)
    return t[p].val;
  spread(p);
  int mid = (t[p].l + t[p].r) >> 1;
  ll ret = 0;
  if (mid >= l)
    ret += sum(ls, l, r);
  if (mid < r)
    ret += sum(rs, l, r);
  return ret;
}
bool check() {
  for (int i = 1; i <= n; ++i)
    if (b[i] != n)
      return 0;
  return 1;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  build(1, 1, n);
  memset(a, 0, sizeof(a));
  for (int i = 1; i <= m; ++i) {
    for (int i = 1; i <= n; ++i)
      b[i] = sum(1, i, i);
    if (check())
      break;
    build(1, 1, n);
    for (int i = 1; i <= n; ++i) {
      int l = max(i - b[i], 1), r = min(i + b[i], n);
      change(1, l, r, 1);
    }
  }
  for (int i = 1; i <= n; ++i)
    cout << sum(1, i, i) << " ";
  cout << endl;
  return 0;
}