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

// Remeber Choose Compiler: GCC9
#include <bits/stdc++.h>

using namespace std;

// Debug Output Utility
#ifdef LOCAL
#define dbg(args...)                                                           \\
  do {                                                                         \\
    cout << #args << " -> ";                                                   \\
    err(args);                                                                 \\
  } while (0)
void err() { std::cout << std::endl; }
template <typename T, typename... Args> void err(T a, Args... args) {
  std::cout << a << ' ';
  err(args...);
}
template <template <typename...> class T, typename t, typename... A>
void err(const T<t> &arg, const A &...args) {
  for (auto &v : arg)
    std::cout << v << ' ';
  err(args...);
}
#else
#define dbg(...)
#endif

// Macro Definitions:
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

// Type Alias:
using int64 = long long;
using PII = pair<int, int>;

// Global Const Variable
const int64 INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const int MAXN = 200005;

int64 n, k;
int64 arr[MAXN], tree[4 * MAXN], lazy[4 * MAXN];

void push_up(int rt) { tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]); }

void push_down(int rt, int len) {
  tree[rt << 1] += lazy[rt];
  lazy[rt << 1] += lazy[rt];
  tree[rt << 1 | 1] += lazy[rt];
  lazy[rt << 1 | 1] += lazy[rt];
  lazy[rt] = 0;
}
#define lchild rt << 1, l, m
#define rchild rt << 1 | 1, m + 1, r
void build(int rt = 1, int l = 1, int r = n) {
  if (l == r) {
    tree[rt] = arr[l];
    lazy[rt] = 0;
    return;
  }
  int m = (l + r) >> 1;
  build(lchild);
  build(rchild);
  push_up(rt);
}
void update(int L, int R, int64 delta, int rt = 1, int l = 1, int r = n) {
  if (L <= l && r <= R) {
    tree[rt] += delta;
    lazy[rt] += delta;
    return;
  }
  if (lazy[rt])
    push_down(rt, r - l + 1);
  int m = (l + r) >> 1;
  if (L <= m)
    update(L, R, delta, lchild);
  if (R > m)
    update(L, R, delta, rchild);
  push_up(rt);
}
int64 query(int L, int R, int rt = 1, int l = 1, int r = n) {
  if (L <= l && r <= R)
    return tree[rt];
  if (lazy[rt])
    push_down(rt, r - l + 1);
  int m = (l + r) >> 1;
  int64 ret = INF;
  if (L <= m)
    ret = min(ret, query(L, R, lchild));
  if (R > m)
    ret = min(ret, query(L, R, rchild));
  return ret;
}

void run() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  build();
  while (k--) {
    for (int i = 1; i <= n; i++) {
      int64 l = max(1ll, i - arr[i]), r = min(n, i + arr[i]);
      update(l, r, 1);
    }
    int64 low = INF;
    for (int i = 1; i <= n; i++) {
      arr[i] = query(i, i) - arr[i];
      low = min(low, arr[i]);
    }
    build();
    if (low >= n)
      break;
  }
  for (int i = 1; i <= n; i++) {
    cout << min(arr[i], n) << " ";
  }
  cout << endl;
}

// Debug Input Redirection
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  run();
}

Error: TLE

Fixed submission:

// Remeber Choose Compiler: GCC9
#include <bits/stdc++.h>

using namespace std;

// Debug Output Utility
#ifdef LOCAL
#define dbg(args...)                                                           \\
  do {                                                                         \\
    cout << #args << " -> ";                                                   \\
    err(args);                                                                 \\
  } while (0)
void err() { std::cout << std::endl; }
template <typename T, typename... Args> void err(T a, Args... args) {
  std::cout << a << ' ';
  err(args...);
}
template <template <typename...> class T, typename t, typename... A>
void err(const T<t> &arg, const A &...args) {
  for (auto &v : arg)
    std::cout << v << ' ';
  err(args...);
}
#else
#define dbg(...)
#endif

// Macro Definitions:
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

// Type Alias:
using int64 = long long;
using PII = pair<int, int>;

// Global Const Variable
const int64 INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const int MAXN = 200005;

int64 n, k;
int64 arr[MAXN], tree[4 * MAXN], lazy[4 * MAXN];

void push_up(int rt) { tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]); }

void push_down(int rt, int len) {
  tree[rt << 1] += lazy[rt];
  lazy[rt << 1] += lazy[rt];
  tree[rt << 1 | 1] += lazy[rt];
  lazy[rt << 1 | 1] += lazy[rt];
  lazy[rt] = 0;
}
#define lchild rt << 1, l, m
#define rchild rt << 1 | 1, m + 1, r
void build(int rt = 1, int l = 1, int r = n) {
  if (l == r) {
    tree[rt] = arr[l];
    return;
  }
  int m = (l + r) >> 1;
  build(lchild);
  build(rchild);
  push_up(rt);
}
void update(int L, int R, int64 delta, int rt = 1, int l = 1, int r = n) {
  if (L <= l && r <= R) {
    tree[rt] += delta;
    lazy[rt] += delta;
    return;
  }
  if (lazy[rt])
    push_down(rt, r - l + 1);
  int m = (l + r) >> 1;
  if (L <= m)
    update(L, R, delta, lchild);
  if (R > m)
    update(L, R, delta, rchild);
  push_up(rt);
}
int64 query(int L, int R, int rt = 1, int l = 1, int r = n) {
  if (L <= l && r <= R)
    return tree[rt];
  if (lazy[rt])
    push_down(rt, r - l + 1);
  int m = (l + r) >> 1;
  int64 ret = INF;
  if (L <= m)
    ret = min(ret, query(L, R, lchild));
  if (R > m)
    ret = min(ret, query(L, R, rchild));
  return ret;
}

void run() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  build();
  while (k--) {
    for (int i = 1; i <= n; i++) {
      int64 l = max(1ll, i - arr[i]), r = min(n, i + arr[i]);
      update(l, r, 1);
    }
    int64 low = INF;
    for (int i = 1; i <= n; i++) {
      int64 tmp = arr[i];
      arr[i] = query(i, i) - arr[i];
      update(i, i, -tmp);
      low = min(low, arr[i]);
    }
    if (low >= n)
      break;
  }
  for (int i = 1; i <= n; i++) {
    cout << min(arr[i], n) << " ";
  }
  cout << endl;
}

// Debug Input Redirection
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  run();
}