alexjercan/bugnet huggingface dataset:
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e5 + 555;
const int MOD = 1e9 + 7;
int n, k;
int chafeng[maxn];
void add(int l, int r) {
  l = max(1, l);
  r = min(n, r);
  chafeng[l]++;
  chafeng[r + 1]--;
}
bool solve(vector<int> &a) {
  for (int i = 1; i <= n + 1; i++)
    chafeng[i] = 0;
  for (int i = 1; i <= n; i++) {
    add(i - a[i], i + a[i]);
  }
  int now = 0;
  bool ok = true;
  for (int i = 1; i <= n; i++) {
    now += chafeng[i];
    a[i] = now;
    ok &= (a[i] == n);
  }
  return ok;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(10);
  cout << fixed;
  cin >> n >> k;
  vector<int> V(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> V[i];
  for (int i = 1; i <= k; i++) {
    if (solve(V))
      break;
  }
  for (int i = 1; i <= n; i++)
    cout << V[i] << ' ';
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e5 + 555;
const int MOD = 1e9 + 7;
int n, k;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
int ans[maxn];
bool solve(vector<int> &a) {
  tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
       tree_order_statistics_node_update>
      rbtree;
  for (int i = 1; i <= n; i++) {
    rbtree.insert(make_pair(i - a[i], i));
  }
  for (int i = 1; i <= n; i++) {
    auto s = make_pair(i, int(1e9));
    int rnk = rbtree.order_of_key(s);
    rbtree.erase(make_pair(i - a[i], i));
    ans[i] = rnk;
  }
  rbtree.clear();
  for (int i = 1; i <= n; i++) {
    auto s = make_pair(i, -1);
    int rnk = rbtree.order_of_key(s);
    ans[i] += rbtree.size() - rnk;
    rbtree.insert(make_pair(i + a[i], i));
  }
  bool ok = true;
  for (int i = 1; i <= n; i++) {
    a[i] = ans[i];
    ok &= (ans[i] == n);
  }
  return ok;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(10);
  cout << fixed;
  cin >> n >> k;
  vector<int> V(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> V[i];
  for (int i = 1; i <= k; i++) {
    if (solve(V))
      break;
  }
  for (int i = 1; i <= n; i++)
    cout << V[i] << ' ';
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#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];
  }

  int trial_num;
  if (K >= 41)
    trial_num = 41;
  else
    trial_num = K;

  for (int n = 0; n < trial_num; ++n) {
    vector<int> delta(N, 0);
    // for(int i=0; i<N; ++i){
    //     int power = a[i];

    //     int MIN = max(0, i - power);
    //     int MAX = min( N-1, i + power);

    //     if(power > 0){
    //         for(int j=MIN; j<i; ++j){
    //             delta[j]++;
    //         }

    //         for(int j=i+1; j<=MAX; ++j){
    //             delta[j]++;
    //         }
    //     }
    // }
    // for(int i=0; i<N; ++i){
    //     delta[i]++;
    // }
    // swap(a, delta);

    for (int i = 0; i < N; ++i) {
      int power = a[i];
      int l = max(0, i - power);
      int r = min(N - 1, i + power);

      delta[l]++;
      if (r + 1 < N)
        delta[r + 1]--;
    }

    for (int i = 1; i < N; i++) {
      delta[i] += delta[i - 1];
    }
    swap(a, delta);
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#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];
  }

  if (N >= K) {
    for (int n = 0; n < K; ++n) {
      vector<int> delta(N, 0);
      // for(int i=0; i<N; ++i){
      //     int power = a[i];

      //     int MIN = max(0, i - power);
      //     int MAX = min( N-1, i + power);

      //     if(power > 0){
      //         for(int j=MIN; j<i; ++j){
      //             delta[j]++;
      //         }

      //         for(int j=i+1; j<=MAX; ++j){
      //             delta[j]++;
      //         }
      //     }
      // }
      // for(int i=0; i<N; ++i){
      //     delta[i]++;
      // }
      // swap(a, delta);

      for (int i = 0; i < long(N); ++i) {
        int power = a[i];
        int l = max(0, i - power);
        int r = min(N - 1, i + power);

        delta[l]++;
        if (r + 1 < N)
          delta[r + 1]--;
      }

      for (int i = 1; i < N; i++) {
        delta[i] += delta[i - 1];
      }
      swap(a, delta);
    }
  } else {
    for (int i = 0; i < N; ++i) {
      a[i] = N;
    }
  }

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

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

#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];
  }

  // int trial_num;

  for (int n = 0; n < K; ++n) {
    vector<int> delta(N, 0);
    //     for(int i=0; i<N; ++i){
    //         int power = a[i];

    //         int MIN = max(0, i - power);
    //         int MAX = min( N-1, i + power);

    //         if(power > 0){
    //             for(int j=MIN; j<i; ++j){
    //                 delta[j]++;
    //             }

    //             for(int j=i+1; j<=MAX; ++j){
    //                 delta[j]++;
    //             }
    //         }
    //     }
    //     for(int i=0; i<N; ++i){
    //         delta[i]++;
    //     }
    //     swap(a, delta);

    // 　累積和
    for (int i = 0; i < N; ++i) {
      int power = a[i];
      int l = max(0, i - power);
      int r = min(N - 1, i + power);

      delta[l]++;
      if (r + 1 < N)
        delta[r + 1]--;
    }

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

    if (a == delta)
      break;
    else
      swap(a, delta);
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#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];
  }

  int trial_num;
  if (K >= 41)
    trial_num = 41;
  else
    trial_num = K;

  for (int n = 0; n < trial_num; ++n) {
    vector<int> delta(N, 0);
    for (int i = 0; i < N; ++i) {
      int power = a[i];

      int MIN = max(0, i - power);
      int MAX = min(N - 1, i + power);

      if (power > 0) {
        for (int j = MIN; j < i; ++j) {
          delta[j]++;
        }

        for (int j = i + 1; j <= MAX; ++j) {
          delta[j]++;
        }
      }
    }
    for (int i = 0; i < N; ++i) {
      delta[i]++;
    }
    swap(a, delta);

    // for(int i=0; i<N; ++i){
    //     int power = a[i];
    //     int l  = max(0, i- power);
    //     int r = min(N-1, i+power);

    //     delta[l]++;
    //     if(r+1 < N) delta[r+1]--;
    // }

    // for(int i=1; i<N; i++){
    //     delta[i] += delta[i-1];
    // }
    // swap(a, delta);
  }

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

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

#include <bits/stdc++.h>
#define rep(i, N) for (int(i) = 0; (i) < (N); (i)++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;

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

  rep(i, k) {
    vector<int> sum(n + 1);
    rep(i, n) {
      int low = max(0, i - a[i]);
      int high = min(n, i + a[i] + 1);
      sum[low]++;
      sum[high]--;
    }

    int cur = 0;
    bool ok = true;
    rep(i, n) {
      cur += sum[i];
      a[i] = cur;
      if (cur != n)
        ok = false;
    }

    if (ok)
      break;
  }

  rep(i, n) printf("%d%c", a[i], (i == n - 1) ? '\\n' : ' ');
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, N) for (int(i) = 0; (i) < (N); (i)++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int mod = 1000000007;

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

  rep(i, k) {
    vector<int> sum(n + 1);
    rep(i, n) {
      int low = max(0, i - a[i]);
      int high = min(n + 1, i + a[i] + 1);
      sum[low]++;
      sum[high]--;
    }

    int cur = 0;
    bool ok = true;
    rep(i, n) {
      cur += sum[i];
      a[i] = cur;
      if (cur != n)
        ok = false;
    }

    if (ok)
      break;
  }

  rep(i, n) printf("%d%c", a[i], (i == n - 1) ? '\\n' : ' ');
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (long long i = 0; i < K; i++) {
    vector<long long> cnt(N, 0);
    for (long long i = 0; i < N; i++) {
      cnt[max(i - A[i], 0LL)]++;
      if (i + A[i] + 1 < N) {
        cnt[i + A[i] + 1]--;
      }
    }
    for (long long i = 1; i < N; i++) {
      cnt[i] = cnt[i] + cnt[i - 1];
    }
    for (long long i = 0; i < N; i++) {
      A[i] = cnt[i];
    }
    if (i == 0) {
      if (N - max(A[0], A[N - 1]) <= K - 1) {
        for (long long i = 0; i < N; i++) {
          cout << N;
          if (i + 1 != N) {
            cout << " ";
          }
        }
        cout << endl;
        return 0;
      }
    }
    if (i + 1 == K) {
      for (long long i = 0; i < N; i++) {
        cout << max(cnt[i], 0LL);
        if (i + 1 != N) {
          cout << " ";
        }
      }
      cout << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed 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> cnt(N);
    for (int i = 0; i < N; i++) {
      cnt[max(i - A[i], 0LL)]++;
      if (i + A[i] + 1 < N) {
        cnt[i + A[i] + 1]--;
      }
    }
    for (int i = 1; i < N; i++) {
      cnt[i] = cnt[i] + cnt[i - 1];
    }
    if (i + 1 != K) {
      for (int i = 0; i < N; i++) {
        A[i] = cnt[i];
      }
    } else {
      for (int i = 0; i < N; i++) {
        cout << cnt[i];
        if (i + 1 != N) {
          cout << " ";
        }
      }
      cout << endl;
    }
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author NikuKyabe
 */

#include "bits/stdc++.h"

using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) FOR(i, 0, (n))
typedef vector<int> vec;

class CLamps {
public:
  void solve(std::istream &in, std::ostream &out) {
    int n, k;
    in >> n >> k;
    vector<int> a(n);
    REP(i, n) in >> a[i];
    vector<pair<double, double>> b(n);
    REP(j, min(k, 100ll)) {
      REP(i, n) { b[i] = make_pair(max(i - a[i], 0ll), min(i + a[i] + 1, n)); }
      vec c(n + 1, 0);
      REP(i, n) {
        c[b[i].first]++;
        c[b[i].second]--;
      }
      FOR(i, 1, c.size()) {
        c[i] += c[i - 1];
        a[i - 1] = c[i - 1];
      }
    }
    REP(i, n) { out << a[i] << " "; }
    out << endl;
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cout << fixed;
  CLamps solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: TLE

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author NikuKyabe
 */

#include "bits/stdc++.h"

using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) FOR(i, 0, (n))
typedef vector<int> vec;

class CLamps {
public:
  void solve(std::istream &in, std::ostream &out) {
    int n, k;
    in >> n >> k;
    vector<int> a(n);
    REP(i, n) in >> a[i];
    vector<pair<double, double>> b(n);
    REP(j, k) {
      REP(i, n) { b[i] = make_pair(max(i - a[i], 0ll), min(i + a[i] + 1, n)); }
      vec c(n + 1, 0);
      REP(i, n) {
        c[b[i].first]++;
        c[b[i].second]--;
      }
      FOR(i, 1, c.size()) {
        c[i] += c[i - 1];
        a[i - 1] = c[i - 1];
      }
    }
    REP(i, n) { out << a[i] << " "; }
    out << endl;
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cout << fixed;
  CLamps solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

signed main() {
  cout << fixed << setprecision(10);

  int N, K;
  vector<int> A;
  vector<vector<int>> ans;

  cin >> N >> K;

  A.resize(N);
  ans.resize(2, vector<int>(N + 1));

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

  ans[0] = A;

  for (int i = 0; i < K; i++) {
    for (int j = 0; j < N; j++) {
      ans[(i + 1) % 2][max(0ll, j - ans[i % 2][j])]++;
      ans[(i + 1) % 2][min(N, j + ans[i % 2][j] + 1)]--;
    }

    for (int j = 1; j < N; j++) {
      ans[(i + 1) % 2][j] += ans[(i + 1) % 2][j - 1];
    }
    bool flag = true;

    for (int j = 0; j < N; j++) {
      if (ans[(i + 1) % 2][j] != N) {
        flag = false;
      }
      ans[(i) % 2][j] = 0;
    }

    ans[(i) % 2][N] = 0;

    if (flag == true) {
      K = i + 1;
      break;
    }
  }

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << ans[K % 2][i];
  }
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

signed main() {
  cout << fixed << setprecision(10);

  int N, K;
  vector<int> A;
  vector<vector<int>> ans;

  cin >> N >> K;

  A.resize(N);
  ans.resize(2, vector<int>(N));

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

  ans[0] = A;

  for (int i = 0; i < K; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = max(0ll, j - ans[i % 2][j]);
           k <= min(N - 1, j + ans[i % 2][j]); k++) {
        ans[(i + 1) % 2][k]++;
      }
    }

    bool flag = true;

    for (int j = 0; j < N; j++) {
      if (ans[(i + 1) % 2][j] != N) {
        flag = false;
      }
      ans[(i) % 2][j] = 0;
    }

    if (flag == true)
      break;

    // cout<<"======= "<<i<<" ======== "<<endl;
    // for(int j = 0; j < N; j++){
    // if(j) cout<<" ";
    // cout<<ans[(i+1)%2][j];
    // }
    // cout<<endl;
  }

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << ans[K % 2][i];
  }
  cout << endl;

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

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

vector<int> cont(int n, vector<int> a) {
  vector<int> b(n, 0);

  rep(i, n) {
    int l = max(0, i - a.at(i));
    int r = min(n - 1, i + a.at(i));
    b.at(l)++;
    if (r + 1 < n)
      b.at(r + 1)--;
  }

  for (int i = 1; i < n; ++i)
    b.at(i) += b.at(i - 1);

  return b;
}

int main(void) {
  int n, k;
  cin >> n >> k;

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

  rep(i, n) { cin >> a.at(i); }

  int rp = min(k, 41);

  rep(i, rp) { a = cont(n, a); }

  rep(i, n) { cout << a.at(i) << endl; }

  return 0;
}


Error: TLE

Fixed submission:

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

vector<int> cont(vector<int> a) {
  int n = a.size();
  vector<int> b(n, 0);

  rep(i, n) {
    rep(j, a.at(i) + 1) {
      if (i - j >= 0)
        b.at(i - j)++;
      if (j != 0 && i + j < n)
        b.at(i + j)++;
    }
  }

  return b;
}

int main(void) {
  int n, k;
  cin >> n >> k;

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

  rep(i, n) { cin >> a.at(i); }

  rep(i, k) { a = cont(a); }

  rep(i, n) { cout << a.at(i) << endl; }

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

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

int main() {
  int N, K;
  cin >> N >> K;
  int A[200010] = {};
  int B[200010] = {};
  int C[200010] = {};
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[i] = a;
  }
  int t = 0;
  while (t < K) {
    for (int i = 0; i < N; i++) {
      int b = max(i - A[i], 0);
      int c = min(A[i] + i, N);
      B[b] += 1;
      B[c + 1] += (-1);
    }
    for (int q = 0; q <= N; q++) {
      if (0 < q)
        B[q] += B[q - 1];
    }

    for (int i = 0; i < N; i++) {
      A[i] = B[i];
      B[i] = 0;
    }
    bool s = true;
    for (int u = 0; u < N; u++) {
      if (A[u] != N) {
        s = false;
        break;
      }
    }
    if (s == true) {
      break;
    }
    t++;
  }

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

Error: TLE

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  int A[200010] = {};
  int B[200010] = {};
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[i] = a;
  }
  int t = 0;
  while (t < K) {
    for (int i = 0; i < N; i++) {
      int b = max(i - A[i], 0);
      int c = min(A[i] + i, N);
      for (int j = b; j <= c; j++) {
        B[j] += 1;
      }
    }
    for (int i = 0; i < N; i++) {
      A[i] = B[i];
      B[i] = 0;
    }
    bool s = true;
    for (int u = 0; u < N; u++) {
      if (A[u] != N) {
        s = false;
        break;
      }
    }
    if (s == true) {
      break;
    }
    t++;
  }

  for (int i = 0; i < N; i++) {
    cout << A[i] << endl;
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ll long long int

using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n);
  for (auto &a : A)
    cin >> a;

  for (ll i = 0; i < k; ++i) {
    vector<ll> B(n + 1, 0);
    for (ll j = 0; j < n; ++j) {
      ll l = max(0LL, j - A[j]);
      ll r = min(n, j + A[j] + 1);
      B[l] += 1;
      B[r] -= 1;
    }
    ll cur = 0;
    for (ll j = 0; j < n; ++j) {
      cur += B[j];
      A[j] = cur;
    }

    bool ok = true;
    for (ll j = 0; j < n; ++j) {
      if (A[j] != n) {
        ok = false;
        break;
      }
    }
    if (ok)
      break;
  }

  for (auto a : A)
    cout << a << " ";
  cout << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long int

using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> A(n);
  for (auto &a : A)
    cin >> a;

  for (ll i = 0; i < k; ++i) {
    vector<ll> B(n, 0);
    for (ll j = 0; j < n; ++j) {
      for (ll p = max(0LL, j - A[j]); p <= min(n - 1, j + A[j]); ++p) {
        ++B[p];
      }
    }
    A = B;

    bool ok = true;
    for (ll j = 0; j < n; ++j) {
      if (A[j] != n) {
        ok = false;
        break;
      }
    }
    if (ok)
      break;
  }

  for (auto a : A)
    cout << a << " ";
  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  long n, k, i, j, l, r, j1, zero, sum, count;
  cin >> n >> k;
  long a[n], a1[n], b[n + 1];
  zero = 0;
  sum = 0;
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < k; i++) {
    for (j = 0; j < n; j++)
      b[j] = 0;
    count = 0;
    for (j = 0; j < n; j++) {
      l = max(zero, j - a[j]);
      r = min(n - 1, j + a[j]);
      b[l]++;
      b[r + 1]--;
    }
    for (j = 0; j < n; j++)
      a[j] = 0;
    for (j = 0; j < n; j++) {
      count += b[j];
      a[j] += count;
      sum += a[j];
    }
    if (sum == n * n)
      break;
    else
      sum = 0;
  }
  for (i = 0; i < n; i++)
    cout << a[i] << " ";
  cout << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  long n, k, i, j, l, r, j1, zero, sum, count;
  cin >> n >> k;
  long a[n][2], a1[n];
  zero = 0;
  sum = 0;
  count = 0;
  for (i = 0; i < n; i++)
    cin >> a[i][0];
  for (i = 0; i < n; i++)
    a[i][1] = 0;
  for (i = 0; i < k; i++) {
    for (j = 0; j < n; j++)
      a1[j] = 0;
    for (j = 0; j < n; j++) {
      if (a[j][1] == 1)
        count++;
      else {
        l = max(zero, j - a[j][0]);
        r = min(n - 1, j + a[j][0]);
        if (l == 0 && r == n - 1) {
          a[j][1] = 1;
          count++;
        } else
          for (j1 = l; j1 <= r; j1++)
            a1[j1]++;
      }
    }
    for (j = 0; j < n; j++)
      a1[j] += count;
    count = 0;
    for (j = 0; j < n; j++) {
      a[j][0] = a1[j];
      sum += a1[j];
    }
    if (sum == n * n)
      break;
    else
      sum = 0;
  }
  for (i = 0; i < n; i++)
    cout << a[i][0] << " ";
  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[200010];
int cf[200010];
int fir;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] >= i - 1) {
      fir++;
    }
    if (i - a[i] >= 2) {
      cf[i - a[i]]++;
    }
    if (i + a[i] + 1 <= n) {
      cf[i + a[i] + 1]--;
    }
  }
  while (k--) {
    a[1] = fir;
    int cnt = 0;
    for (int i = 2; i <= n; i++) {
      a[i] = a[i - 1] + cf[i];
      cf[i] = 0;
      cnt += (a[i] >= n);
    }
    cnt += (a[1] >= n);
    fir = 0;
    if (cnt >= n) {
      break;
    }
    for (int i = 1; i <= n; i++) {
      if (a[i] >= i - 1) {
        fir++;
      }
      if (i - a[i] >= 2) {
        cf[i - a[i]]++;
      }
      if (i + a[i] + 1 <= n) {
        cf[i + a[i] + 1]--;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << a[i] << ' ';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[200010];
int cf[200010];
int fir;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] >= i - 1) {
      fir++;
    }
    if (i - a[i] >= 2) {
      cf[i - a[i]]++;
    }
    if (i + a[i] + 1 <= n) {
      cf[i + a[i] + 1]--;
    }
  }
  while (k--) {
    a[1] = fir;
    int cnt = 0;
    for (int i = 2; i <= n; i++) {
      a[i] = a[i - 1] + cf[i];
      cf[i] = 0;
      cnt += (a[i] >= n);
    }
    fir = 0;
    if (cnt >= n) {
      break;
    }
    for (int i = 1; i <= n; i++) {
      if (a[i] >= i - 1) {
        fir++;
      }
      if (i - a[i] >= 2) {
        cf[i - a[i]]++;
      }
      if (i + a[i] + 1 <= n) {
        cf[i + a[i] + 1]--;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << a[i] << ' ';
  }
  return 0;
}