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

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

typedef long long ll;
int main() {

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

    bool br = true;
    for (int i = 1; i <= n; ++i) {
      if (a[i] < n) {
        br = false;
        break;
      }
    }
    if (br == true)
      break;

    for (int i = 1; i <= n; ++i) {
      s[max(1, i - a[i])]++;
      s[min(n + 1, i + a[i] + 1)]--;
    }
    int sum = 0;
    for (int i = 1; i <= n; ++i) {
      sum += s[i];
      a[i] = sum;
      s[i] = 0;
    }
  }
  for (int i = 1; i <= n; ++i) {
    cout << a[i] << ' ';
  }

  return 0;
}

Error: WA

Fixed submission:

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

typedef long long ll;
int main() {

  int n, k;
  cin >> n >> k;
  vector<int> a(n, 0);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  while (k--) {
    vector<int> s(n + 1, 0);
    bool br = true;
    for (int i = 1; i <= n; ++i) {
      if (a[i] < n) {
        br = false;
        break;
      }
    }
    if (br == true)
      break;

    for (int i = 1; i <= n; ++i) {
      s[max(1, i - a[i])]++;
      s[min(n + 1, i + a[i] + 1)]--;
    }
    int sum = 0;
    for (int i = 1; i <= n; ++i) {
      sum += s[i];
      a[i] = sum;
    }
  }
  for (int i = 1; i <= n; ++i) {
    cout << a[i] << ' ';
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
ll read() {
  ll f = 1, x = 0;
  char ch = getchar();
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch <= '9' && ch >= '0'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}
ll n, K, a[200055], dis[200055], sum, flag;
int main() {
  cin >> n >> K;
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (ll k = 1; k <= K; k++) {
    memset(dis, 0, sizeof(dis));
    sum = 0, flag = 1;
    for (ll i = 1; i <= n; i++) {
      ll x = max(i - a[i], (ll)1);
      ll y = min(i + a[i] + 1, n + 1);
      dis[x]++;
      dis[y]--;
    }
    for (ll i = 1; i <= n; i++) {
      sum = sum + dis[i];
      a[i] = sum;
      // printf("%lld ",a[i]);
      if (a[i] != n)
        flag = 0;
    }
    // printf("k=%lld\\n",k);
    if (flag)
      break;
  }
  for (int i = 1; i <= n; i++)
    printf("%lld ", a[i]);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
int read() {
  int f = 1, x = 0;
  char ch = getchar();
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch <= '9' && ch >= '0'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}
int n, K, a[200005], dis[200005];
int main() {
  cin >> n >> K;
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int k = 1; k <= K; k++) {
    memset(dis, 0, sizeof(dis));
    for (int i = 1; i <= n; i++) { // printf("i=%d\\n",i);
      for (int j = max(1, i - a[i]); j <= i + a[i]; j++) {
        dis[j]++;
      }
    }
    int flag = 1;
    for (int i = 1; i <= n; i++) {
      a[i] = dis[i];
      if (a[i] != n)
        flag = 0;
      // printf("%d ",a[i]);
    }
    // printf("k=%d\\n",k);
    if (flag)
      break;
  }
  for (int i = 1; i <= n; i++)
    printf("%d ", a[i]);

  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
#define int int64_t
int ar[(int)2e5 + 10];
int br[(int)2e5 + 10];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int count = 0;
  int n, k;
  cin >> n >> k;
  int mini = n;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
    mini = min(mini, ar[i]);
  }
  while (k > 0 and mini < n) {
    k--;
    count++;
    for (int i = 1; i <= n; i++) {
      int lw = max(i - ar[i], (int)1);
      int up = min(i + ar[i] + 1, n + 1);
      br[lw] += 1;
      br[up] -= 1;
    }
    mini = n;
    for (int i = 1; i <= n; i++) {
      br[i] += br[i - 1];
      ar[i] = br[i];
      mini = min(mini, ar[i]);
    }
    for (int i = 1; i <= n + 1; i++)
      br[i] = 0;
  }
  for (int i = 1; i <= n; i++)
    cout << ar[i] << " ";
  cout << "\\n";
  return 0;
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int int64_t
int ar[(int)2e5 + 10];
int br[(int)2e5 + 10];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int count = 0;
  int n, k;
  cin >> n >> k;
  int mini = n;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
    mini = min(mini, ar[i]);
  }
  while (k > 0 and mini < n) {
    k--;
    count++;
    for (int i = 1; i <= n; i++) {
      int lw = max(i - ar[i], (int)1);
      int up = min(i + ar[i] + 1, n + 1);
      br[lw] += 1;
      br[up] -= 1;
    }
    mini = n;
    for (int i = 1; i <= n; i++) {
      br[i] += br[i - 1];
      ar[i] = br[i];
      mini = min(mini, ar[i]);
    }
    for (int i = 1; i <= 10000; i++)
      br[i] = 0;
  }
  for (int i = 1; i <= n; i++)
    cout << ar[i] << " ";
  cout << "\\n";
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  vector<ll> imos(n + 1);

  rep(_, min(k, 100)) {
    imos.assign(n + 1, 0);
    rep(i, n) {
      if (i - a[i] <= 0)
        imos[0]++;
      else
        imos[i - a[i]]++;

      if (i + a[i] + 1 <= n)
        imos[i + a[i] + 1]--;
      else
        imos[n]--;
    }

    rep(i, n) { imos[i + 1] += imos[i]; }

    rep(i, n) a[i] = imos[i];
  }
  rep(i, n) cout << imos[i] << " ";

  return 0;
}

