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

#include <bits/stdc++.h>
#define endl "\\n"
#define pb push_back
#define ll long long
#define d1(x) cerr << #x << "--> " << x << endl
#define d2(x, y) cerr << #x << "--> " << x << " | " << #y << "--> " << y << endl
#define d3(x, y, z)                                                            \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << endl
#define d4(x, y, z, w)                                                         \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << " | " << #w << "--> " << w << endl
#define vpll vector<pair<ll, ll>>
#define F first
#define S second
#define T                                                                      \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  while (tt--)
using namespace std;
ll mode = 1e9 + 7;
const ll maxn = 5 * 1e5 + 10;
const ll inf = 99999999999;
ll __lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll getRand(ll l, ll r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.F << ", " << a.S << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &v) {
  out << "[";
  for (int i = 0; i < v.size(); i++) {
    if (i)
      out << ", ";
    out << v[i];
  }
  return out << "]";
}

void solve() {

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < k; i++) {

    vector<ll> pre(n + 1, 0);
    for (int j = 1; j <= n; j++) {
      ll x = max(1ll, j - a[j]);
      ll y = min(n, j + a[j]);
      pre[x]++;
      if ((y + 1) <= n)
        pre[y + 1]--;
    }
    for (int j = 1; j <= n; j++) {
      pre[j] += pre[j - 1];
    }

    ll mn = n;
    for (int j = 1; j <= n; j++) {
      a[j] = pre[j];
      mn = min(mn, a[j]);
    }
    if (mn == n) {
      for (int j = 1; j <= n; j++)
        cout << a[j] << " ";
      return;
    }
  }

  for (int j = 1; j <= n; j++) {
    cout << a[j] << " ";
  }

  return;
}

int main() {

#ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
#endif
  srand(time(0));

  ios::sync_with_stdio(0);
  cin.tie(0);

  // T{ solve(); }
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
#define pb push_back
#define ll long long
#define d1(x) cerr << #x << "--> " << x << endl
#define d2(x, y) cerr << #x << "--> " << x << " | " << #y << "--> " << y << endl
#define d3(x, y, z)                                                            \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << endl
#define d4(x, y, z, w)                                                         \\
  cerr << #x << "--> " << x << " | " << #y << "--> " << y << " | " << #z       \\
       << "--> " << z << " | " << #w << "--> " << w << endl
#define vpll vector<pair<ll, ll>>
#define F first
#define S second
#define T                                                                      \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  while (tt--)
using namespace std;
ll mode = 1e9 + 7;
const ll maxn = 5 * 1e5 + 10;
const ll inf = 99999999999;
ll __lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll getRand(ll l, ll r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.F << ", " << a.S << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &v) {
  out << "[";
  for (int i = 0; i < v.size(); i++) {
    if (i)
      out << ", ";
    out << v[i];
  }
  return out << "]";
}

void solve() {

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < k; i++) {

    vector<ll> pre(n + 1, 0);
    for (int j = 1; j <= n; j++) {
      ll x = max(1ll, j - a[j]);
      ll y = min(n, j + a[j]);
      pre[x]++;
      if ((y + 1) <= n)
        pre[y + 1]--;
    }
    for (int j = 1; j <= n; j++) {
      pre[j] += pre[j - 1];
    }

    for (int j = 1; j <= n; j++) {
      a[j] = pre[j];
    }

    //		bool ok=true;
    //		for (int j=1;j<=n;j++){
    //			if ((j-a[j])<=1 && (j+a[j])>=n){
    //
    //			}
    //			else ok=false;
    //		}
    //		if (ok){
    //			for (int j=1;j<=n;j++){
    //				a[j]+=(k-i)*n;
    //			}
    //		return ;
    //
    //		}
  }

  for (int j = 1; j <= n; j++) {
    cout << a[j] << " ";
  }

  return;
}

int main() {

#ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
#endif
  srand(time(0));

  ios::sync_with_stdio(0);
  cin.tie(0);

  // T{ solve(); }
  solve();

  return 0;
}