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

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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using ll = long long;

#include <bits/stdc++.h>

void solve(long long N, long long K, std::vector<long long> A) {

  for (int i = 0; i < K; i++) {
    vector<ll> B(N);
    for (int i = 0; i < N; i++) {
      int l = max(0LL, i - A[i]);
      int r = min(N - 1, i + A[i]);
      B[l]++;
      if (r + 1 < N)
        B[r + 1]--;
    }
    for (int i = 1; i < N; i++)
      B[i] += B[i - 1];
    if (A == B) {
      break;
    } else {
      A = B;
    }
  }
  for (int i = 00; i < N; i++) {
    cout << A[i] << " ";
  }
  cout << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, K, std::move(A));
  return 0;
}


Error: TLE

Fixed submission:

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

using ll = long long;

#include <bits/stdc++.h>

// https://onlinejudge.u-aizu.ac.jp/status/users/hamuhei4869/submissions/1/DSL_2_G/judge/4384116/C++11

template <typename T> class LazySegmentTree {
  //[l, r)半開区間を扱う
private:
  std::vector<T> node, lazy;
  std::function<T(T, T)> f; // 取得の演算
  std::function<T(T, T)> g; // 更新の演算
  std::function<T(T, int)> pow; // 更新の演算をk回作用させたときの結果

  T f_ide; // 演算fの単位元
  T g_ide; // 演算gの単位元

  int size;

public:
  LazySegmentTree(int n, std::function<T(T, T)> f, std::function<T(T, T)> g,
                  T f_ide, T g_ide)
      : f(f), g(g), f_ide(f_ide), g_ide(g_ide) {
    init(n);
    pow = [&](T a, int k) {
      T ans = g_ide;
      while (k > 0) {
        if (k & 1)
          ans = g(ans, a);
        a = g(a, a);
        k >>= 1;
      }
      return ans;
    };
  }
  LazySegmentTree(int n, std::function<T(T, T)> f, std::function<T(T, T)> g,
                  std::function<T(T, int)> pow, T f_ide, T g_ide)
      : f(f), g(g), pow(pow), f_ide(f_ide), g_ide(g_ide) {
    init(n);
  }
  void init(int n) {
    size = 1;
    while (size < n)
      size *= 2;
    node = std::vector<T>(2 * size - 1, f_ide);
    lazy = std::vector<T>(2 * size - 1, g_ide);
  }
  void eval(int k, int l, int r) {
    node[k] = g(node[k], pow(lazy[k], r - l));

    if (r - l >= 2) {
      lazy[2 * k + 1] = g(lazy[2 * k + 1], lazy[k]);
      lazy[2 * k + 2] = g(lazy[2 * k + 2], lazy[k]);
    }

    lazy[k] = g_ide;
  }
  void update(int a, int b, T x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = size;
    if (r <= a || b <= l)
      return;

    if (a <= l && r <= b) {
      lazy[k] = g(lazy[k], x);
      eval(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      eval(2 * k + 1, l, (l + r) / 2);
      eval(2 * k + 2, (l + r) / 2, r);

      node[k] = f(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  T get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = size;
    if (r <= a || b <= l)
      return f_ide;

    eval(k, l, r);

    if (a <= l && r <= b) {
      return node[k];
    } else {
      T result_left = get(a, b, 2 * k + 1, l, (l + r) / 2);
      T result_right = get(a, b, 2 * k + 2, (l + r) / 2, r);
      return f(result_left, result_right);
    }
  }
};

void solve(long long N, long long K, std::vector<long long> A) {
  std::function<long long(long long, long long)> f =
      [](long long a, long long b) { return a + b; };
  std::function<ll(ll, int)> pow = [](ll a, int b) { return a * b; };
  auto g = f;
  for (int i = 0; i < K; i++) {
    LazySegmentTree<long long> Seg(N, f, g, pow, 0, 0);
    /*
    for(int j = 00;j < N;j++){
        cout<<A[j]<<" ";
    }
    cout<<endl;
    */
    for (int i = 0; i < N; i++) {
      Seg.update(max(0LL, i - A[i]), min(N, i + A[i] + 1), 1);
    }
    bool w = true;
    vector<ll> u = A;
    for (int j = 0; j < N; j++) {
      A[j] = Seg.get(j, j + 1);
    }
    if (u == A) {
      break;
    }
  }
  for (int i = 00; i < N; i++) {
    cout << A[i] << " ";
  }
  cout << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, K, std::move(A));
  return 0;
}