alexjercan/bugnet huggingface dataset:
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < k; i++) {
    vector<int> nxt(n, 0);
    for (int j = 0; j < n; j++) {
      int l = max(0, j - a[j]);
      int r = min(n - 1, j + a[j]);
      nxt[l]++;
      if (r < n - 1) {
        nxt[r + 1]--;
      }
    }
    for (int j = 1; j < n; j++) {
      nxt[j] += nxt[j - 1];
    }

    if (a == nxt)
      break;
    else
      a = nxt;
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << (i == n - 1 ? "\\n" : " ");
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> nxt;
  for (int i = 0; i < k; i++) {
    nxt = a;
    for (int j = 0; j < n; j++) {
      int l = max(0, j - a[j]);
      int r = min(n - 1, j + a[j]);
      nxt[l]++;
      if (r < n - 1) {
        nxt[r + 1]--;
      }
    }
    for (int j = 1; j < n; j++) {
      nxt[j] += nxt[j - 1];
    }

    if (a == nxt)
      break;
    else
      a = nxt;
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << (i == n - 1 ? "\\n" : " ");
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];

  while (0 < k--) {
    vector<ll> work = a;
    a = vector<ll>(n, 0);
    REP(i, n) {
      ll st = max(0LL, i - work[i]);
      ll ed = min(i + work[i], n) + 1;
      a[st] += 1;
      if (ed < n)
        a[ed] -= 1;
    }
    bool flag = true;
    REP(i, n) {
      if (i < n - 1)
        a[i + 1] += a[i];
      if (a[i] < n)
        flag = false;
    }
    if (flag)
      break;
  }
  REP(i, n) cout << a[i] << ' ';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n, 0);
  REP(i, n) {
    ll x;
    cin >> x;
    ll st = max(i - x, 0LL);
    ll ed = min(i + x, n);
    a[st] += 1;
    if (ed < n - 1)
      a[ed + 1] -= 1;
  }
  while (1) {
    REP(i, n - 1) a[i + 1] += a[i];
    if (--k <= 0)
      break;
    vector<ll> work = a;
    a = vector<ll>(n, 0);
    REP(i, n) {
      ll st = max(i - work[i], 0LL);
      ll ed = min(i + work[i], n);
      a[st] += 1;
      if (ed < n - 1)
        a[ed + 1] -= 1;
    }
  }

  REP(i, n) cout << a[i] << ' ';
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, K;
  vector<int> A;

  cin >> N >> K;
  A.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  for (int k = 0; (k < K) && (k < 45); ++k) {
    vector<int> B(N, 0);

    // B[i] に i+1 を照らす電球の数をカウント
    for (int i = 0; i < N; ++i) {
      int first = (i >= A[i]) ? (i - A[i]) : 0;
      B[first]++;

      int last = (i + A[i] + 1);
      if ((i + A[i] + 1) < N) {
        B[last]--;
      }
    }

    for (int i = 1; i < N; ++i) {
      B[i] += B[i - 1];
    }

    A = B;
  }

  // 結果の出力
  for (size_t i = 0; i < N; ++i) {
    cout << A[i] << " ";
  }
  cout << endl;

  // cout <<

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  size_t N, K;
  vector<size_t> A;

  cin >> N >> K;
  A.resize(N);
  for (size_t i = 0; i < N; ++i) {
    cin >> A[i];
  }

  vector<size_t> B(N, 0);
  for (size_t k = 0; (k < K) && (k < 45); ++k) {
    for (size_t i = 0; i < N; ++i) {
      B[i] = 0;
    }; // 初期化

    // B[i] に i+1 を照らす電球の数をカウント
    for (size_t i = 0; i < N; ++i) {
      size_t first = (i >= A[i]) ? (i - A[i]) : 0;
      size_t last = (i + A[i] < N) ? (i + A[i]) : N;

      for (size_t j = first; j <= last; ++j) {
        B[j]++;
      }
    }

    A = B;
  }

  // 結果の出力
  for (size_t i = 0; i < N; ++i) {
    cout << A[i] << " ";
  }
  cout << endl;

  // cout <<

  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  rep(i, min(k, 1000)) {
    vector<int> x(n);
    vector<int> y(n);
    rep(j, n) {
      x.at(max(0, j - a.at(j)))++;
      y.at(min(n - 1, j + a.at(j)))++;
      // cout << j << " " << a.at(i) << " " << min(n-1,j+a.at(j)) << endl;
    }
    rep(j, n - 1) {
      x.at(j + 1) = x.at(j) + x.at(j + 1);
      y.at(n - 2 - j) = y.at(n - 2 - j) + y.at(n - 2 - j + 1);
    }
    // rep(j,n) cout << x.at(j) << " ";
    // cout << endl;

    rep(j, n) { a.at(j) = x.at(j) + y.at(j) - n; }
  }
  rep(i, n) cout << a.at(i) << " ";
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  rep(i, k) {
    vector<int> x(n);
    vector<int> y(n);
    rep(j, n) {
      x.at(max(0, j - a.at(j)))++;
      y.at(min(n - 1, j + a.at(j)))++;
      // cout << j << " " << a.at(i) << " " << min(n-1,j+a.at(j)) << endl;
    }
    rep(j, n - 1) {
      x.at(j + 1) = x.at(j) + x.at(j + 1);
      y.at(n - 2 - j) = y.at(n - 2 - j) + y.at(n - 2 - j + 1);
    }
    rep(j, n) { a.at(j) = x.at(j) + y.at(j) - n; }
  }
  rep(i, n) cout << a.at(i) << " ";
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1000005], b[1000005], c[1000005], d[1000005];
int main() {
  ll i, ans1 = 1, ans2, w, t = 1, flag = 0, j = 1, n, k, l, r;
  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    b[i] = 0;
  }
  while (k--) {
    flag = 1;
    for (i = 1; i <= n; i++) {
      c[i] = a[i];
      d[i] = 0;
    }
    for (i = 1; i <= n; i++) {

      l = max(t, i - a[i]);
      r = min(n, i + a[i]);
      d[l] += 1;
      d[r + 1] -= 1;
    }
    for (i = 1; i <= n; i++) {
      b[i] = b[i - 1] + d[i];
    }
    for (i = 1; i <= n; i++) {
      a[i] = b[i];
      b[i] = 0;
    }
    /* for(i=1; i<=n; i++)
     {
         printf("%lld ",a[i]);
     }*/
    for (i = 1; i <= n; i++) {
      if (c[i] != a[i]) {
        flag = 0;
        break;
      }
    }
    // printf("k=%lld\\n",ans1++);
    if (flag == 1)
      break;
  }

  for (i = 1; i <= n; i++) {
    printf("%lld ", a[i]);
  }
  printf("\\n");
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1000005], b[1000005], c[1000005];
int main() {
  ll i, ans1 = 1, ans2, w, t = 1, flag = 0, j = 1, n, k;
  scanf("%lld %lld", &n, &k);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    b[i] = 0;
  }
  while (k--) {
    flag = 1;
    for (i = 1; i <= n; i++) {
      c[i] = a[i];
    }
    for (i = 1; i <= n; i++) {
      for (j = max(t, i - a[i]); j <= min(n, i + a[i]); j++) {
        b[j]++;
      }
    }
    for (i = 1; i <= n; i++) {
      a[i] = b[i];
      b[i] = 0;
    }
    /* for(i=1; i<=n; i++)
     {
         printf("%lld ",a[i]);
     }*/
    for (i = 1; i <= n; i++) {
      if (c[i] != a[i]) {
        flag = 0;
        break;
      }
    }
    // printf("k=%lld\\n",ans1++);
    if (flag == 1)
      break;
  }

  for (i = 1; i <= n; i++) {
    printf("%lld ", a[i]);
  }
  printf("\\n");
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int maxN = 200010;

