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

#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;

using ll = int;
using ldb = long double;
using pa = pair<ll, ll>;
using vec = vector<ll>;

template <typename T>
using index_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>;

#define pb push_back
#define po pop_back
#define mp make_pair
#define mt make_tuple
#define unique_sort(x)                                                         \\
  sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))
#define F first
#define S second
#define int ll
#define f(i, x, n) for (ll i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define str string
#define edl "\\n"
#define add insert
#define tm (tl + tr) / 2
#define child 2 * v
#define ceils(s, n) (s + n - 1) / n
#define cot continue
#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

const int N = 2e5 + 10;
const double PI = acos(-1);
// const ll LINF = LLONG_MAX;
ll a[N], n, query, V[N];
ll seg[45][4 * N];
ll lazy[45][4 * N];
void push(ll v, ll i) {
  seg[i][child] += lazy[i][v];
  lazy[i][child] += lazy[i][v];
  seg[i][child + 1] += lazy[i][v];
  lazy[i][child + 1] += lazy[i][v];
  lazy[i][v] = 0;
}
void update(ll v, ll tl, ll tr, ll ql, ll qr, ll val, ll i) {

  if (ql > qr) {
    return;
  }
  if (ql == tl and qr == tr) {
    seg[i][v] += val;
    lazy[i][v] += val;
  } else {
    push(v, i);
    update(child, tl, tm, ql, min(qr, tm), val, i);
    update(child + 1, tm + 1, tr, max(ql, tm + 1), qr, val, i);
    seg[i][v] = seg[i][child] + seg[i][child + 1];
  }
}
ll rquery(ll v, ll tl, ll tr, ll ql, ll qr, ll i) {
  if (ql > qr) {
    return 0;
  }
  if (ql <= tl and tr <= qr) {
    return seg[i][v];
  }
  push(v, i);
  return rquery(child, tl, tm, ql, min(qr, tm), i) +
         rquery(child + 1, tm + 1, tr, max(ql, tm + 1), qr, i);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int k;
  cin >> n >> k;
  f(i, 1, n + 1) { cin >> V[i]; }
  double r = pow(n, 0.44);
  if (k >= r) {
    f(i, 1, n + 1) { cout << n << " "; }
    return 0;
  }
  int c1 = 1;
  int tmp[n + 2];
  f(i, 1, n + 2) { tmp[i] = 0; }
  while (k--) {
    f(i, 1, n + 1) {
      int a1 = max(1, i - V[i]);
      int a2 = min(n, i + V[i]);

      tmp[a1]++;
      tmp[a2 + 1]--;
      // update(1,1,n,a1,a2,1,c1);
    }
    int c = 0, cc = 0;
    f(i, 1, n + 1) {
      cc += tmp[i];
      V[i] = cc;
      c += (V[i] >= n);
      tmp[i] = cc;
    }
    if (c == n) {
      for (int i = 1; i <= n; i++) {
        cout << V[i] << " ";
      }
      return 0;
    }
    f(i, 1, n + 2) { tmp[i] = 0; }
    c1++;
  }
  for (int i = 1; i <= n; i++) {
    cout << V[i] << " ";
  }
  return 0;
}


Error: TLE

Fixed submission:

#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;

using ll = int;
using ldb = long double;
using pa = pair<ll, ll>;
using vec = vector<ll>;

template <typename T>
using index_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>;

#define pb push_back
#define po pop_back
#define mp make_pair
#define mt make_tuple
#define unique_sort(x)                                                         \\
  sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))
#define F first
#define S second
#define int ll
#define f(i, x, n) for (ll i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define str string
#define edl "\\n"
#define add insert
#define tm (tl + tr) / 2
#define child 2 * v
#define ceils(s, n) (s + n - 1) / n
#define cot continue
#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

const int N = 2e5 + 10;
const double PI = acos(-1);
// const ll LINF = LLONG_MAX;
ll a[N], n, query, V[N];
ll seg[45][4 * N];
ll lazy[45][4 * N];
void push(ll v, ll i) {
  seg[i][child] += lazy[i][v];
  lazy[i][child] += lazy[i][v];
  seg[i][child + 1] += lazy[i][v];
  lazy[i][child + 1] += lazy[i][v];
  lazy[i][v] = 0;
}
void update(ll v, ll tl, ll tr, ll ql, ll qr, ll val, ll i) {

  if (ql > qr) {
    return;
  }
  if (ql == tl and qr == tr) {
    seg[i][v] += val;
    lazy[i][v] += val;
  } else {
    push(v, i);
    update(child, tl, tm, ql, min(qr, tm), val, i);
    update(child + 1, tm + 1, tr, max(ql, tm + 1), qr, val, i);
    seg[i][v] = seg[i][child] + seg[i][child + 1];
  }
}
ll rquery(ll v, ll tl, ll tr, ll ql, ll qr, ll i) {
  if (ql > qr) {
    return 0;
  }
  if (ql <= tl and tr <= qr) {
    return seg[i][v];
  }
  push(v, i);
  return rquery(child, tl, tm, ql, min(qr, tm), i) +
         rquery(child + 1, tm + 1, tr, max(ql, tm + 1), qr, i);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int k;
  cin >> n >> k;
  f(i, 1, n + 1) { cin >> V[i]; }
  double r = pow(n, 0.44);
  if (k >= r) {
    f(i, 1, n + 1) { cout << n << " "; }
    return 0;
  }
  int c1 = 1;
  while (k--) {
    // c1++;
    f(i, 1, n + 1) {
      int a1 = max(1, i - V[i]);
      int a2 = min(n, i + V[i]);
      update(1, 1, n, a1, a2, 1, c1);
    }
    int c = 0;
    f(i, 1, n + 1) {
      V[i] = rquery(1, 1, n, i, i, c1);
      c += (V[i] >= n);
    }
    if (c == n) {
      // cout << c1 << "\\n";
      for (int i = 1; i <= n; i++) {
        cout << V[i] << " ";
      }
      return 0;
    }
    c1++;
  }
  for (int i = 1; i <= n; i++) {
    cout << V[i] << " ";
  }
  return 0;
}