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

#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
ll N, K;
vll imos(vll A) {
  vll table(N + 1);
  for (ll i = 0; i < N; i++) {
    table[max(0LL, i - A[i])]++;
    table[min((ll)(N), i + A[i] + 1)]--;
  }

  //    cout<<table[0]<<" ";
  for (ll i = 1; i < N; i++) {
    table[i] += table[i - 1];
    //        cout<<table[i]<<" ";
  }
  //    cout<<endl;
  return table;
}
vll solve(function<vll(vll)> func, vll x) {
  if (K == 1) {
    return func(x);
  }
  if (K == 0) {
    return x;
  }
  if (K % 2 == 1) {
    K--;
    return func(solve(func, x));
  } else {
    auto func2 = [&func](vll t) { return func(func(t)); };
    K /= 2;
    return solve(func2, x);
  }
}
int main() {

  cin >> N >> K;
  vll A(N);
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
  }
  /*    for (size_t i = 0; i < N; i++)
      {
          A = imos(A);
      }
      */
  chmin(K, 50LL);
  auto ans = solve(imos, A);
  for (size_t i = 0; i < N; i++) {
    cout << ans[i] << " ";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
ll N, K;
vll imos(vll A) {
  vll table(N + 1);
  for (ll i = 0; i < N; i++) {
    table[max(0LL, i - A[i])]++;
    table[min((ll)(N), i + A[i] + 1)]--;
  }

  //    cout<<table[0]<<" ";
  for (ll i = 1; i < N; i++) {
    table[i] += table[i - 1];
    //        cout<<table[i]<<" ";
  }
  //    cout<<endl;
  return table;
}
vll solve(function<vll(vll)> func, vll x) {
  if (K == 1) {
    return func(x);
  }
  if (K == 0) {
    return x;
  }
  if (K % 2 == 1) {
    K--;
    return func(solve(func, x));
  } else {
    auto func2 = [&func](vll t) { return func(func(t)); };
    K /= 2;
    return solve(func2, x);
  }
}
int main() {

  cin >> N >> K;
  vll A(N);
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
  }
  /*    for (size_t i = 0; i < N; i++)
      {
          A = imos(A);
      }
      */
  auto ans = solve(imos, A);
  for (size_t i = 0; i < N; i++) {
    cout << ans[i] << " ";
  }

  return 0;
}