Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(&x[0], val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define e1 first
#define e2 second
#define newline putchar('\n')
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};

VI a;

int main() {
  int n, k;
  cin >> n >> k;
  a.resize(n);

  REP(i, n) { cin >> a[i]; }

  REP(i, k) {
    VI sum(n + 1);
    REP(j, n) {
      // cout<<max((int)j-a[j], 0)<<" "<<min((int)j+a[j]+1, n)<<endl;
      sum[max((int)j - a[j], 0)]++;
      sum[min((int)j + a[j] + 1, n)]--;
    }

    a[0] = sum[0];
    REP(j, n - 1) { a[j + 1] = a[j] + sum[j + 1]; }

    if (a[0] == k && a[n - 1] == k) {
      break;
    }
  }

  REP(i, n) {
    cout << a[i];
    if (i != n - 1)
      cout << " ";
  }
  newline;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(&x[0], val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define e1 first
#define e2 second
#define newline putchar('\n')
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};

VI a;

int main() {
  int n, k;
  cin >> n >> k;
  a.resize(n);

  REP(i, n) { cin >> a[i]; }

  REP(i, k) {
    VI sum(n + 1);
    REP(j, n) {
      // cout<<max((int)j-a[j], 0)<<" "<<min((int)j+a[j]+1, n)<<endl;
      sum[max((int)j - a[j], 0)]++;
      sum[min((int)j + a[j] + 1, n)]--;
    }

    a[0] = sum[0];
    REP(j, n - 1) { a[j + 1] = a[j] + sum[j + 1]; }

    if (a[0] == n && a[n - 1] == n) {
      break;
    }
  }

  REP(i, n) {
    cout << a[i];
    if (i != n - 1)
      cout << " ";
  }
  newline;

  return 0;
}