int A[2][maxN];

int main() {
  int n, k, i, j, p;
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; ++i)
    scanf("%d", A[0] + i);
  for (p = 0; p < k; ++p) {
    int cur = p & 1, nxt = 1 - cur;
    for (i = 0; i < n; ++i)
      if (A[cur][i] != n)
        break;
    if (i == n)
      break;
    for (i = 0; i < n; ++i)
      A[nxt][i] = 0;
    for (i = 0; i < n; ++i) {
      if (i < A[cur][i])
        ++A[nxt][0];
      else
        ++A[nxt][i - A[cur][i]];
      if (i + A[cur][i] + 1 < n)
        --A[nxt][i + A[cur][i] + 1];
    }
    for (i = 0; i < n; ++i)
      A[nxt][i] += A[nxt][i - 1];
  }
  for (i = 0; i < n; ++i)
    printf("%d ", p < k ? n : A[k & 1][i]);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int maxN = 200010;

int A[2][maxN];

int main() {
  int n, m, k, i, j, p;
  scanf("%d%d", &n, &k);
  m = n + n;
  for (i = n; i < m; ++i)
    scanf("%d", A[0] + i);
  for (p = 0; p < k; ++p) {
    int cur = p & 1, nxt = 1 - cur;
    for (i = n; i < m; ++i)
      if (A[cur][i] != n)
        break;
    if (i == m)
      break;
    for (i = n; i < m; ++i)
      A[nxt][i] = 0;
    for (i = n; i < m; ++i) {
      ++A[nxt][i - A[cur][i]];
      --A[nxt][i + A[cur][i] + 1];
    }
    for (i = n; i < m; ++i)
      A[nxt][i] += A[nxt][i - 1];
  }
  for (i = n; i < m; ++i)
    printf("%d ", p < k ? n : A[k & 1][i]);
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef long int li;

long long min(long long a, long long b) {
  if (a < b)
    return a;
  else
    return b;
}

long long max(long long a, long long b) {
  if (a > b)
    return a;
  else
    return b;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<long long> in(n);
  for (auto &it : in)
    cin >> it;
  for (int i = 0; i < k; i++) {
    vector<long long> dp(n + 1);
    for (int j = 0; j < n; j++) {
      dp[max(0ll, j - in[j])] += 1;
      dp[min((long long)n, j + in[j] + 1)] -= 1;
    }
    bool b = 1;
    for (int j = 0; j < n; j++) {
      if (j != 0)
        dp[j] += dp[j - 1];
      in[j] = dp[j];
      if (in[j] != n)
        b = 0;
    }
    if (b == 1)
      break;
  }
  for (int i = 0; i < n; i++) {
    cout << in[i] << ' ';
  }
  cout << '\\n';
}

/*
    check for corner cases.
    When using log check for zeros.
    Use queues and stacks if they are helping.

    (___)
    . . )____________
   _|__/\\           |\\
  (o_o_) \\__________| \\
           /|     ||   )
          < |     ||
*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef long int li;

long long min(long long a, long long b) {
  if (a < b)
    return a;
  else
    return b;
}

long long max(long long a, long long b) {
  if (a > b)
    return a;
  else
    return b;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<long long> in(n);
  for (auto &it : in)
    cin >> it;
  for (int i = 0; i < k; i++) {
    vector<long long> dp(n + 1);
    for (int j = 0; j < n; j++) {
      dp[max(0ll, j - in[j])] += 1;
      dp[min((long long)n, j + in[j] + 1)] -= 1;
    }
    for (int j = 0; j < n; j++) {
      if (j != 0)
        dp[j] += dp[j - 1];
      in[j] = dp[j];
    }
  }
  for (int i = 0; i < n; i++) {
    cout << in[i] << ' ';
  }
  cout << '\\n';
}

/*
    check for corner cases.
    When using log check for zeros.
    Use queues and stacks if they are helping.

    (___)
    . . )____________
   _|__/\\           |\\
  (o_o_) \\__________| \\
           /|     ||   )
          < |     ||
*/
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> vec(n);
  vector<int> vecnew(n);
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
  }
  // アルゴリズムはO(logn)なので
  k = min(k, 41);
  for (int i = 0; i < k; i++) {
    vecnew.assign(n, 0);
    // 累積和のテクニック
    for (int j = 0; j < n; j++) {
      int s = max(0, j - vec[j]);
      int l = min(n - 1, j + vec[j]);
      vecnew[s]++;
      if (l + 1 < n) {
        vecnew[l + 1]--;
      }
    }
    for (int j = 1; j < n; j++) {
      vecnew[j] += vecnew[j - 1];
    }
    for (int j = 0; j < n; j++) {
      vec[j] = vecnew[j];
    }
  }
  for (int i = 0; i < n - 1; i++) {
    cout << vec[i] << " ";
  }
  cout << vec[n - 1] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> vec(n);
  vector<int> vec2(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
  }
  // アルゴリズムはO(logn)なので
  k = min(k, 41);
  for (int i = 0; i < k; i++) {
    // 累積和のテクニック
    for (int j = 0; j < n; j++) {
      int s = max(0, j - vec[j]);
      int l = min(n - 1, j + vec[j]);
      vec2[s]++;
      if (l + 1 < n) {
        vec2[l + 1]--;
      }
    }
    for (int i = 1; i < n; i++) {
      vec2[i] += vec2[i - 1];
      vec[i - 1] = vec2[i - 1];
    }
    vec[n - 1] = vec2[n - 1];
  }
  for (int i = 0; i < n - 1; i++) {
    cout << vec[i] << " ";
  }
  cout << vec[n - 1] << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)

