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

#include <bits/stdc++.h>
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
#define e_b emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
using namespace std;
using ll = long long;
using ld = long double;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);

  for (int i = 0; i < K; i++) {
    vector<int> imos(N + 1, 0);
    for (int j = 0; j < N; j++) {
      int l = max(0, j - A.at(j));
      int r = min(N - 1, j + A.at(j));
      imos.at(l)++;
      imos.at(r + 1)--;
    }
    for (int j = 1; j < N + 1; j++)
      imos.at(j) += imos.at(j - 1);
    for (int j = 0; j < N; j++)
      A.at(j) = imos.at(j);

    bool flag = true;
    for (int j = 0; j < N; j++) {
      if (A.at(j) < N)
        flag = false;
    }
    if (flag)
      break;
  }

  for (int i = 0; i < N; i++)
    cout << A.at(i) << " ";
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
#define e_b emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
using namespace std;
using ll = long long;
using ld = long double;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

    for (int i = 0; i < SZ; i++)
      node.at(i + N - 1) = v.at(i);
    for (int i = N - 2; i >= 0; i--)
      node.at(i) = min(node.at(2 * i + 1), node.at(2 * i + 2));
  }

  void eval(int k, int l, int r) {
    if (lazy.at(k) == 0)
      return;
    node.at(k) += lazy.at(k);
    if (r - l > 1) {
      lazy.at(2 * k + 1) += lazy.at(k);
      lazy.at(2 * k + 2) += lazy.at(k);
    }
    lazy.at(k) = 0;
  }

  void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r == -1)
      r = N;
    eval(k, l, r);
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      lazy.at(k) += 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.at(k) = min(node.at(2 * k + 1), node.at(2 * k + 2));
    }
  }

  ll getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r == -1)
      r = N;
    if (r <= a || b <= l)
      return 1e12;
    eval(k, l, r);
    if (a <= l && r <= b)
      return node.at(k);
    ll vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);

  LazySegmentTree seg_pre(A);
  LazySegmentTree seg_zero(vector<ll>(N, 0));
  LazySegmentTree seg(vector<ll>(N, 0));
  for (int i = 0; i < K; i++) {
    seg.node = seg_zero.node;
    seg.lazy = seg_zero.lazy;
    for (int j = 0; j < N; j++) {
      int now = seg_pre.getmin(j, j + 1);
      seg.add(max(0, j - now), min(N + 1, j + now + 1), 1);
    }

    if (seg.getmin(0, N + 1) == N) {
      for (int j = 0; j < N; j++)
        cout << N << " ";
      cout << endl;
      return 0;
    }
    seg_pre.node = seg.node;
    seg_pre.lazy = seg.lazy;
  }

  for (int i = 0; i < N; i++)
    cout << seg_pre.getmin(i, i + 1) << " ";
  cout << endl;
  return 0;
}