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

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) (v).begin(), (v).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
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;
}
// snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vll a(n);
  ll all = 0;
  vll ans(n);
  ll total = 0;
  bool flag = false;
  rep(i, n) {
    cin >> a[i];
    total += min(a[i], n);
  }
  rep(i, k) {
    vll sta(n);
    vll fin(n);
    ll alli = 0;
    ll mi = 0;
    vector<pll> b;
    rep(j, n) {
      if (j - a[j] < 0 && a[j] + j > n - 1)
        alli++;
      else {
        sta[max((ll)0, j - a[j])]++;
        fin[min((ll)n - 1, j + a[j])]++;
      }
    }
    sort(all(b));
    ll count = 0;
    rep(j, n) {
      count += sta[j];
      ans[j] = count + alli;
      count -= fin[j];
    }
    a = ans;
    if (alli == n) {
      flag = true;
      break;
    }
  }
  if (flag) {
    rep(i, n - 1) cout << n << " ";
    cout << n << endl;
    return 0;
  }
  rep(i, n - 1) cout << ans[i] << " ";
  cout << a[n - 1] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) (v).begin(), (v).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
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;
}
// snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp

int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vll a(n);
  ll all = 0;
  vll ans(n);
  bool flag = false;
  rep(i, n) cin >> a[i];
  rep(i, k) {
    ll alli = 0;
    ll mi = 0;
    vector<pll> b;
    rep(j, n) {
      if (j - a[j] < 0 && a[j] + j > n - 1)
        alli++;
      else
        b.pb({max((ll)0, j - a[j]), min((ll)n - 1, j + a[j])});
    }
    sort(all(b));
    vll fin;
    ll j = 0;
    ll count = 0;
    rep(i, n) {
      while (true) {
        if (b.size() > j && b[j].first == i) {
          count++;
          fin.pb(b[j].second);
          sort(rall(fin));
          j++;
        } else
          break;
      }
      ans[i] = count + alli;
      while (true) {
        if (fin.size() != 0 && fin[fin.size() - 1] == i) {
          fin.pop_back();
          count--;
        } else
          break;
      }
    }
    a = ans;
    if (alli == n) {
      flag = true;
      break;
    }
  }
  ll plus = 0;
  if (flag) {
    rep(i, n - 1) cout << n << " ";
    cout << n << endl;
    return 0;
  }
  rep(i, n - 1) cout << ans[i] + plus << " ";
  cout << a[n - 1] + plus << endl;
}