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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ld long double
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

void print(Vl vec) {
  rep(i, 0, sz(vec)) {
    if (i)
      cout << " ";
    cout << vec[i];
  }
  coel;
}

////////////////////////////////////////////////////////

// デバッグを消し忘れるな！！！

int main() {
  ll n, k;
  cin >> n >> k;
  Vl a(n + 2);
  Rep(i, 1, n) cin >> a[i];

  // k >= n のときは全部nになる
  if (k >= n) {
    rep(i, 0, n) {
      if (i)
        cout << " ";
      cout << n;
    }
    coel;
    return 0;
  }

  // // 愚直にやったら計算量O(n*k) (n*k >= 10^10)
  Vl d(n + 2, 0);
  Vl e;
  do {
    e = a;
    rep(i, 0, n + 2) d[i] = 0;
    Rep(i, 1, n) {
      if (a[i] == 0) {
        continue;
      }
      ll l = max(1LL, i - a[i]), r = min(n, i + a[i]);
      d[l]++;
      d[r + 1]--;
    }
    Rep(i, 2, n) { d[i] += d[i - 1]; }
    Rep(i, 1, n) if (a[i] == 0) d[i]++;
    a = d;
    k--;
  } while (k != 0 && e != a);

  Rep(i, 1, n) cout << d[i] << " ";
  coel;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ld long double
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

void print(Vl vec) {
  rep(i, 0, sz(vec)) {
    if (i)
      cout << " ";
    cout << vec[i];
  }
  coel;
}

////////////////////////////////////////////////////////

// デバッグを消し忘れるな！！！

int main() {
  ll n, k;
  cin >> n >> k;
  Vl a(n + 2);
  Rep(i, 1, n) cin >> a[i];

  // k >= n のときは全部nになる
  if (k >= n) {
    rep(i, 0, n) {
      if (i)
        cout << " ";
      cout << n;
    }
    coel;
    return 0;
  }

  // // 愚直にやったら計算量O(n*k) (n*k >= 10^10)
  Vl d(n + 2, 0);
  while (k--) {
    rep(i, 0, n + 2) d[i] = 0;
    Rep(i, 1, n) {
      if (a[i] == 0) {
        continue;
      }
      ll l = max(1LL, i - a[i]), r = min(n, i + a[i]);
      d[l]++;
      d[r + 1]--;
    }
    Rep(i, 2, n) { d[i] += d[i - 1]; }
    Rep(i, 1, n) if (a[i] == 0) d[i]++;
    a = d;
  }

  Rep(i, 1, n) cout << d[i] << " ";
  coel;

  return 0;
}