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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
#define rep(x, b, e) for (int x = (b); x < (e); ++x)
#define ford(x, b, e) for (int x = ((int)(b)) - 1; x >= (e); --x)
#define all(c) c.begin(), c.end()
#define sz(x) ((int)((x).size()))
#define pb push_back
#define st first
#define nd second
#define mp(x, y) make_pair(x, y)
typedef short int sint;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it) {
    *this << ((it != d.b) ? ", " : "") << *it;
  }
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int nax = 2e5;
int n, k;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> k;
  // n = 2e5, k = 1e5;
  vector<int> a(n);
  rep(i, 0, n) { cin >> a[i]; }
  rep(q, 0, k) {
    vi cnt(n + 1);
    rep(j, 0, n) {
      int st = max(0, j - a[j]);
      int en = min(n, j + a[j] + 1);
      ++cnt[st];
      --cnt[en];
    }
    int curr = 0;
    int wsk = 0;
    vi b(n);
    rep(j, 0, n) {
      curr += cnt[j];
      b[j] = curr;
    }
    a = b;
    bool alln = true;
    rep(i, 0, n) {
      if (a[i] != n) {
        alln = false;
      }
    }
    if (alln) {
      break;
    }
  }
  rep(i, 0, n) cout << a[i] << ' ';
  cout << "\\n";
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
#define rep(x, b, e) for (int x = (b); x < (e); ++x)
#define ford(x, b, e) for (int x = ((int)(b)) - 1; x >= (e); --x)
#define all(c) c.begin(), c.end()
#define sz(x) ((int)((x).size()))
#define pb push_back
#define st first
#define nd second
#define mp(x, y) make_pair(x, y)
typedef short int sint;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it) {
    *this << ((it != d.b) ? ", " : "") << *it;
  }
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int nax = 2e5;
int n, k;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> k;
  vector<int> a(n);
  rep(i, 0, n) { cin >> a[i]; }
  rep(q, 0, k) {
    vector<pii> eve;
    rep(j, 0, n) {
      int st = max(0, j - a[j]);
      int en = min(n - 1, j + a[j]);
      eve.pb({st, -1});
      eve.pb({en, 1});
    }
    sort(all(eve));
    int curr = 0;
    int wsk = 0;
    vi b(n);
    rep(j, 0, n) {
      while (wsk < sz(eve) && eve[wsk].st <= j && eve[wsk].nd == -1) {
        curr -= eve[wsk].nd;
        ++wsk;
      }
      b[j] = curr;
      while (wsk < sz(eve) && eve[wsk].st <= j && eve[wsk].nd == 1) {
        curr -= eve[wsk].nd;
        ++wsk;
      }
    }
    a = b;
    bool alln = true;
    rep(i, 0, n) {
      if (a[i] != (n - 1)) {
        alln = false;
      }
    }
    if (alln) {
      break;
    }
  }
  rep(i, 0, n) cout << a[i] << ' ';
  cout << "\\n";
}