Error: TLE

Fixed submission:

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

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

  vector<ll> imos(n + 1);

  rep(_, k) {
    imos.assign(n + 1, 0);
    rep(i, n) {
      if (i - a[i] <= 0)
        imos[0]++;
      else
        imos[i - a[i]]++;

      if (i + a[i] + 1 <= n)
        imos[i + a[i] + 1]--;
      else
        imos[n]--;
    }

    rep(i, n) { imos[i + 1] += imos[i]; }

    rep(i, n) a[i] = imos[i];
  }
  rep(i, n) cout << imos[i] << " ";

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

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int M = 1e7 + 5;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
const double eps = 1e-9;
typedef long long ll;
typedef pair<int, int> pi;

vector<ll> solve(vector<ll> a) {
  ll n = a.size();
  vector<ll> pref(n + 2, 0);
  for (int i = 0; i < n; i++) {
    int l = max(0LL, i - a[i]);
    int r = min(n - 1LL, i + a[i]);
    pref[l]++;
    pref[r + 1]--;
  }
  for (int i = 0; i < n; i++) {
    if (i)
      pref[i] += pref[i - 1];
    a[i] = pref[i];
  }
  return a;
}

int check(vector<ll> a) {
  int n = a.size();
  for (int i = 0; i < n; i++) {
    if (a[i] != n)
      return 0;
  }
  return 1;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);

  for (int i = 0; i < n; i++)
    cin >> a[i];

  while (k--) {
    a = solve(a);
    if (check(a))
      break;
  }
  for (auto x : a)
    cout << x << " ";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int M = 1e7 + 5;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
const double eps = 1e-9;
typedef long long ll;
typedef pair<int, int> pi;

