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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9 + 7)
#define INF (1LL << 60)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n) {
  ll s = sqrt(n);
  ll r = 0;
  for (ll i = 2; i <= s; i++) {
    if ((n % i) == 0) {
      r = 0;
      do {
        r++;
        n = n / i;
      } while ((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if (n > s) {
    prim.push_back({n, 1});
  }
}

void solve() {
  ll N, K;
  cin >> N >> K;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];

  rep(j, K) {
    vector<ll> im(N + 1, 0);
    rep(i, N) {
      ll st = max(i - a[i], 0LL);
      ll ed = min(i + a[i] + 1, N);
      im[st]++;
      im[ed]--;
    }
    vector<ll> b(N, 0);
    b[0] = im[0];
    rep1(i, N - 1) { b[i] = b[i - 1] + im[i]; }
    bool loop = false;
    rep(l, N) {
      if (a[l] != b[l]) {
        a[l] = b[l];
        loop = true;
      } else {
      }
    }
    if (!loop)
      break;
  }
  rep(i, N) { cout << a[i] << " "; }
  cout << endl;
}

int main(void) {
  // ll t; cin >> t; REP(i, t)
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9 + 7)
#define INF (1LL << 60)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n) {
  ll s = sqrt(n);
  ll r = 0;
  for (ll i = 2; i <= s; i++) {
    if ((n % i) == 0) {
      r = 0;
      do {
        r++;
        n = n / i;
      } while ((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if (n > s) {
    prim.push_back({n, 1});
  }
}

void solve() {
  ll N, K;
  cin >> N >> K;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];

  rep1(i, K) {
    vector<ll> b(N, 0);
    rep(j, N) {
      for (int l = j - a[j]; l <= j + a[j]; l++) {
        if (l >= 0 && l < N) {
          b[l]++;
        }
      }
    }
    bool ok = false;
    rep(j, N) {
      a[j] = b[j];
      if (b[j] != N)
        ok = true;
    }
    if (!ok)
      break;
  }
  rep(i, N) { cout << a[i] << " "; }
  cout << endl;
}

int main(void) {
  // ll t; cin >> t; REP(i, t)
  solve();
}