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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

class CLamps {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    REP(i, N) cin >> A[i];

    while (K--) {
      vector<int> V(N + 1);
      REP(i, N) {
        int val = A[i];
        int l = max(0LL, i - val);
        int r = min(N, i + val + 1);
        V[l] += 1;
        V[r] -= 1;
      }
      FOR(i, 1, N) { V[i] += V[i - 1]; }
      swap(A, V);

      bool finish = true;
      REP(i, N) {
        if (A[i] < N) {
          finish = false;
          break;
        }
      }
      if (finish)
        break;
    }
    REP(i, N) cout << A[i] << " ";
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);

  CLamps solver;
  solver.solve(in, out);
  return 0;
}


Error: TLE

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

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

public:
  LazySegmentTree(vector<int> 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, int 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];
    }
  }

  int 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];
    int vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};

class CLamps {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    REP(i, N) cin >> A[i];

    vector<int> V(N);
    LazySegmentTree seg(V);
    while (K--) {
      REP(i, N) {
        int val = A[i];
        int l = max(0LL, i - val);
        int r = min(N, i + val + 1);
        seg.add(l, r, 1);
      }
      REP(i, N) {
        A[i] = seg.getsum(i, i + 1);
        seg.add(i, i + 1, -A[i]);
      }

      bool finish = true;
      REP(i, N) {
        if (A[i] < N) {
          finish = false;
          break;
        }
      }
      if (finish)
        break;
    }
    REP(i, N) cout << A[i] << " ";
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  std::istream &in(std::cin);
  std::ostream &out(std::cout);

  CLamps solver;
  solver.solve(in, out);
  return 0;
}