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

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;

#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define bk back()
#define pb push_back

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define trav(x, a) for (auto &x : a)

const int MX = 2e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1061109567; // 4557430888798830399LL
const ld EPS = 1e-9;
const ld PI = acos(-1);

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

// FILE I/O
void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }
void setIO(string s = "") {
  ios_base::sync_with_stdio(0);
  cin.tie(0); // fast I/O
  // cin.exceptions(cin.failbit);
  // throws exception when do smth illegal
  // ex. try to read letter into int
  if (sz(s)) {
    setIn(s + ".in"), setOut(s + ".out");
  } // for USACO
}

int32_t main() {

  setIO();

  int N, K;
  cin >> N >> K;

  vi arr(N);
  trav(x, arr) cin >> x;

  while (K--) {
    vi b(N);
    F0R(i, N) {
      int R = min(N - 1, i + arr[i]), L = max(0, i - arr[i]);
      b[L]++;
      if (R + 1 < N)
        b[R + 1]--;
    }

    F0R(i, N) {
      if (i > 0)
        b[i] += b[i - 1];
    }

    if (arr == b)
      break;
    arr = b;
  }

  trav(x, arr) cout << x << " ";
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;

#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define bk back()
#define pb push_back

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define trav(x, a) for (auto &x : a)

const int MX = 2e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1061109567; // 4557430888798830399LL
const ld EPS = 1e-9;
const ld PI = acos(-1);

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

// FILE I/O
void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }
void setIO(string s = "") {
  ios_base::sync_with_stdio(0);
  cin.tie(0); // fast I/O
  // cin.exceptions(cin.failbit);
  // throws exception when do smth illegal
  // ex. try to read letter into int
  if (sz(s)) {
    setIn(s + ".in"), setOut(s + ".out");
  } // for USACO
}

int32_t main() {

  setIO();

  int N, K;
  cin >> N >> K;

  vi arr(N);
  trav(x, arr) cin >> x;

  while (K--) {
    vi b(N + 1);
    F0R(i, N) {
      int R = min(N - 1, i + arr[i]), L = max(0, i - arr[i]);
      b[L]++;
      b[R + 1]--;
    }

    F0R(i, N) {
      if (i > 0)
        b[i] += b[i - 1];
    }

    F0R(i, N) arr[i] = b[i];
    bool ok = true;
    F0R(i, N - 1) if (arr[i] == arr[i + 1]) { ok = false; }
    if (ok)
      break;
  }

  trav(x, arr) cout << x << " ";
}