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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <new>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n, m) for (int i = n; i < m; i++)

using ll = long long;
using Vii = vector<vector<int>>;
using Gjudge = vector<vector<bool>>;
using Vi = vector<int>;
using Vl = vector<ll>;
using Vll = vector<vector<ll>>;
using Vs = vector<string>;
using Vb = vector<bool>;
using the = pair<int, int>;
using lthe = pair<ll, ll>;

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

const int Mod = static_cast<int>(1e9 + 7);
const int INF = INT_MAX;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int n, k;
  cin >> n >> k;
  Vi A(n);
  rep(i, n) cin >> A[i];

  if (k >= 50) {
    rep(i, n) {
      if (i)
        cout << " " << n;
      else
        cout << n;
    }
    cout << endl;
    return 0;
  }

  for (int i = 0; i < k; i++) {
    Vi B(n, 0);
    for (int j = 0; j < n; j++) {
      int l = max(0, j - A[j]);
      int r = min(n - 1, j + A[j]);
      B[l]++;
      if (r + 1 <= n - 1)
        B[r + 1]--;
    }
    for (int i = 1; i < n; i++) {
      B[i] += B[i - 1];
    }
    rep(i, n) A[i] = B[i];
  }

  rep(i, n) {
    if (i)
      cout << " " << A[i];
    else
      cout << A[i];
  }
  cout << endl;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <new>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n, m) for (int i = n; i < m; i++)

using ll = long long;
using Vii = vector<vector<int>>;
using Gjudge = vector<vector<bool>>;
using Vi = vector<int>;
using Vl = vector<ll>;
using Vll = vector<vector<ll>>;
using Vs = vector<string>;
using Vb = vector<bool>;
using the = pair<int, int>;
using lthe = pair<ll, ll>;

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

const int Mod = static_cast<int>(1e9 + 7);
const int INF = INT_MAX;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int n, k;
  cin >> n >> k;
  Vi A(n);
  rep(i, n) cin >> A[i];

  if (k >= 50) {
    rep(i, n) {
      if (i)
        cout << " " << n;
      else
        cout << n;
    }
    cout << endl;
    return 0;
  }

  for (int i = 0; i < k; i++) {
    Vi B(n, 0);
    for (int j = 0; j < n; j++) {
      for (int z = min(A[j], max(n - j, j)); z >= 0; z--) {
        if (z == 0) {
          B[j]++;
          continue;
        }
        if (j + z < n) {
          B[j + z]++;
        }
        if (j - z >= 0) {
          B[j - z]++;
        }
      }
    }
    rep(i, n) A[i] = B[i];
  }
  rep(i, n) {
    if (i)
      cout << " " << A[i];
    else
      cout << A[i];
  }
  cout << endl;
}