const int mxN = 2e5 + 5;
int N, K, A[mxN];
int ft[mxN];

void u(int i, int x) {
  for (; i <= N; i += i & -i)
    ft[i] += x;
}
void update(int i, int j, int x) {
  u(i, x);
  u(j + 1, -x);
}
int query(int i) {
  int r = 0;
  for (; i; i -= i & -i)
    r += ft[i];
  return r;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> K;
  FOR(i, 1, N) {
    cin >> A[i];
    update(max(1, i - A[i]), min(N, i + A[i]), 1);
  }

  FOR(i, 1, K) {
    bool brk = 1;
    FOR(j, 1, N) {
      A[j] = query(j);
      brk &= A[j] == N;
    }
    if (brk)
      break;
    memset(ft, 0, sizeof ft);
    FOR(j, 1, N) { update(max(1, j - A[j]), min(N, j + A[j]), 1); }
  }
  FOR(i, 1, N) { cout << A[i] << ' '; }
  cout << '\\n';
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)

const int mxN = 1e5 + 5;
int N, K, A[mxN];
int ft[mxN];

void u(int i, int x) {
  for (; i <= N; i += i & -i)
    ft[i] += x;
}
void update(int i, int j, int x) {
  u(i, x);
  u(j + 1, -x);
}
int query(int i) {
  int r = 0;
  for (; i; i -= i & -i)
    r += ft[i];
  return r;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> K;
  FOR(i, 1, N) {
    cin >> A[i];
    update(max(1, i - A[i]), min(N, i + A[i]), 1);
  }

  FOR(i, 1, K) {
    bool brk = 1;
    FOR(j, 1, N) {
      A[j] = query(j);
      brk &= A[j] == N;
    }
    if (brk)
      break;
    memset(ft, 0, sizeof ft);
    FOR(j, 1, N) { update(max(1, j - A[j]), min(N, j + A[j]), 1); }
  }
  FOR(i, 1, N) { cout << A[i] << ' '; }
  cout << '\\n';
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  K = min(K, 100);
  int A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int k = 0; k < K; k++) {
    int B[N];
    fill(B, B + N, 0);
    for (int i = 0; i < N; i++) {
      int l = max(0, i - A[i]);
      int r = min(N, i + A[i] + 1);
      B[l]++;
      if (r < N)
        B[r]--;
    }
    for (int i = 1; i < N; i++) {
      B[i] += B[i - 1];
    }
    for (int i = 0; i < N; i++) {
      A[i] = B[i];
    }
  }
  for (int i = 0; i < N; i++) {
    cout << (i ? " " : "") << A[i];
  }
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

