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

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define REP(i, n) for (int i = 0; i < (n); i++)

template <typename T, typename E> struct SegmentTree {
  using G = function<T(T, E)>;
  using H = function<E(E, E)>;
  int n;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(){};
  SegmentTree(int n_, G g, H h, T ti, E ei) : g(g), h(h), ti(ti), ei(ei) {
    init(n_);
  }
  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.clear();
    dat.resize(n, ti);
    laz.clear();
    laz.resize(2 * n - 1, ei);
  }
  void build(int n_, vector<T> v) {
    for (int i = 0; i < n_; i++)
      dat[i] = v[i];
  }
  void update(int a, int b, E x, int k, int l, int r) {
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      laz[k] = h(laz[k], x);
      return;
    }
    update(a, b, x, k * 2 + 1, l, (l + r) / 2);
    update(a, b, x, k * 2 + 2, (l + r) / 2, r);
  }
  void update(int a, int b, E x) { update(a, b, x, 0, 0, n); }
  T query(int k) {
    T c = dat[k];
    k += n - 1;
    E x = laz[k];
    while (k > 0) {
      k = (k - 1) / 2;
      x = h(x, laz[k]);
    }
    return g(c, x);
  }
};

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N, K;
  cin >> N >> K;

  vector<int> b1(N + 2, 0);
  for (int i = 0; i < N; i++) {
    cin >> b1[i];
  }

  for (int k = 0; k < min(100LL, K); k++) {
    vector<int> b2(N + 2, 0);
    for (int i = 0; i < N; i++) {
      int l = max(0LL, i - b1[i]);
      int r = min(N - 1, i + b1[i]);
      b2[l]++;
      b2[r + 1]--;
    }
    for (int i = 1; i < N; i++) {
      b2[i] += b2[i - 1];
    }
    b1 = b2;
  }

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << b1[i];
  }
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define REP(i, n) for (int i = 0; i < (n); i++)

template <typename T, typename E> struct SegmentTree {
  using G = function<T(T, E)>;
  using H = function<E(E, E)>;
  int n;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(){};
  SegmentTree(int n_, G g, H h, T ti, E ei) : g(g), h(h), ti(ti), ei(ei) {
    init(n_);
  }
  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.clear();
    dat.resize(n, ti);
    laz.clear();
    laz.resize(2 * n - 1, ei);
  }
  void build(int n_, vector<T> v) {
    for (int i = 0; i < n_; i++)
      dat[i] = v[i];
  }
  void update(int a, int b, E x, int k, int l, int r) {
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      laz[k] = h(laz[k], x);
      return;
    }
    update(a, b, x, k * 2 + 1, l, (l + r) / 2);
    update(a, b, x, k * 2 + 2, (l + r) / 2, r);
  }
  void update(int a, int b, E x) { update(a, b, x, 0, 0, n); }
  T query(int k) {
    T c = dat[k];
    k += n - 1;
    E x = laz[k];
    while (k > 0) {
      k = (k - 1) / 2;
      x = h(x, laz[k]);
    }
    return g(c, x);
  }
};

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N, K;
  cin >> N >> K;

  SegmentTree<int, int> b1(
      N + 2, [](int a, int b) { return a + b; },
      [](int a, int b) { return a + b; }, 0, 0);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    b1.update(i, i + 1, a);
  }

  for (int k = 0; k < min(100LL, K); k++) {
    SegmentTree<int, int> b2(
        N + 2, [](int a, int b) { return a + b; },
        [](int a, int b) { return a + b; }, 0, 0);
    for (int i = 0; i < N; i++) {
      int l = max(0LL, i - b1.query(i));
      int r = min(N - 1, i + b1.query(i));
      b2.update(l, r + 1, 1);
    }
    b1 = b2;
  }

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << b1.query(i);
  }
  cout << endl;

  return 0;
}