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

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

#define endl '\\n'
#define ll long long
#define rint register int

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\\n';

using db = long double;
using pll = pair<ll, ll>;
#define scan(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];

#define rep(i, x, n, inc) for (i = x; i < n; i += inc)
#define repr(i, x, n, inc) for (i = x; i > n; i += inc)
#define all(a) (a).begin(), (a).end()
#define unique_sort(x)                                                         \\
  sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a, b) make_pair(a, b)
#define ff first
#define ss second
#define hell 1000000007
#define infl LLONG_MAX

#define Foxen(i, s) for (i = s.begin(); i != s.end(); i++)
#define Fill(s, v) memset(s, v, sizeof(s))
#define cout_p(x, p)                                                           \\
  cout << fixed << setprecision((p)) << x << endl // print with precision

#define tc(tt)                                                                 \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  for (ll _tt = 0; _tt < tt; _tt++) // testcase

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll t, i, x, j, y, z, k, n;
  cin >> n >> k;
  ll a[n], b[n];
  scan(a, n);

  rep(j, 0, min(51LL, k), 1) {
    memset(b, 0, sizeof(b));
    rep(i, 0, n, 1) {
      ll mi = max(0LL, i - a[i]), mx = min(n - 1, i + a[i]);
      b[mi]++;
      b[mx + 1]--;
    }
    bool br = a[0] == b[0];
    rep(i, 1, n, 1) {
      b[i] += b[i - 1];
      br = br && (a[i] == b[i]);
    }

    if (br)
      break;

    rep(i, 0, n, 1) a[i] = b[i];
  }

  rep(i, 0, n, 1) cout << a[i] << " ";
}


Error: TLE

Fixed submission:

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

#define endl '\\n'
#define ll long long
#define rint register int

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\\n';

using db = long double;
using pll = pair<ll, ll>;
#define scan(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];

#define rep(i, x, n, inc) for (i = x; i < n; i += inc)
#define repr(i, x, n, inc) for (i = x; i > n; i += inc)
#define all(a) (a).begin(), (a).end()
#define unique_sort(x)                                                         \\
  sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a, b) make_pair(a, b)
#define ff first
#define ss second
#define hell 1000000007
#define infl LLONG_MAX

#define Foxen(i, s) for (i = s.begin(); i != s.end(); i++)
#define Fill(s, v) memset(s, v, sizeof(s))
#define cout_p(x, p)                                                           \\
  cout << fixed << setprecision((p)) << x << endl // print with precision

#define tc(tt)                                                                 \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  for (ll _tt = 0; _tt < tt; _tt++) // testcase

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll t, i, x, j, y, z, k, n;
  cin >> n >> k;
  ll a[n], b[n];
  scan(a, n);

  rep(j, 0, k, 1) {
    memset(b, 0, sizeof(b));
    rep(i, 0, n, 1) {
      ll mi = max(0LL, i - a[i]), mx = min(n - 1, i + a[i]);
      b[mi]++;
      b[mx + 1]--;
    }
    bool br = a[0] == b[0];
    rep(i, 1, n, 1) {
      b[i] += b[i - 1];
      br = br && (a[i] == b[i]);
    }

    if (br)
      break;

    rep(i, 0, n, 1) a[i] = b[i];
  }

  rep(i, 0, n, 1) cout << a[i] << " ";
}