void test() {
  int N = 2 * 1e5;
  int A[N];
  fill(A, A + N, 0);
  for (int k = 0; k < 20; k++) {
    cout << "k = " << k + 1 << endl;
    int B[N];
    fill(B, B + N, 0);
    for (int i = 0; i < N; i++) {
      for (int j = max(0, i - A[i]); j < min(N, i + A[i] + 1); j++) {
        B[j]++;
      }
    }
    for (int i = 0; i < N; i++) {
      A[i] = B[i];
    }
    cout << A[0] << endl;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  K = min(K, 18);
  int A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int k = 0; k < K; k++) {
    int B[N];
    fill(B, B + N, 0);
    for (int i = 0; i < N; i++) {
      for (int j = max(0, i - A[i]); j < min(N, i + A[i] + 1); j++) {
        B[j]++;
      }
    }
    for (int i = 0; i < N; i++) {
      A[i] = B[i];
    }
  }
  for (int i = 0; i < N; i++) {
    cout << (i ? " " : "") << A[i];
  }
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define repi(i, start, end) for (int i = int(start); i < int(end); ++i)
#define rep(i, n) repi(i, 0, n)
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define v(T) vector<T>
#define vv(T) v(v(T))
#define vvv(T) v(v(v(T)))
typedef v(int) vi;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  rep(i, v.size()) os << v[i] << (i + 1 != int(v.size()) ? " " : "");
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}

