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

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, K;
  cin >> N >> K;
  vector<i64> as(N);
  F0R(i, N) { cin >> as[i]; }
  vector<int> ls(N + 1), rs(N);
  ls[N] = INT32_MAX;
  F0R(i, K) {
    F0R(j, N) {
      ls[j] = j - as[j];
      rs[j] = j + as[j] + 1;
    }
    sort(rng(ls));
    sort(rng(rs));
    // Print(ls);
    // Print(rs);
    int li = 0, ri = 0;
    int low = N + 1;
    int n = 0;
    F0R(j, N) {
      while (ls[li] <= j) {
        n++;
        li++;
      }
      while (rs[ri] == j) {
        n--;
        ri++;
      }
      chmin(low, as[j] = n);
    }

    if (low >= N) {
      break;
    }
  }

  F0R(i, N) { cout << as[i] << ' '; }
}
#endif


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, K;
  cin >> N >> K;
  vector<i64> as(N);
  F0R(i, N) { cin >> as[i]; }
  F0R(i, K) {
    vector<i64> bs(N);
    F0R(j, N) {
      int l = j - as[j];
      int r = j + as[j];
      FOR(k, max(l, 0), min(r + 1, N)) { bs[k]++; }
    }
    // Print(bs);
    int high = 0;
    F0R(j, N) { chmax(high, bs[j]); }
    swap(as, bs);

    if (high >= N - 1) {
      i64 add = (i64)N * (K - i - 1);
      F0R(j, N) { as[j] += add; }
    }
  }

  F0R(i, N) { cout << as[i] << ' '; }
}
#endif