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

//                                               KEEP GRINDING
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define int long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define precise(i) fixed << setprecision(10) << i
#define f(i, a, b) for (int i = a; i < b; ++i)

#define endl '\\n'
#define debug cout << "\\n========================================\\n" << endl;
#define err1(a) cout << #a << " " << a << endl;
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl;
#define err3(a, b, c)                                                          \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << endl;
#define err4(a, b, c, d)                                                       \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << " " << #d << " " << d << endl;

#define all(a) a.begin(), a.end()
#define show(a)                                                                \\
  for (auto xyz : a)                                                           \\
    cout << xyz << " ";                                                        \\
  cout << endl;
#define mx(a) *max_element(a)
#define mn(a) *min_element(a)
#define LB                                                                     \\
  lower_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >=
              // OUR VALUE
#define UB                                                                     \\
  upper_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >
              // OUR VALUE

#define fr first
#define sc second

#define MAXN 500000
#define INF 9223372036854775807
int N, K;
vector<int> help(vector<int> A) {

  vector<int> B(N, 0);
  for (int i = 0; i < N; i++) {
    int l = max((int)0, i - A[i]);
    int r = min(N - 1, i + A[i]);
    B[l]++;
    if (r + 1 < N)
      B[r + 1]--;
  }
  for (int i = 1; i < N; i++)
    B[i] += B[i - 1];
  return B;
}
signed main() {
  fastio cin >> N >> K;
  vector<int> a(N);
  f(i, 0, N) cin >> a[i];

  K = min(K, (int)42);
  for (int i = 1; i <= K; i++) {
    a = help(a);
  }
  show(a);
}

Error: TLE

Fixed submission:

//                                               KEEP GRINDING
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define int long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define precise(i) fixed << setprecision(10) << i
#define f(i, a, b) for (int i = a; i < b; ++i)

#define endl '\\n'
#define debug cout << "\\n========================================\\n" << endl;
#define err1(a) cout << #a << " " << a << endl;
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl;
#define err3(a, b, c)                                                          \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << endl;
#define err4(a, b, c, d)                                                       \\
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \\
       << " " << #d << " " << d << endl;

#define all(a) a.begin(), a.end()
#define show(a)                                                                \\
  for (auto xyz : a)                                                           \\
    cout << xyz << " ";                                                        \\
  cout << endl;
#define mx(a) *max_element(a)
#define mn(a) *min_element(a)
#define LB                                                                     \\
  lower_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >=
              // OUR VALUE
#define UB                                                                     \\
  upper_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >
              // OUR VALUE

#define fr first
#define sc second

#define MAXN 500000
#define INF 9223372036854775807
int n, k;
vector<int> help(vector<int> v) {

  vector<int> ans(n, 1);

  new_data_set s;
  f(i, 0, n) {
    ans[i] += (int)s.size() - (int)s.order_of_key(i);
    s.insert(i + v[i]);
  }
  s.clear();
  for (int i = n - 1; i >= 0; --i) {
    ans[i] += (int)s.order_of_key(i + 1);
    s.insert(i - v[i]);
  }
  return ans;
}
signed main() {
  fastio cin >> n >> k;
  vector<int> a(n);
  f(i, 0, n) cin >> a[i];

  for (int i = 1; i <= k; i++) {
    vector<int> temp = help(a);
    bool f = 1;
    f(i, 0, n) {
      if (temp[i] != n) {
        f = 0;
        break;
      }
    }
    if (f) {
      show(temp);
      return 0;
    }
    a = temp;
  }
  show(a);
}