Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define VIEW(x)                                                                \
  do {                                                                         \
    cerr << #x << ": ";                                                        \
    for (auto i : x)                                                           \
      cerr << i << " ";                                                        \
    cerr << endl;                                                              \
  } while (0)
#define ALL(x) (x).begin(), (x).end()
template <class T> bool umax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool umin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
template <typename T> void FILL(vector<T> &v, const T &x) {
  fill(v.begin(), v.end(), x);
}
template <typename T> void FILL(vector<vector<T>> &v, const T &x) {
  for (auto &i : v)
    fill(i.begin(), i.end(), x);
}

vector<int> Imos(int mx, vector<pair<int, int>> area) {
  /* O(mx + area.size()) */
  vector<int> d(mx + 2), sum(mx + 1);
  for (auto a : area) {
    assert(a.first <= a.second);
    d[a.first]++, d[a.second + 1]--; /* inclusive (a.first, a.second) */
  }
  sum[0] = d[0];
  for (int i = 1; i <= mx; ++i)
    sum[i] = sum[i - 1] + d[i];
  return sum;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  rep(r, k) {
    vector<int> d(n + 1);
    rep(i, n) {
      int l = max(i - a[i], 0), r = min(i + a[i], n);
      d[l]++;
      d[r + 1]--;
    }
    a[0] = d[0];
    rep(i, 1, n) a[i] = a[i - 1] + d[i];
    bool all_n = true;
    rep(i, n) if (a[i] != n) {
      all_n = false;
      break;
    }
    if (all_n)
      break;
  }
  rep(i, n) cout << a[i] << ((i == n - 1) ? "\n" : " ");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define VIEW(x)                                                                \
  do {                                                                         \
    cerr << #x << ": ";                                                        \
    for (auto i : x)                                                           \
      cerr << i << " ";                                                        \
    cerr << endl;                                                              \
  } while (0)
#define ALL(x) (x).begin(), (x).end()
template <class T> bool umax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool umin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
template <typename T> void FILL(vector<T> &v, const T &x) {
  fill(v.begin(), v.end(), x);
}
template <typename T> void FILL(vector<vector<T>> &v, const T &x) {
  for (auto &i : v)
    fill(i.begin(), i.end(), x);
}

vector<int> Imos(int mx, vector<pair<int, int>> area) {
  /* O(mx + area.size()) */
  vector<int> d(mx + 2), sum(mx + 1);
  for (auto a : area) {
    assert(a.first <= a.second);
    d[a.first]++, d[a.second + 1]--; /* inclusive (a.first, a.second) */
  }
  sum[0] = d[0];
  for (int i = 1; i <= mx; ++i)
    sum[i] = sum[i - 1] + d[i];
  return sum;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  rep(r, k) {
    vector<int> d(n + 1);
    rep(i, n) {
      int l = max(i - a[i], 0), r = min(i + a[i], n - 1);
      d[l]++;
      d[r + 1]--;
    }
    a[0] = d[0];
    rep(i, 1, n) a[i] = a[i - 1] + d[i];
    bool all_n = true;
    rep(i, n) if (a[i] != n) {
      all_n = false;
      break;
    }
    if (all_n)
      break;
  }
  rep(i, n) cout << a[i] << ((i == n - 1) ? "\n" : " ");
}