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

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

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 n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

template <typename T> struct Doubling {
  const int LOG_X;
  bool flag1, flag2;
  vector<vector<T>> dp;

  Doubling(int n, ll x) : LOG_X(log2(x) + 1) { // n:配列のサイズ,x:遷移する回数
    flag1 = false;
    flag2 = false;
    dp.assign(LOG_X, vector<T>(n, -1));
  }

  void init(vector<T> &a) { // 適宜変える
    int n = a.size();
    flag1 = true;
    rep(i, n) { dp[0][i] = a[i]; }
  }

  void build() {
    assert(flag1);
    flag2 = true;
    for (int k = 0; k + 1 < LOG_X; ++k) {
      for (int i = 0; i < dp[0].size(); ++i) {
        if (dp[k][i] == -1) { // 2^k先の要素がないなら2^(k+1)先の要素もない
          dp[k + 1][i] = -1;
        } else { // 2^k*2^k=2^(k+1)
          dp[k + 1][i] = dp[k][dp[k][i] + 1];
        }
      }
    }
  }

  T query(int s, ll x) { // s:スタート地点,x:遷移回数
    assert(flag2);
    for (int i = LOG_X - 1; i >= 0; i--) {
      if (x & (1LL << i)) {
        s = dp[i][s] + 1;
      }
    }
    return s;
  }
};

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  if (n <= k) {
    rep(i, n) {
      if (i)
        cout << " ";
      cout << n;
    }
    return;
  }
  vector<vector<ll>> imos(2, vector<ll>(n + 5));
  int cnt = 0;
  rep(x, k) {
    rep(i, n) {
      imos[x % 2][max(0LL, i - a[i])]++;
      imos[x % 2][min(n, i + a[i] + 1)]--;
    }
    bool flag = x >= 1 ? true : false;
    rep(i, n) {
      imos[x % 2][i + 1] += imos[x % 2][i];
      a[i] = imos[x % 2][i];
      if ((x >= 1)) {
        if (imos[x % 2][i] != imos[(x - 1) % 2][i])
          flag = false;
      }
    }
    if (flag)
      break;
    else if (x >= 1) {
      rep(i, n) imos[(x - 1) % 2][i] = 0;
    }
    ++cnt;
  }
  rep(i, n) {
    if (i)
      cout << " ";
    cout << imos[(cnt - 1) % 2][i];
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

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 n, ll m) {
  ll tmp;
  while (m != 0) {
    tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

ll lcm(ll n, ll m) {
  return abs(n) / gcd(n, m) * abs(m); // gl=xy
}

using namespace std;

template <typename T> struct Doubling {
  const int LOG_X;
  bool flag1, flag2;
  vector<vector<T>> dp;

  Doubling(int n, ll x) : LOG_X(log2(x) + 1) { // n:配列のサイズ,x:遷移する回数
    flag1 = false;
    flag2 = false;
    dp.assign(LOG_X, vector<T>(n, -1));
  }

  void init(vector<T> &a) { // 適宜変える
    int n = a.size();
    flag1 = true;
    rep(i, n) { dp[0][i] = a[i]; }
  }

  void build() {
    assert(flag1);
    flag2 = true;
    for (int k = 0; k + 1 < LOG_X; ++k) {
      for (int i = 0; i < dp[0].size(); ++i) {
        if (dp[k][i] == -1) { // 2^k先の要素がないなら2^(k+1)先の要素もない
          dp[k + 1][i] = -1;
        } else { // 2^k*2^k=2^(k+1)
          dp[k + 1][i] = dp[k][dp[k][i] + 1];
        }
      }
    }
  }

  T query(int s, ll x) { // s:スタート地点,x:遷移回数
    assert(flag2);
    for (int i = LOG_X - 1; i >= 0; i--) {
      if (x & (1LL << i)) {
        s = dp[i][s] + 1;
      }
    }
    return s;
  }
};

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  if (n <= k) {
    rep(i, n) {
      if (i)
        cout << " ";
      cout << n;
    }
    return;
  }
  vector<ll> imos(n + 5);
  rep(x, k) {
    rep(i, n) {
      imos[max(0LL, i - a[i])]++;
      imos[min(n, i + a[i] + 1)]--;
    }
    rep(i, n) {
      imos[i + 1] += imos[i];
      a[i] = imos[i];
      if (x != k - 1)
        imos[i] = 0;
    }
  }
  rep(i, n) {
    if (i)
      cout << " ";
    cout << imos[i];
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}