vector<ll> solve(vector<ll> a) {
  ll n = a.size();
  vector<ll> pref(n + 2, 0);
  for (int i = 0; i < n; i++) {
    int l = max(0LL, i - a[i]);
    int r = min(n - 1LL, i + a[i]);
    pref[l]++;
    pref[r + 1]--;
  }
  for (int i = 0; i < n; i++) {
    if (i)
      pref[i] += pref[i - 1];
    a[i] = pref[i];
  }
  return a;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);

  for (int i = 0; i < n; i++)
    cin >> a[i];

  while (k--) {
    a = solve(a);
  }
  for (auto x : a)
    cout << x << " ";
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  REP(i, 0, N) cin >> A[i];

  while (K--) {
    vector<int> B(N, 0);
    REP(i, 0, N) {
      B[max(0, i - A[i])]++;
      if (i + A[i] + 1 < N)
        B[i + A[i] + 1]--;
    }
    bool done = true;
    REP(i, 1, N) {
      B[i] += B[i - 1];
      if (B[i] != N)
        done = false;
    }

    A = B;
    if (done)
      break;
  }
  for (auto a : A)
    cout << a << " ";
  cout << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  REP(i, 0, N) cin >> A[i];

  vector<int> B(N, 0);
  REP(i, 0, N) {
    B[max(0, i - A[i])]++;
    if (i + A[i] + 1 < N)
      B[i + A[i] + 1]--;
  }

  REP(i, 1, N) B[i] += B[i - 1];

  if (K == 1) {
    for (auto b : B)
      cout << b << " ";
    cout << endl;
    return 0;
  }

  A = B;
  B.assign(N, 0);

  K--;
  REP(i, 0, N) {
    B[max(0, i - A[i] - (K - 1))]++;
    if (i + A[i] + 1 < N)
      B[i + A[i] + 1 + (K - 1)]--;
  }

  REP(i, 1, N) B[i] += B[i - 1];

  for (auto b : B)
    cout << b << " ";
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;
const ll K_MAX = 1e5 + 5;

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

  rep(i, k) {
    int c_n = 0;
    vector<int> b(n);
    rep(j, n) { // j番目の数．// 累積和
      if (a[j] == n)
        c_n++;
      int l = max(0, j - a[j]);
      int r = min(n - 1, j + a[j]);
      b[l]++;
      if (r < n - 1)
        b[r + 1]--;
    }
    if (c_n == n)
      break;

    rep1(j, n - 1) { b[j] += b[j - 1]; }

    a = b;
  }

  rep(i, n) { cout << a[i] << " "; }
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;
const ll K_MAX = 1e5 + 5;

vector<int> a(int(1e5 + 5)), d;
int j2;

