Problem p02647 (C++)

Buggy submission (status: Runtime Error):
// Date: 2020-06-13

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
#define IO                                                                     \
  freopen("in.txt", "r", stdin);                                               \
  freopen("out.txt", "w", stdout)
#define FIO                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define rep(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define per(i, b, a) for (int i = int(b); i >= int(a); --i)
#define D(x) cout << #x << " = " << x << endl;
#define mem(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)x.size())
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const LL INF = 1e18;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
LL qpow(LL x, LL y, LL MOD) {
  LL a = 1;
  while (y) {
    if (y & 1)
      a = a * x % MOD;
    x = x * x % MOD;
    y >>= 1;
  }
  return a;
}

int a[N], b[N];
int main() {
  FIO;
  int n, k;
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  while (k--) {
    int cnt = 0;
    rep(i, 0, n + 1) b[i] = 0;
    rep(i, 1, n) {
      b[i - a[i]]++;
      b[i + a[i] + 1]--;
    }
    rep(i, 1, n) {
      b[i] += b[i - 1];
      a[i] = b[i];
      if (a[i] == n)
        ++cnt;
    }
    if (cnt == n)
      break;
  }
  rep(i, 1, n) cout << a[i] << " ";

  return 0;
}

Fixed submission:
// Date: 2020-06-13

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
#define IO                                                                     \
  freopen("in.txt", "r", stdin);                                               \
  freopen("out.txt", "w", stdout)
#define FIO                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define rep(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define per(i, b, a) for (int i = int(b); i >= int(a); --i)
#define D(x) cout << #x << " = " << x << endl;
#define mem(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)x.size())
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const LL INF = 1e18;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
LL qpow(LL x, LL y, LL MOD) {
  LL a = 1;
  while (y) {
    if (y & 1)
      a = a * x % MOD;
    x = x * x % MOD;
    y >>= 1;
  }
  return a;
}

int a[N], b[N];
int main() {
  FIO;
  int n, k;
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  while (k--) {
    int cnt = 0;
    rep(i, 0, n + 1) b[i] = 0;
    rep(i, 1, n) {
      b[max(0, i - a[i])]++;
      b[min(n + 1, i + a[i] + 1)]--;
    }
    rep(i, 1, n) {
      b[i] += b[i - 1];
      a[i] = b[i];
      if (a[i] == n)
        ++cnt;
    }
    if (cnt == n)
      break;
  }
  rep(i, 1, n) cout << a[i] << " ";

  return 0;
}