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

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

// typedef
// -----------------------------------------------------------------------------
typedef long long ll;
typedef long double ld;

// container util
// -----------------------------------------------------------------------------
#define ALL(c) (c).begin(), (c).end()
#define SZ(c) ((ll)(c).size())
#define MAX(c) *max_element(ALL(c))
#define MIN(c) *min_element(ALL(c))
#define SORT(c) sort((c).begin(), (c).end())

// repetition
// -----------------------------------------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
// -----------------------------------------------------------------------------
const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1.0);

// debug
// -----------------------------------------------------------------------------
#define DUMP(x) cerr << #x << " = " << (x) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  int N, K;
  cin >> N >> K;
  K = min(K, 100);
  vector<int> A(N + 1, 0);
  FOR(i, 1, N + 1) cin >> A[i];
  REP(i, K) {
    vector<int> tmp(N + 1, 0);
    FOR(i, 1, N + 1) {
      int d = A[i];
      int b = (i - d < 0) ? 0 : i - d;
      tmp[b]++;
      if (i + d + 1 <= N) {
        int e = i + d + 1;
        tmp[e]--;
      }
    }
    A[0] = tmp[0];
    FOR(i, 1, N + 1) { A[i] = A[i - 1] + tmp[i]; }
  }
  FOR(i, 1, N + 1) { cout << A[i] << " "; }
  cout << "\\n";
}

Error: TLE

Fixed submission:

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

// typedef
// -----------------------------------------------------------------------------
typedef long long ll;
typedef long double ld;

// container util
// -----------------------------------------------------------------------------
#define ALL(c) (c).begin(), (c).end()
#define SZ(c) ((ll)(c).size())
#define MAX(c) *max_element(ALL(c))
#define MIN(c) *min_element(ALL(c))
#define SORT(c) sort((c).begin(), (c).end())

// repetition
// -----------------------------------------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
// -----------------------------------------------------------------------------
const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1.0);

// debug
// -----------------------------------------------------------------------------
#define DUMP(x) cerr << #x << " = " << (x) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  int N, K;
  cin >> N >> K;
  vector<int> A(N + 1, 0);
  FOR(i, 1, N + 1) cin >> A[i];
  REP(i, K) {
    vector<int> tmp(N + 1, 0);
    FOR(i, 1, N + 1) {
      int d = A[i];
      int b = (i - d < 0) ? 0 : i - d;
      tmp[b]++;
      if (i + d + 1 <= N) {
        int e = i + d + 1;
        tmp[e]--;
      }
    }
    A[0] = tmp[0];
    FOR(i, 1, N + 1) { A[i] = A[i - 1] + tmp[i]; }
  }
  FOR(i, 1, N + 1) { cout << A[i] << " "; }
  cout << "\\n";
}