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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pr pair<int, int>
#define mod 1000000007
#define endl "\\n"

int power(int a, int b, int m = mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  int res = power(a, b / 2, m);
  res = (res * res) % m;
  if (b & 1)
    res = (res * a) % m;
  return res;
}
int modinv(int a, int m = mod) { return power(a, m - 2, m); }
int add(int a, int b, int m = mod) {
  int c = (a % m + b % m);
  if (c >= m)
    c -= m;
  return c;
}
int sub(int a, int b, int m = mod) {
  int c = (a % m - b % m);
  if (c < 0)
    c += m;
  return c;
}
int mul(int a, int b, int m = mod) { return (a * b) % m; }
//	x<<1 => x*2		x>>1 => x/2;	cout<<flush;
//	cout<<fixed<<setprecision(10)<<ans<<endl;

void solve() {
  int n, k, cnt = 0, i;
  cin >> n >> k;
  int a[n + 1], b[n + 1];
  a[0] = 0;
  memset(b, 0, sizeof(b));
  for (i = 0; i < n; i++)
    cin >> a[i];
  while (cnt != n && k > 0) {
    cnt = 0;
    for (i = 0; i < n; i++) {
      int l = max(0ll, i - a[i]);
      int r = min(n, i + a[i] + 1);
      b[l]++;
      b[r]--;
    }
    for (i = 0; i < n; i++) {
      if (i)
        b[i] += b[i - 1];
      a[i] = b[i];
      if (a[i] == n)
        cnt++;
    }
    for (i = 0; i <= n; i++)
      b[i] = 0;
    k--;
  }
  for (i = 0; i < n; i++)
    cout << a[i] << " ";
  cout << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pr pair<int, int>
#define mod 1000000007
#define endl "\\n"

int power(int a, int b, int m = mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  int res = power(a, b / 2, m);
  res = (res * res) % m;
  if (b & 1)
    res = (res * a) % m;
  return res;
}
int modinv(int a, int m = mod) { return power(a, m - 2, m); }
int add(int a, int b, int m = mod) {
  int c = (a % m + b % m);
  if (c >= m)
    c -= m;
  return c;
}
int sub(int a, int b, int m = mod) {
  int c = (a % m - b % m);
  if (c < 0)
    c += m;
  return c;
}
int mul(int a, int b, int m = mod) { return (a * b) % m; }
//	x<<1 => x*2		x>>1 => x/2;	cout<<flush;
//	cout<<fixed<<setprecision(10)<<ans<<endl;

void solve() {
  int n, k, cnt = 0, i;
  cin >> n >> k;
  int a[n + 1], b[n + 1];
  a[0] = 0;
  memset(b, 0, sizeof(b));
  for (i = 0; i < n; i++)
    cin >> a[i];
  while (cnt != n && k > 0) {
    for (i = 0; i < n; i++) {
      int l = max(0ll, i - a[i]);
      int r = min(n, i + a[i] + 1);
      b[l]++;
      b[r]--;
    }
    for (i = 0; i < n; i++) {
      if (i)
        b[i] += b[i - 1];
      a[i] = b[i];
    }
    for (i = 0; i <= n; i++)
      b[i] = 0;
    k--;
  }
  for (i = 0; i < n; i++)
    cout << a[i] << " ";
  cout << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}