v(int) update(v(int) X) {
  int n = X.size();
  v(int) A(n + 1, 0);
  rep(i, n) {
    A[max(0, i - X[i])]++;
    A[min(n, i + X[i] + 1)]--;
  }
  v(int) B(n, 0);
  B[0] = A[0];
  repi(i, 1, n) { B[i] = B[i - 1] + A[i]; }
  return B;
}

void Main() {
  int N, K;
  cin >> N >> K;
  v(int) A(N);
  cin >> A;

  rep(i, min(K, 100)) { A = update(A); }

  cout << A << "\\n";
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define repi(i, start, end) for (int i = int(start); i < int(end); ++i)
#define rep(i, n) repi(i, 0, n)
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define v(T) vector<T>
#define vv(T) v(v(T))
#define vvv(T) v(v(v(T)))
typedef v(int) vi;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  rep(i, v.size()) os << v[i] << (i + 1 != int(v.size()) ? " " : "");
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}

v(int) update(v(int) X) {
  int n = X.size();
  v(int) A(n, 0);
  rep(i, n) {
    A[max(0, i - X[i])]++;
    A[min(n, i + X[i] + 1)]--;
  }
  v(int) B(n, 0);
  B[0] = A[0];
  repi(i, 1, n) { B[i] = B[i - 1] + A[i]; }
  return B;
}

void Main() {
  int N, K;
  cin >> N >> K;
  v(int) A(N);
  cin >> A;

  rep(i, K) {
    bool flag = true;
    rep(j, N) {
      if (A[j] != N) {
        flag = false;
        break;
      }
      if (flag) {
        cout << A << "\\n";
        return;
      }
    }
    A = update(A);
  }

  cout << A << "\\n";
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ALL(obj) (obj).begin, (obj).end()

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);

  ll minA = N + 1;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  while (K > 0) {
    minA = N + 1;

    vector<ll> B(N, 0);
    for (int j = 0; j < N; j++) {
      if (j - A[j] < 0) {
        B[0] += 1;
      } else {
        B[j - A[j]] += 1;
      }

      if (j + A[j] < N - 1) {
        B[j + A[j] + 1] -= 1;
      }
    }

    A[0] = B[0];
    minA = min(minA, A[0]);
    for (int j = 1; j < N; j++) {
      A[j] = A[j - 1] + B[j];
      minA = min(minA, A[j]);
    }
    if (minA == N) {
      for (int j = 0; j < N; j++) {
        cout << N << " ";
      }
      return 0;
    }

    K -= 1;
  }

  for (int j = 0; j < N; j++) {
    cout << A[j] << " ";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ALL(obj) (obj).begin, (obj).end()

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);

  ll minA = N - 1;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
    minA = min(minA, A[i]);
  }

  while (K > 0) {
    if (minA == N) {
      for (int j = 0; j < N; j++) {
        cout << N << " ";
        return 0;
      }
    };

    vector<ll> B(N, 0);
    for (int j = 0; j < N; j++) {
      if (j - A[j] < 0) {
        B[0] += 1;
      } else {
        B[j - A[j]] += 1;
      }

      if (j + A[j] < N - 1) {
        B[j + A[j] + 1] -= 1;
      }
    }

    A[0] = B[0];
    minA = min(minA, A[0]);
    for (int j = 1; j < N; j++) {
      A[j] = A[j - 1] + B[j];
      minA = min(minA, A[j]);
    }

    K -= 1;
  }

  for (int j = 0; j < N; j++) {
    cout << A[j] << " ";
  }
}