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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MOD 998244353
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define rrep(i, a, b) for (ll i = a; i > b; --i)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795
#define fix(f, n) fixed << setprecision(n) << f
#define all(x) x.begin(), x.end()
#define endl "\\n"
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

ll newmod(ll a, ll b) { return ((a % b) + b) % b; }

ll powM(ll a, ll b, ll m) {
  a %= m;
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }

  return ans;
}

ll pow(ll a, ll b) {

  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a;
    a = a * a;
    b >>= 1;
  }

  return ans;
}

int main() {
  IOS;

  ll n, k;

  cin >> n >> k;

  vl a(n + 1);

  rep(i, 1, n + 1) cin >> a[i];

  ll flag = 0;

  while (!flag && k) {

    k--;
    vl temp(n + 2);
    for (ll i = 1; i <= n; i++) {
      ll left = max(i - a[i], 1LL);
      ll right = min(n, a[i] + i);
      temp[left]++;
      temp[right + 1]--;
    }

    ll cnt = 0;
    for (ll i = 1; i <= n; i++) {
      temp[i] += temp[i - 1];
      a[i] = temp[i];
      if (a[i] == n)
        cnt++;
    }

    if (cnt == n)
      flag = 1;
  }

  for (ll i = 1; i <= n; i++)
    cout << a[i] << " ";

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MOD 998244353
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define rrep(i, a, b) for (ll i = a; i > b; --i)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795
#define fix(f, n) fixed << setprecision(n) << f
#define all(x) x.begin(), x.end()
#define endl "\\n"
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

ll newmod(ll a, ll b) { return ((a % b) + b) % b; }

ll powM(ll a, ll b, ll m) {
  a %= m;
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }

  return ans;
}

ll pow(ll a, ll b) {

  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a;
    a = a * a;
    b >>= 1;
  }

  return ans;
}

int main() {
  IOS;

  ll n, k;

  cin >> n >> k;

  vl a(n + 1);

  rep(i, 1, n + 1) cin >> a[i];

  ll flag = 0;

  while (!flag && k) {

    k--;
    vl temp(n + 1);
    for (ll i = 1; i <= n; i++) {
      ll left = max(i - a[i], 1LL);
      ll right = min(n, a[i] + i);

      for (ll j = left; j <= right; j++) {
        temp[j]++;
      }
    }
    ll cnt = 0;
    for (ll i = 1; i <= n; i++) {
      a[i] = temp[i];
      if (a[i] == n)
        cnt++;
    }

    if (cnt == n)
      flag = 1;
  }

  for (ll i = 1; i <= n; i++)
    cout << a[i] << " ";

  return 0;
}