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

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \\
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a) ? "Yes" : "No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
int main() {
  int n, k;
  cin >> n >> k;
  VVi a(2, Vi(n + 1, 0));
  Vi ans;
  rep(i, n) { cin >> a[0][i]; }
  int last = 0;
  int len = 0;
  rep(i, k) {
    len = 0;
    rep(j, n) { a[(i + 1) % 2][j] = 0; }
    rep(j, n) {
      //	clog << max(0, j - a[i % 2][j]) << " " << min(n, j + a[i % 2][j]
      //+ 1) << endl;
      a[(i + 1) % 2][max(0, j - a[i % 2][j])]++;
      a[(i + 1) % 2][min(n, j + a[i % 2][j] + 1)]--;
    }
    rep(j, n) {
      //	clog << a[(i + 1) % 2][j] <<" ";
      if (j != 0) {
        a[(i + 1) % 2][j] += a[(i + 1) % 2][j - 1];
      }
      if (n == a[(i + 1) % 2][j]) {
        len++;
      }
    }
    //	clog<<endl;
    //	clog<<len<<" "<<i<<endl;
    if (len == n or i == k - 1) {
      ans = a[(i + 1) % 2];
      break;
    }
  }
  rep(i, n) {
    cout << (ans[i]);
    if (i != n - 1) {
      cout << " ";
    }
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \\
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a) ? "Yes" : "No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
int main() {
  int n, k;
  int ans = 0;
  cin >> n >> k;
  ll bef;
  V a(n + 1, 0LL), cnt(n + 1, 0LL);
  rep(i, n) {
    cin >> bef;
    cnt[i] += bef;
    cnt[i + 1] -= bef;
  }
  rep(i, min(k, 1 + (int)log2(n))) {
    rep(I, n + 1) { a[I] = 0LL; }
    rep(j, n) {
      if (j != 0) {

        cnt[j] += cnt[j - 1];
      }
      clog << j - cnt[j] << " " << 1 + j + cnt[j] << endl;
      a[max(0LL, j - cnt[j])]++;
      a[min((ll)n, 1 + j + cnt[j])]--;
    }
    cnt = a;
    // clog << endl;
    // rep(I, n+1)
    //{

    //	cout << cnt[I] << " ";
    //
    //}
    //	clog << endl;
  }
  rep(j, n) {
    if (j != 0) {
      cnt[j] += cnt[j - 1];
    }
    cout << cnt[j];
    if (j != n - 1) {
      cout << " ";
    }
  }
  cout << endl;
}
/*
1
1 0 0 1 0
1 0] [0 1 0]
1 2 2 1 2
---------

0 2
1 2
2 3
2 5
4 5

1 1
1 1 1 1
1 1 1 1 1
0 0 1 1 1
0 0 1 1 1
---------
3 3 4 4 3



0 0 0 0 0 0
----------
1 1 1 1 1 1 ....1
---------------
2 3 3 3 3 3 3....3 2
------------
1 1 1
1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1 1
0 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1
0 0 0 1 1 1 1 1 1 1
....................
  ........      1 1 1
---

4 5 6 6 6 6 6 ...6 5 4
7 8 9 10 11 12 13 13 13 .... 12 11 10 9 8 7
14 15 16 17 18 19 20 21 22 23 24 25 26 27 27 27 ... 26 ~ 15 14

[0 1 2 4 7] i_n= i_(n-1)+1
[0 1 3 6 13 27...n] a_n=a_(n-1)*2 +1

0 0 0....0 0 0 1 0 0 0...0
0 0 0 ...0 0 1 1 1 0 0 ...0
0 0 0 ...0 1 2 3 2 1 0 ... 0
0 0 0 ...1 2 3 3 3 2 1 ... 0
0 0 0 .2 2 2 1 1
*/