Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define debug(x)                                                               \\
  cout << "DEBUG"                                                              \\
       << " " << #x << ":" << x << '\\n'
#define rep(i, n)                                                              \\
  for (int_fast32_t i = 0; i < ((int_fast32_t)(n)); i++) // 0-indexed up
#define rep1(i, n)                                                             \\
  for (int_fast32_t i = 1; i <= ((int_fast32_t)(n)); i++) // 1-indexed up
#define rrep(i, n)                                                             \\
  for (int_fast32_t i = ((int_fast32_t)(n)-1); i >= 0; i--) // 0-indexed down
#define rrep1(i, n)                                                            \\
  for (int_fast32_t i = ((int_fast32_t)(n)); i >= 1; i--) // 1-indexed down
#define all(x) (x).begin(), (x).end()

using namespace std;

template <typename T> using vec = vector<T>;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
using ll = long long;
using ld = long double;
using vi = vec<int_fast32_t>;
using vl = vec<int_fast64_t>;
using vld = vec<ld>;
using vii = vec<vi>;
using PII = pair<int_fast32_t, int_fast32_t>;

template <class T> using maxheap = std::priority_queue<T>;
template <class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ld Pi = std::acos(-1.0L);
constexpr ll infll = (1LL << 62) - 1;
constexpr int inf = (1 << 30) - 1;
const int mod = 1000000007;
// const int mod = 998244353;

/*FUNCs=================================================*/

/*MAIN==================================================*/
signed main() {
  int START_TIME = clock();
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  //    cout << fixed << setprecision(15);

  i32 n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, n) cin >> a[i];

  i32 cnt = 0;
  rep(_, k) {
    vi tmp(n + 1);
    rep(i, n) {
      i32 d = a[i];
      if (d == 0)
        continue;
      else {
        ++tmp[max<i32>(0, i - d)];
        --tmp[min<i32>(n, i + d + 1)];
      }
    }
    rep(i, n) tmp[i + 1] += tmp[i];
    rep(i, n) if (a[i] == 0) tmp[i]++;
    rep(i, n) {
      if (tmp[i] > n) {
        tmp[i] = n;
      }
    }
    bool fin = 1;
    rep(i, n) if (tmp[i] != n) fin = 0;
    tmp.pop_back();
    a = tmp;
    ++cnt;
    if (fin)
      break;
  }

  //    debug(cnt);
  rep(i, n) cout << a[i] << (i == n - 1 ? '\\n' : ' ');

  // printf("ExecutionTime: %d
  // /ms\\n",1000*(int)((clock()-START_TIME)/CLOCKS_PER_SEC));
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define debug(x)                                                               \\
  cout << "DEBUG"                                                              \\
       << " " << #x << ":" << x << '\\n'
#define rep(i, n)                                                              \\
  for (int_fast32_t i = 0; i < ((int_fast32_t)(n)); i++) // 0-indexed up
#define rep1(i, n)                                                             \\
  for (int_fast32_t i = 1; i <= ((int_fast32_t)(n)); i++) // 1-indexed up
#define rrep(i, n)                                                             \\
  for (int_fast32_t i = ((int_fast32_t)(n)-1); i >= 0; i--) // 0-indexed down
#define rrep1(i, n)                                                            \\
  for (int_fast32_t i = ((int_fast32_t)(n)); i >= 1; i--) // 1-indexed down
#define all(x) (x).begin(), (x).end()

using namespace std;

template <typename T> using vec = vector<T>;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
using ll = long long;
using ld = long double;
using vi = vec<int_fast32_t>;
using vl = vec<int_fast64_t>;
using vld = vec<ld>;
using vii = vec<vi>;
using PII = pair<int_fast32_t, int_fast32_t>;

template <class T> using maxheap = std::priority_queue<T>;
template <class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ld Pi = std::acos(-1.0L);
constexpr ll infll = (1LL << 62) - 1;
constexpr int inf = (1 << 30) - 1;
const int mod = 1000000007;
// const int mod = 998244353;

/*FUNCs=================================================*/

/*MAIN==================================================*/
signed main() {
  int START_TIME = clock();
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  //    cout << fixed << setprecision(15);

  i32 n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, n) cin >> a[i];

  rep(_, k) {
    vi tmp(n + 1);
    rep(i, n) {
      i32 d = a[i];
      if (d == 0)
        continue;
      else {
        ++tmp[max<i32>(0, i - d)];
        --tmp[min<i32>(n, i + d + 1)];
      }
    }
    rep(i, n) tmp[i + 1] += tmp[i];
    rep(i, n) if (a[i] == 0) tmp[i]++;
    bool fin = 1;
    rep(i, n) {
      if (tmp[i] > n) {
        tmp[i] = n;
      } else
        fin = 0;
    }
    tmp.pop_back();
    a = tmp;
    if (fin)
      break;
  }

  rep(i, n) cout << a[i] << (i == n - 1 ? '\\n' : ' ');

  // printf("ExecutionTime: %d
  // /ms\\n",1000*(int)((clock()-START_TIME)/CLOCKS_PER_SEC));
}