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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\\n';

const int mx = 100010;
const ll mod = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1, 0);
  rep(i, n) { cin >> a[i]; }

  rep(_, min(k, 41)) {
    vector<int> r(n + 1, 0);
    rep(i, n) {
      r[max(0, i - a[i])]++;
      r[min(n, i + a[i] + 1)]--;
    }
    rep(i, n) { r[i + 1] += r[i]; }
    swap(a, r);
  }
  rep(i, n) { cout << a[i] << " "; }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\\n';

const int mx = 100010;
const ll mod = 1e9 + 7;

template <typename F, typename T, typename Func_mf, typename Func_op,
          typename Func_mv>
struct LazySegtree {
  ll n, n_org;
  vector<T> dat;
  vector<F> laz;
  Func_mf merge_functions; // lazyとlazyの演算
  Func_op operate;         // dataとlazyの演算
  Func_mv merge_values;    // dataとdataの演算
  F fe;                    // lazyの単位元かつ初期値
  T te;                    // dataの単位元かつ初期値

  LazySegtree() {}
  LazySegtree(ll n_org, Func_mf merge_functions, Func_op operate,
              Func_mv merge_values, F fe, T te)
      : n_org(n_org), merge_functions(merge_functions), operate(operate),
        merge_values(merge_values), fe(fe), te(te) {
    n = 1;
    while (n < n_org)
      n <<= 1;
    dat.resize(2 * n - 1, te);
    laz.resize(2 * n - 1, fe);
  }

  void build(vector<T> &A) {
    for (ll k = 0; k < ll(A.size()); k++)
      dat[k + n - 1] = A[k];
    for (ll k = n - 2; k >= 0; k--)
      dat[k] = merge_values(dat[2 * k + 1], dat[2 * k + 2]);
  }
  void update(ll a, ll b, F x) { //[a,b)をupdate     b]:b+1
    update(a, b, x, 0, 0, n);
  }
  T get(ll a, ll b) { //[a,b)から(最小値とか和とかを)get  b]:b+1
    return query(a, b, 0, 0, n);
  }

private:
  void eval(ll k, ll w) {
    if (laz[k] == fe)
      return;
    operate(dat[k], laz[k], w);
    if (k < n - 1) {
      merge_functions(laz[2 * k + 1], laz[k]);
      merge_functions(laz[2 * k + 2], laz[k]);
    }
    laz[k] = fe;
  }
  T query(ll a, ll b, ll k, ll lb, ll rb) {
    eval(k, rb - lb);
    if (rb <= a || b <= lb)
      return te;
    if (a <= lb && rb <= b)
      return dat[k];
    ll mb = (lb + rb) >> 1;
    T vl = query(a, b, 2 * k + 1, lb, mb);
    T vr = query(a, b, 2 * k + 2, mb, rb);
    return merge_values(vl, vr);
  }
  void update(ll a, ll b, F x, ll k, ll lb, ll rb) {
    eval(k, rb - lb);
    if (b <= lb || rb <= a)
      return;
    if (a <= lb && rb <= b) {
      merge_functions(laz[k], x);
      eval(k, rb - lb);
    } else {
      ll mb = (lb + rb) >> 1;
      update(a, b, x, 2 * k + 1, lb, mb);
      update(a, b, x, 2 * k + 2, mb, rb);
      dat[k] = merge_values(dat[2 * k + 1], dat[2 * k + 2]);
    }
  }
};
auto make_segtree = [](ll N) { // auto make_segtree = [&](ll N)
  // 区間加算と区間最小
  /*
  using F = ll;
  using T = ll;
  auto merge_functions = [](F& f, F& g){f += g;};
  auto operate = [](T& v, F& f, ll w){v += f;};
  auto merge_values = [](T& a, T& b){return min(a, b);};
  F fe = 0;
  T te = inf;
  */
  // 区間加算と区間最大
  /*
  using F = ll;
  using T = ll;
  auto merge_functions = [](F& f, F& g){f += g;};
  auto operate = [](T& v, F& f, ll w){v += f;};
  auto merge_values = [](T& a, T& b){return max(a, b);};
  F fe = 0;
  T te = -inf;*/

  // 区間加算と区間和

  using F = ll;
  using T = ll;
  auto merge_functions = [](F &f, F &g) { f += g; };
  auto operate = [](T &v, F &f, ll w) { v += f * w; };
  auto merge_values = [](T &a, T &b) { return a + b; };
  F fe = 0;
  T te = 0;

  // 区間更新と区間最小
  /*
  using F=ll;
  using T=ll;
  auto merge_functions=[](F& f,F& g){f=g;};
  auto operate = [](T& v, F& f, ll w){v=f;};
  auto merge_values = [](T& a, T& b){return min(a, b);};
  F fe = inf;
  T te = inf;*/

  // 区間更新と区間最大
  /*
  using F=ll;
  using T=ll;
  auto merge_functions=[](F& f,F& g){f=g;};
  auto operate = [](T& v, F& f, ll w){v=f;};
  auto merge_values = [](T& a, T& b){return max(a, b);};
  F fe = -inf;
  T te = -inf;*/

  // 区間更新と区間和
  /*
  using F=ll;
  using T=ll;
  auto merge_functions=[](F& f,F& g){f=g;};
  auto operate = [](T& v, F& f, ll w){v=f*w;};
  auto merge_values = [](T& a, T& b){return a+b;};
  F fe = -inf;
  T te = 0;
  */
  return LazySegtree<F, T, decltype(merge_functions), decltype(operate),
                     decltype(merge_values)>(N, merge_functions, operate,
                                             merge_values, fe, te);
};
/*
auto st = make_segtree(n);
vector<ll> init(n);
st.build(init);
 */

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  auto st = make_segtree(n);
  st.build(a);
  auto st2 = make_segtree(n);
  vector<ll> b(n, 0);
  st2.build(b);

  vector<ll> out(n, -1);
  rep(i, k) {
    if (i % 2 == 0) {
      rep(j, n) {
        ll br = st.get(j, j + 1);
        st2.update(j - br, j + br + 1, 1);
      }
      if (st2.get(0, n) >= n * n) {
        // cout << st2.get(0,n) << endl;
        rep(p, n) { out[p] = st2.get(p, p + 1); }
        break;
      }
      vector<ll> c(n, 0);
      st.build(c);
    } else {
      rep(j, n) {
        ll br = st2.get(j, j + 1);
        st.update(j - br, j + br + 1, 1);
      }
      if (st.get(0, n) >= n * n) {
        // cout << st.get(0,n) << endl;
        rep(p, n) { out[p] = st.get(p, p + 1); }
        break;
      }
      vector<ll> c(n, 0);
      st2.build(c);
    }
  }
  if (out[0] == -1) {
    rep(p, n) { out[p] = (k % 2 ? st2.get(p, p + 1) : st.get(p, p + 1)); }
  }

  rep(i, n) { cout << out[i] << " "; }
  cout << ln;
  return 0;
}