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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define NMAX 1000 * 100 + 1

#define numberOfSetBits(S)                                                     \\
  __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x, y) memset(x, y, sizeof(x))
#define gcd(a, b) __gcd(a, b)
#define all(x) x.begin(), x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)                                                               \\
  for (auto it : x)                                                            \\
    cout << it << ' ';                                                         \\
  cout << endl;
#define printii(x)                                                             \\
  for (auto it : x)                                                            \\
    cout << it.F << ' ' << it.S << '\\t';                                       \\
  cout << endl;
#define in(x, n)                                                               \\
  for (int e = 0; e < n; e++) {                                                \\
    ll y;                                                                      \\
    cin >> y;                                                                  \\
    x.pb(y);                                                                   \\
  }

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll, ll>
#define pll pair<ll, ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii, ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a, b, c) mp(mp(a, b), c)

vi a, c, d;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, x, t, m, y, z, k;
  cin >> n >> k;
  in(a, n);
  c.assign(n, n);
  for (int i = 0; i < k; i++) {
    d.assign(n + 1, 0);
    if (a == c)
      break;
    for (int j = 0; j < n; j++) {
      d[max(0ll, j - a[j])]++;
      d[min(n - 1, j + a[j]) + 1]--;
    }
    for (int j = 1; j < n; j++) {
      d[j] += d[j - 1];
    }
    for (int j = 0; j < n; j++) {
      a[j] = d[j];
    }
  }
  print(a);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define NMAX 1000 * 100 + 1

#define numberOfSetBits(S)                                                     \\
  __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x, y) memset(x, y, sizeof(x))
#define gcd(a, b) __gcd(a, b)
#define all(x) x.begin(), x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x)                                                               \\
  for (auto it : x)                                                            \\
    cout << it << ' ';                                                         \\
  cout << endl;
#define printii(x)                                                             \\
  for (auto it : x)                                                            \\
    cout << it.F << ' ' << it.S << '\\t';                                       \\
  cout << endl;
#define in(x, n)                                                               \\
  for (int e = 0; e < n; e++) {                                                \\
    ll y;                                                                      \\
    cin >> y;                                                                  \\
    x.pb(y);                                                                   \\
  }

#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll, ll>
#define pll pair<ll, ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii, ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a, b, c) mp(mp(a, b), c)

struct LazySegmentTree {
  // Change these functions
  ll null = 0, lnull = 0, init = 0;
  ll combine(ll p, ll q) { return p + q; }
  ll getValue(ll v) {
    if (lazy[v] != lnull)
      return len[v] * lazy[v] + st[v];
    return st[v];
  }
  void modifyLazy(ll v, ll x) { lazy[v] += x; }

  // Constructors and utilities
  vi st, lazy, len;
  ll n;
  inline ll left(ll x) { return x << 1; }
  inline ll right(ll x) { return (x << 1) | 1; }
  inline ll mid(ll l, ll r) { return (l + r) / 2; }

  LazySegmentTree() {}
  LazySegmentTree(ll N) {
    n = N;
    st.assign(4 * n, 0);
    lazy.assign(4 * n, lnull);
    len.resize(4 * n);
    vi a(n, init);
    build(a, 1, 0, n - 1);
  }
  LazySegmentTree(vi &a) {
    n = a.size();
    st.assign(4 * n, 0);
    lazy.assign(4 * n, lnull);
    len.resize(4 * n);
    build(a, 1, 0, n - 1);
  }
  void build(vi &a, ll v, ll tl, ll tr) {
    len[v] = tr - tl + 1;
    if (tl == tr) {
      st[v] = a[tl];
      return;
    }
    ll tm = mid(tl, tr);
    build(a, left(v), tl, tm);
    build(a, right(v), tm + 1, tr);
    combine(v);
  }

  // Segtree functions
  void combine(ll v) { st[v] = combine(getValue(left(v)), getValue(right(v))); }
  void push(ll v) {
    if (lazy[v] == lnull)
      return;
    st[v] = getValue(v);
    modifyLazy(left(v), lazy[v]);
    modifyLazy(right(v), lazy[v]);
    lazy[v] = lnull;
  }

  void update(ll v, ll tl, ll tr, ll l, ll r, ll x) {
    if (l > r)
      return;
    if (tl == l && tr == r) {
      modifyLazy(v, x);
      return;
    }
    push(v);
    ll tm = mid(tl, tr);
    update(left(v), tl, tm, l, min(r, tm), x);
    update(right(v), tm + 1, tr, max(l, tm + 1), r, x);
    combine(v);
  }
  ll query(ll v, ll tl, ll tr, ll l, ll r) {
    if (l > r)
      return null;
    if (tl == l && tr == r)
      return getValue(v);
    push(v);
    ll tm = mid(tl, tr);
    ll resl = query(left(v), tl, tm, l, min(r, tm));
    ll resr = query(right(v), tm + 1, tr, max(l, tm + 1), r);
    return combine(resl, resr);
  }

  // UI
  void update(ll pos, ll x) { update(1, 0, n - 1, pos, pos, x); }
  void update(ll l, ll r, ll x) { update(1, 0, n - 1, l, r, x); }
  ll query(ll l, ll r) { return query(1, 0, n - 1, l, r); }
};

vi a, b, c;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, x, t, m, y, z, k;
  cin >> n >> k;
  in(a, n);
  c.assign(n, n);
  LazySegmentTree foo(n);
  b.resize(n);
  for (int i = 0; i < k; i++) {
    if (a == c)
      break;
    foo = LazySegmentTree(n);
    for (int j = 0; j < n; j++) {
      foo.update(max(0ll, j - a[j]), min(n - 1, j + a[j]), 1);
    }
    for (int j = 0; j < n; j++) {
      x = foo.query(j, j);
      b[j] = x;
    }
    a = b;
  }
  print(a);

  return 0;
}