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

#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, (b), sizeof(a))
#define fore(i, a) for (int i = 0; i < a; i++)
#define fore1(i, j, a) for (int i = j; i < a; i++)
#define print(ar)                                                              \\
  for (int i = 0; i < ar.size(); i++)                                          \\
    cout << ar[i] << " ";
#define END cout << '\\n'
const double pi = acos(-1.0);

typedef pair<int, int> PII;

typedef vector<int> VI;

typedef vector<string> VS;

typedef vector<PII> VII;

typedef vector<VI> VVI;

typedef map<int, int> MPII;

typedef set<int> SETI;

typedef multiset<int> MSETI;

typedef long int li;

typedef unsigned long int uli;

typedef long long int ll;

typedef unsigned long long int ull;
ll fastexp(ll a, ll b, ll n) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % n;
    a = a * a % n;
    b >>= 1;
  }
  return res;
}
void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
bool check(VI arr) {
  fore1(i, 1, arr.size()) {
    if (arr[i] < arr.size() - 1)
      return false;
  }
  return true;
}
int main() {
  fast();
  int n, k;
  cin >> n >> k;

  VI arr(n + 1, 0);
  fore1(i, 1, n + 1) cin >> arr[i];
  while (k--) {
    if (check(arr) == false) {
      VI vec(n + 1, 0);
      fore1(i, 1, n + 1) {
        int a = (i - arr[i] >= 1) ? i - arr[i] : 1;
        int b = (i + arr[i] <= n) ? i + arr[i] : n;
        // cout<<a<<" "<<b<<endl;
        vec[a] += 1;
        if (b + 1 <= n)
          vec[b + 1] -= 1;
      }
      fore1(i, 2, n + 1) vec[i] += vec[i - 1];
      fore1(i, 1, n + 1) arr[i] = vec[i];
    } else {
      break;
    }
  }
  fore1(i, 1, n + 1) cout << arr[i] << " ";
  cout << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, (b), sizeof(a))
#define fore(i, a) for (int i = 0; i < a; i++)
#define fore1(i, j, a) for (int i = j; i < a; i++)
#define print(ar)                                                              \\
  for (int i = 0; i < ar.size(); i++)                                          \\
    cout << ar[i] << " ";
#define END cout << '\\n'
const double pi = acos(-1.0);

typedef pair<int, int> PII;

typedef vector<long long> VI;

typedef vector<string> VS;

typedef vector<PII> VII;

typedef vector<VI> VVI;

typedef map<int, int> MPII;

typedef set<int> SETI;

typedef multiset<int> MSETI;

typedef long int li;

typedef unsigned long int uli;

typedef long long int ll;

typedef unsigned long long int ull;
ll fastexp(ll a, ll b, ll n) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % n;
    a = a * a % n;
    b >>= 1;
  }
  return res;
}
void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  fast();
  int n, k;
  cin >> n >> k;

  VI arr(n + 1, 0);
  fore1(i, 1, n + 1) cin >> arr[i];
  while (k--) {
    VI vec(n + 1, 0);
    fore1(i, 1, n + 1) {
      int a = (i - arr[i] >= 1) ? i - arr[i] : 1;
      int b = (i + arr[i] <= n) ? i + arr[i] : n;
      // cout<<a<<" "<<b<<endl;
      vec[a] += 1;
      if (b + 1 <= n)
        vec[b + 1] -= 1;
    }
    fore1(i, 2, n + 1) vec[i] += vec[i - 1];
    fore1(i, 1, n + 1) arr[i] = vec[i];
  }
  fore1(i, 1, n + 1) cout << arr[i] << " ";
  cout << endl;
}