int main() {
  int n, k;
  cin >> n >> k;
  a.resize(n);
  d.resize(n);
  rep(i, n) {
    cin >> a[i];
    d[i] = i;
  }

  vector<int> b(n), zero(n, 0);

  rep(i, k) {
    int c_n = 0;

    b = zero;
    rep(j, n) { // j番目の数．
      if (a[j] == n)
        c_n++;

      for (int l = -a[j]; l <= a[j]; l++) {
        if (j + l >= 0 && j + l < n)
          b[j + l]++;
      }
    }
    if (c_n == n)
      break;
    a = b;
  }

  rep(i, n) { cout << a[i] << " "; }
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  vector<int> C(N, N);
  rep(j, K) {
    vector<int> B(N + 1, 0);
    rep(i, N) {
      int Le = max(0, i - A[i]);
      int Ri = min(N, i + A[i] + 1);
      B[Le]++;
      B[Ri]--;
    }
    rep(i, N) B[i + 1] += B[i];
    B.pop_back();
    if (A == B)
      break;
    A = B;
  }
  rep(i, N) { cout << A[i] << endl; }
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#include <initializer_list>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define repr(i, n) for (int i = int(n - 1); i >= 0; --i)
#define REP(i, m, n) for (int i = int(m), i##_len = (n); i < i##_len; ++i)
#define VV(T) vector<vector<T>>
#define co(P) cout << P << endl
#define ci(P) cin >> P
#define pb push_back
using namespace std;
using vi = vector<int>;
using vii = vector<vi>;
using ll = long long;
using d = double;

int main() {
  unsigned int N, K;
  vector<vector<ll>> A(N, vector<ll>(K + 1, 0));
  cin >> N >> K;
  rep(i, N) cin >> A[i][0];
  rep(i, K) {
    rep(j, N) {
      if (i - A[i][j] >= 0 && i + A[i][j] < N) {
        rep(k, 2 * A[i][j] + 1) { A[i - A[i][j] + k][j]++; }
      }
      if (i - A[i][j] < 0 && i + A[i][j] < N) {
        rep(k, A[i][j] + 1 + i - 1) { A[k][j]++; }
      }
      if (i - A[i][j] >= 0 && i + A[i][j] >= N) {
        rep(k, A[i][j] + 1 + N - A[i][j]) { A[i - A[i][j] + k][j]++; }
      }
      if (i - A[i][j] < 0 && i + A[i][j] >= N) {
        rep(k, N) { A[k][j]++; }
      }
    }
  }
  rep(i, N - 1) { cout << A[i][K + 1] << " "; }
  cout << A[N][K + 1] << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, ll> Pil;

int N, K;
const int MAX_N = 2.0e5 + 10;
int A[MAX_N];

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

  if (N <= K) {
    for (int i = 0; i < N; i++) {
      cout << N << " ";
    }
    cout << endl;
  } else {
    for (int i = 0; i < K; i++) {
      int B[N + 1];
      memset(B, 0, sizeof(B));
      for (int j = 0; j < N; j++) {
        int l = max(0, j - A[j]);
        int r = min(N, j + A[j] + 1);
        B[l]++;
        B[r]--;
      }
      for (int j = 0; j < N; j++) {
        B[j + 1] += B[j];
      }
      bool all_N = true;
      for (int j = 0; j < N; j++) {
        if (B[j] != N)
          all_N = false;
        A[j] = B[j];
      }
      if (all_N)
        break;
    }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, ll> Pil;

int N, K;
const int MAX_N = 2.0e5 + 10;
int A[MAX_N];

int main() {
  // ifstream in("test.txt");
  // cin.rdbuf(in.rdbuf());

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

  if (N <= K) {
    for (int i = 0; i < N; i++) {
      cout << N << " ";
    }
    cout << endl;
  } else {
    for (int i = 0; i < K; i++) {
      int next_A[N];
      memset(next_A, 0, sizeof(next_A));
      bool all_N = true;
      for (int j = 0; j < N; j++) {
        if (A[j] != N)
          all_N = false;
        for (int k = max(0, j - A[j]); k < min(N, j + A[j] + 1); k++) {
          next_A[k]++;
        }
      }
      for (int j = 0; j < N; j++) {
        A[j] = next_A[j];
      }
      if (all_N)
        break;
    }

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

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll i = 0; i < k; i++) {
    vector<ll> b(n);
    for (ll j = 0; j < n; j++) {
      ll l = j - a[j], r = j + a[j] + 1;
      if (l < 0) {
        b[0]++;
      } else {
        b[l]++;
      }
      if (r <= n - 1) {
        b[r]--;
      }
    }
    for (int j = 1; j < n; j++) {
      b[j] += b[j - 1];
    }
    a = b;
    if (a[0] == n && a[n - 1] == n)
      break;
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
  return;
}

int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    vector<ll> nx(n);
    for (int j = 0; j < n; j++) {
      int x = a[j];
      for (int k = max(0, j - x); k <= min(n - 1, j + x); k++) {
        nx[k]++;
      }
    }
    a = nx;
    if (a[0] == n && a[n - 1] == n)
      break;
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
  return;
}

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

#include <bits/stdc++.h>
#include <cctype>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
typedef long long int ll;
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int, int>

int main() {
  int N, K;
  cin >> N >> K;
  int a[N];
  rep(i, N) cin >> a[i];

  rep(j, min(K, 1000)) {
    vector<int> b(N + 1, 0);
    rep(i, N) {
      if (i - a[i] < 0)
        b[0]++;
      else
        b[i - a[i]]++;

      if (i + a[i] + 1 <= N)
        b[i + a[i] + 1]--;
      else
        b[N]--;
    }
    rep(i, N) {
      if (i == 0)
        a[i] = b[i];
      else
        a[i] = a[i - 1] + b[i];
    }
  }

  rep(i, N) {
    if (i > 0)
      cout << " ";
    cout << a[i];
  }
  cout << endl;

  // cout << fixed << setprecision(10);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cctype>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
typedef long long int ll;
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int, int>

int main() {
  int N, K;
  cin >> N >> K;
  int a[N];
  rep(i, N) cin >> a[i];

  rep(j, K) {
    vector<int> b(N + 1, 0);
    rep(i, N) {
      if (i - a[i] < 0)
        b[0]++;
      else
        b[i - a[i]]++;

      if (i + a[i] + 1 <= N)
        b[i + a[i] + 1]--;
      else
        b[N]--;
    }
    rep(i, N) {
      if (i == 0)
        a[i] = b[i];
      else
        a[i] = a[i - 1] + b[i];
    }
  }

  rep(i, N) {
    if (i > 0)
      cout << " ";
    cout << a[i];
  }
  cout << endl;

  // cout << fixed << setprecision(10);

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

struct init {
  init() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init_;

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> next(N + 1, 0);
    for (int j = 0; j < N; j++) {
      next[max(0, j - A[j])]++;
      next[min(N, j + A[j] + 1)]--;
    }
    for (int i = 0; i < N; i++) {
      next[i + 1] += next[i];
    }
    next.pop_back();
    swap(next, A);
    if (A == vector<int>(N, N))
      break;
  }

  for (const auto &e : A)
    cout << e << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

#ifdef _DEBUG
#include "../../library/library/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

struct init {
  init() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init_;

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> next(N, 0);
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      if (max(j, N - j - 1) <= A[j]) {
        cnt++;
        continue;
      }
      for (int k = max(0, j - A[j]); k <= min(N - 1, j + A[j]); k++) {
        next[k]++;
      }
    }
    for (auto &e : next)
      e += cnt;
    swap(next, A);
    if (A == vector<int>(N, N))
      break;
  }

  for (const auto &e : A)
    cout << e << "\\n";

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

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
ll n, k;
ll a[200005];
ll b[200005] = {0};
int main(void) {
  scanf("%lld%lld", &n, &k);
  for (ll i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  ll i1 = 0;
  while (true) {
    ll now = 0;
    memset(b, 0, sizeof(b));
    for (ll i = 0; i < n; i++) {
      if (i - a[i] < 0)
        b[0]++;
      else
        b[i - a[i]]++;
      if (i + a[i] < n)
        b[i + a[i] + 1]--;
    }
    for (ll i = 1; i < n; i++)
      b[i] += b[i - 1];
    for (ll i = 0; i < n; i++)
      if (b[i] >= n)
        now++;
    if (now == n)
      break;
    i1++;
    if (i1 == k)
      break;
    for (ll i = 0; i < n; i++)
      a[i] = b[i];
  }

  if (i1 == k) {
    for (ll i = 0; i < n; i++) {
      printf("%lld ", b[i]);
    }
  } else {
    for (ll i = 0; i < n; i++) {
      printf("%lld ", n);
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
ll n, k;
ll a[200005];
ll b[200005] = {0};
int main(void) {
  scanf("%lld%lld", &n, &k);
  for (ll i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  ll i1 = 0;
  while (true) {
    ll now = 0;
    memset(b, 0, sizeof(b));
    for (ll i = 0; i < n; i++) {
      if (i - a[i] < 0)
        b[0]++;
      else
        b[i - a[i]]++;
      if (i + a[i] < n)
        b[i + a[i] + 1]--;
    }
    for (ll i = 1; i < n; i++)
      b[i] += b[i - 1];
    for (ll i = 0; i < n; i++)
      if (b[i] > n)
        now++;
    if (now == n)
      break;
    i1++;
    if (i1 == k)
      break;
    for (ll i = 0; i < n; i++)
      a[i] = b[i];
  }

  if (i1 == k) {
    for (ll i = 0; i < n; i++) {
      printf("%lld ", b[i]);
    }
  } else {
    for (ll i = 0; i < n; i++) {
      printf("%lld ", n);
    }
  }
}