Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = int64_t;
using uint = uint32_t;
using ulint = uint64_t;

template <class T> using vector2d = vector<vector<T>>;

template <class T> bool UpdateMax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> bool UpdateMin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> void OutVec(const vector<T> &vec) {
  for (int i = 0; i < vec.size() - 1; ++i) {
    cout << vec[i] << " ";
  }
  cout << vec.back() << endl;
}

template <class T> void OutVec2d(const vector2d<T> &vec) {
  for (auto v : vec) {
    OutVec(v);
  }
}

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < k; i++) {
    vector<int> imos(n + 1);
    for (int j = 0; j < n; j++) {
      int l = max(0, j - a[j]);
      int r = min(n, j + a[j]);
      imos[l]++;
      imos[r + 1]--;
    }
    a[0] = imos[0];
    for (int j = 0; j < n; j++) {
      a[j] = a[j - 1] + imos[j];
    }
    bool all_n = true;
    for (int j = 0; j < n; j++) {
      if (a[j] != n) {
        all_n = false;
        break;
      }
    }
    if (all_n) {
      OutVec(a);
      return 0;
    }
  }
  OutVec(a);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = int64_t;
using uint = uint32_t;
using ulint = uint64_t;

template <class T> using vector2d = vector<vector<T>>;

template <class T> bool UpdateMax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> bool UpdateMin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> void OutVec(const vector<T> &vec) {
  for (int i = 0; i < vec.size() - 1; ++i) {
    cout << vec[i] << " ";
  }
  cout << vec.back() << endl;
}

template <class T> void OutVec2d(const vector2d<T> &vec) {
  for (auto v : vec) {
    OutVec(v);
  }
}

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < k; i++) {
    vector<int> imos(n + 10);
    for (int j = 0; j < n; j++) {
      int l = max(0, j - a[j]);
      int r = min(n, j + a[j]);
      imos[l]++;
      imos[r + 1]--;
    }
    a[0] = imos[0];
    for (int j = 0; j < n; j++) {
      a[j] = a[j - 1] + imos[j];
    }
    bool all_n = true;
    for (int j = 0; j < n; j++) {
      if (a[j] != n) {
        all_n = false;
        break;
      }
    }
    if (all_n) {
      OutVec(a);
      return 0;
    }
  }
  OutVec(a);

  return 0;
}