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

#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007, INF = (1LL << 60);
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  while (k--) {
    vector<ll> ret(n + 1);
    for (int i = 0; i < n; i++) {
      ll mi = max(0LL, i - a[i]);
      ll ma = min(n, i + a[i] + 1);
      ret[mi]++;
      ret[ma]--;
    }
    for (int i = 1; i <= n; i++) {
      ret[i] += ret[i - 1];
    }
    bool maki = true;
    for (int i = 0; i < n; i++) {
      a[i] = ret[i];
      if (a[i] < n)
        maki = false;
    }
    if (maki) {
      for (int i = 0; i < n; i++) {
        a[i] = n;
      }
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007, INF = (1LL << 60);
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;

struct LazySegmentTree {
private:
  int n;
  vector<ll> node, lazy;

public:
  LazySegmentTree(vector<ll> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 2];
  }

  void eval(int k, int l, int r) {
    if (lazy[k] != 0) {
      node[k] += lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k] / 2;
        lazy[2 * k + 2] += lazy[k] / 2;
      }
      lazy[k] = 0;
    }
  }

  void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] += (r - l) * x;
      eval(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }

  ll getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return node[k];
    ll vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  while (k--) {
    LazySegmentTree seg(vector<ll>(n, 0));
    for (int i = 0; i < n; i++) {
      ll mi = max(0LL, i - a[i]);
      ll ma = min(n, i + a[i] + 1);
      seg.add(mi, ma, 1LL);
    }
    bool maki = true;
    for (int i = 0; i < n; i++) {
      a[i] = seg.getsum(i, i + 1);
      if (a[i] < n)
        maki = false;
    }
    if (maki) {
      for (int i = 0; i < n; i++) {
        a[i] = n;
      }
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
  return 0;
}