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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i <= (n); i++)
#define zep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \\
  for (int i = (m); i <= n; i++) {                                             \\
    cout << (x[i]) << " ";                                                     \\
  }                                                                            \\
  cout << endl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;
  vector<int> a(n + 2);
  rep(i, 1, n) cin >> a[i];

  zep(j, 0, k) {
    vector<int> b(n + 2, 0);

    rep(i, 1, n) {
      b[max(1LL, i - a[i])] += 1;
      b[min(n + 1, i + a[i] + 1)] -= 1;
    }

    bool fin = true;
    rep(i, 1, n) {
      b[i] += b[i - 1];
      if (b[i] != n)
        fin = false;
    }
    a.swap(b);

    if (fin)
      break;
  }
  printa(a, 1, n)

      return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i <= (n); i++)
#define zep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \\
  for (int i = (m); i <= n; i++) {                                             \\
    cout << (x[i]) << " ";                                                     \\
  }                                                                            \\
  cout << endl;

template <typename T> class segtree {
private:
  int n, sz, h;
  vector<T> node, lazy;
  void eval(int k) {
    if (lazy[k]) {
      node[k] += lazy[k];
      if (k < n) {
        lazy[k * 2] += lazy[k], lazy[k * 2 + 1] += lazy[k];
      }
      lazy[k] = 0;
    }
  }

public:
  segtree(const vector<T> &v) : n(1), sz((int)v.size()), h(0) {
    while (n < sz)
      n *= 2, h++;
    node.resize(2 * n, numeric_limits<T>::max());
    lazy.resize(2 * n, 0);
    for (int i = 0; i < sz; i++)
      node[i + n] = v[i];
    for (int i = n - 1; i >= 1; i--)
      node[i] = min(node[2 * i], node[2 * i + 1]);
  }
  void range(int a, int b, T x, int k = 1, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k);
    if (b <= l || r <= a) {
      return;
    }
    if (a <= l && r <= b) {
      lazy[k] += x;
      eval(k);
    } else {
      range(a, b, x, 2 * k, l, (l + r) / 2);
      range(a, b, x, 2 * k + 1, (l + r) / 2, r);
      node[k] = min(node[2 * k], node[2 * k + 1]);
    }
  }
  T query(int a, int b) {
    a += n, b += n - 1;
    for (int i = h; i > 0; i--)
      eval(a >> i), eval(b >> i);
    b++;
    T res1 = numeric_limits<T>::max(), res2 = numeric_limits<T>::max();
    while (a < b) {
      if (a & 1)
        eval(a), res1 = min(res1, node[a++]);
      if (b & 1)
        eval(--b), res2 = min(res2, node[b]);
      a >>= 1, b >>= 1;
    }
    return min(res1, res2);
  }
  void _print() {
    for (int i = 0; i < sz; i++)
      cout << query(i, i + 1) << " ";
    cout << endl;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 2);
  rep(i, 1, n) cin >> a[i];

  zep(j, 0, k) {
    vector<ll> b(n + 2, 0);

    rep(i, 1, n) {
      b[max(1LL, i - a[i])] += 1;
      b[min(n + 1, i + a[i] + 1)] -= 1;
    }

    rep(i, 1, n) b[i] += b[i - 1];

    a.swap(b);
    bool fin = true;
    zep(i, 0, n) {
      if (a[i] != n) {
        fin = false;
        break;
      }
    }

    if (fin)
      break;
  }
  printa(a, 1, n)

      return 0;
}