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

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

using ll = long long;
// print(fixed << setprecision(10));
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repk(i, n, k) for (ll i = k; i < (n); i++)
#define all(n) n.begin(), n.end()
#define print(n) cout << n << endl
double PI = acos(-1.0);
int INF = 1e9;
ll LINF = 1e18;
ll MOD = 1e9 + 7;

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

  int n, k;
  bool f;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, k) {
    vector<int> b(n, 0);
    rep(j, n) {
      if (j - a[j] < 0)
        b[0]++;
      else
        b[j - a[j]]++;
      if (j + a[j] < n - 1)
        b[j + a[j] + 1]--;
    }
    f = 1;
    rep(j, n) {
      if (j != 0)
        b[j] += b[j - 1];
      if (f == 1 && b[j] != n)
        f = 0;
    }
    a = b;
    if (f)
      break;
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
  return 0;
}

Error: WA

Fixed submission:

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

using ll = long long;
// print(fixed << setprecision(10));
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repk(i, n, k) for (ll i = k; i < (n); i++)
#define all(n) n.begin(), n.end()
#define print(n) cout << n << endl
double PI = acos(-1.0);
int INF = 1e9;
ll LINF = 1e18;
ll MOD = 1e9 + 7;

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

  int n, k, min;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, k) {
    vector<int> b(n, 0);
    rep(j, n) {
      if (j - a[j] < 0)
        b[0]++;
      else
        b[j - a[j]]++;
      if (j + a[j] < n - 1)
        b[j + a[j] + 1]--;
    }
    b[0] = min;
    rep(j, n) {
      if (j != 0)
        b[j] += b[j - 1];
      if (b[j] < min)
        min = b[j];
    }
    if (min == n)
      break;
    a = b;
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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> b(n + 1);
    for (int j = 0; j < n; ++j) {
      int l = max(0, j - a[j]);
      int r = min(j + a[j] + 1, n);
      b[l]++;
      b[r]--;
    }
    for (int m = 0; m < n; ++m)
      b[m + 1] += b[m];
    b.pop_back();
    if (a == b)
      break;
    a = b;
  }
  // if(k>=50)k=50;
  // for(int i=0;i<k;++i){
  //     vector<int> cnt(n,0);
  //     for(int j=0;j<n;++j){
  //         if(a[j]==0)cnt[j]++;
  //         else{
  //             cnt[j]++;
  //             for(int k=1;k<=a[j];++k){
  //                 if(j+k<n)cnt[j+k]++;
  //                 if(j-k>=0)cnt[j-k]++;
  //             }
  //         }
  //     }
  //     for(int l=0;l<n;++l)a[l]=cnt[l];
  // }
  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;
using ll = long long;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  if (k >= 50)
    k = 50;
  for (int i = 0; i < k; ++i) {
    vector<ll> cnt(n, 0);
    for (int j = 0; j < n; ++j) {
      if (a[j] == 0)
        cnt[j]++;
      else {
        cnt[j]++;
        for (int k = 1; k <= a[j]; ++k) {
          if (j + k < n)
            cnt[j + k]++;
          if (j - k >= 0)
            cnt[j - k]++;
        }
      }
    }
    for (int l = 0; l < n; ++l)
      a[l] = cnt[l];
  }
  for (int i = 0; i < n; ++i)
    cout << a[i] << " ";
  cout << "" << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> B(n + 2, 0);
  vector<int> a(n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  while (k--) {
    for (int i = 0; i <= n + 1; i++) {
      B[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      int l = max(1, i - a[i]);
      int r = min(n, i + a[i]);
      B[l]++;
      B[r + 1]--;
    }

    //  cout<<B[1]<<" dsf";
    for (int i = 1; i <= n; i++)
      B[i] += B[i - 1];

    // for(int i=1;i<=n;i++)cout<<B[i]<<" ";
    //     cout<<endl;

    bool check = true;

    for (int i = 1; i <= n; i++) {
      a[i] = B[i];
      if (a[i] != n)
        check = false;
    }
    if (check)
      break;
  }
  for (int i = 1; i <= n; i++) {
    if (i > 1)
      cout << " ";
    cout << a[i];
  }
  cout << " \\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int I[200000 + 1];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  int A[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  while (k--) {
    for (int i = 1; i <= n; i++) {
      int x = i - A[i];
      if (x < 1)
        x = 1;
      int y = i + A[i];
      if (y > n)
        y = n;
      for (int j = x; j <= y; j++) {
        I[j]++;
      }
    }
    for (int i = 1; i <= n; i++) {
      A[i] = I[i];
      //    cout<<A[i]<<" ";
      I[i] = 0;
    }
    //  cout<<endl;
  }

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

/*
 * Since g++10 is released, some characters is not valid inside #if 0 :(
 * So, why not using clang++? :D

 * Date:
  echo -n '  ' && date +%Y.%m.%d # Just Run this (Type !!sh in Vim).

 * Solution:
  To be updated after "Accept".

 * Digression:

 * CopyRight:
          ▁▃▄▄▄▃▃▃▃▄▶
        ▗▇▀▔    ▔▔▔▔
       ▄▛   ▃▅━━■▄▂
      ▟▊   ▐▘     ▀▙
     ▟▜▌   ▐▖   ▋  ▐▍
    ▟▘ ▜   ▝▀▇▆●▘  ▐▌
  ▗▟▘   ▜▃       ▁▅▛
  ▔▀▼▅▄▃▃██▅▄▄▄▅■▀▔
        ▔▔▔▔▔▔
 */

#include <algorithm>
#include <cstdio>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;

struct {
  inline operator int() {
    int x;
    return scanf("%d", &x), x;
  }
  inline operator ll() {
    ll x;
    return scanf("%lld", &x), x;
  }
  template <class T> inline void operator()(T &x) { x = *this; }
  template <class T, class... A> inline void operator()(T &x, A &...a) {
    x = *this;
    this->operator()(a...);
  }
} read;

const int maxn = 200005;
int a[maxn], b[maxn];

int main() {
  int n = read, m = read;
  for (int i = 1; i <= n; i++)
    read(a[i]);
  while (m--) {
    for (int i = 1; i <= n; i++) {
      int l = std::max(1, i - a[i]), r = std::min(i + a[i], n);
      ++b[l];
      --b[r + 1];
    }
    bool full = 1;
    for (int i = 1; i <= n; i++) {
      a[i] = b[i] + a[i - 1];
      b[i] = 0;
      full &= a[i] == n;
    }
    if (full)
      break;
  }
  for (int i = 1; i <= n; i++)
    printf("%d ", a[i]);
  puts("");
}


Error: TLE

Fixed submission:

/*
 * Since g++10 is released, some characters is not valid inside #if 0 :(
 * So, why not using clang++? :D

 * Date:
  echo -n '  ' && date +%Y.%m.%d # Just Run this (Type !!sh in Vim).

 * Solution:
  To be updated after "Accept".

 * Digression:

 * CopyRight:
          ▁▃▄▄▄▃▃▃▃▄▶
        ▗▇▀▔    ▔▔▔▔
       ▄▛   ▃▅━━■▄▂
      ▟▊   ▐▘     ▀▙
     ▟▜▌   ▐▖   ▋  ▐▍
    ▟▘ ▜   ▝▀▇▆●▘  ▐▌
  ▗▟▘   ▜▃       ▁▅▛
  ▔▀▼▅▄▃▃██▅▄▄▄▅■▀▔
        ▔▔▔▔▔▔
 */

#include <algorithm>
#include <cstdio>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;

struct {
  inline operator int() {
    int x;
    return scanf("%d", &x), x;
  }
  inline operator ll() {
    ll x;
    return scanf("%lld", &x), x;
  }
  template <class T> inline void operator()(T &x) { x = *this; }
  template <class T, class... A> inline void operator()(T &x, A &...a) {
    x = *this;
    this->operator()(a...);
  }
} read;

const int maxn = 200005;
int a[maxn], b[maxn];

int main() {
  int n = read, m = read;
  for (int i = 1; i <= n; i++)
    read(a[i]);
  while (m--) {
    for (int i = 1; i <= n; i++) {
      int l = std::max(1, i - a[i]), r = std::min(i + a[i], n);
      ++b[l];
      --b[r + 1];
    }
    bool full = 0;
    for (int i = 1; i <= n; i++) {
      a[i] = b[i] + a[i - 1];
      b[i] = 0;
      full &= a[i] == n;
    }
    if (full)
      break;
  }
  for (int i = 1; i <= n; i++)
    printf("%d ", a[i]);
  puts("");
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main() {
  cout << fixed << setprecision(10);
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) { cin >> A[i]; }
  vector<int> minimum(N), maximum(N);
  vector<int> ans(N + 5);
  rep(i, K) {
    rep(i, N) {
      minimum[i] = i - A[i];
      if (minimum[i] < 0)
        minimum[i] = 0;
      maximum[i] = i + A[i];
      if (maximum[i] > N - 1)
        maximum[i] = N - 1;
    }
    for (int i = 0; i < N; i++) {
      ans[minimum[i]]++;
      ans[maximum[i] + 1]--;
    }
    for (int i = 0; i < N; i++) {
      if (i > 0)
        ans[i] += ans[i - 1];
    }
    rep(i, N) {
      A[i] = ans[i];
      ans[i] = 0;
    }
    if (A[0] == N && A[N - 1] == N) {
      break;
    }
  }
  rep(i, N) {
    if (i != N - 1)
      cout << A[i] << " ";
    else
      cout << A[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main() {
  cout << fixed << setprecision(10);
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) { cin >> A[i]; }
  vector<int> minimum(N), maximum(N);
  vector<int> ans(N + 5);
  rep(i, K) {
    rep(i, N) {
      minimum[i] = i - A[i];
      if (minimum[i] < 0)
        minimum[i] = 0;
      maximum[i] = i + A[i];
      if (maximum[i] > N - 1)
        maximum[i] = N - 1;
    }
    for (int i = 0; i < N; i++) {
      ans[minimum[i]]++;
      ans[maximum[i] + 1]--;
    }
    for (int i = 0; i < N; i++) {
      if (i > 0)
        ans[i] += ans[i - 1];
    }
    rep(i, N) {
      A[i] = ans[i];
      ans[i] = 0;
    }
    if (ans[0] == N && ans[N - 1] == N) {
      break;
    }
  }
  rep(i, N) {
    if (i != N - 1)
      cout << A[i] << " ";
    else
      cout << A[i] << endl;
  }
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
using vi = vector<int64_t>;
using vvi = vector<vi>;
using ll = int64_t;
using P = pair<ll, ll>;
ll INF = 1000000007;
// ll INF = 9223372036854775807;
// ll INF = 998244353;

int main() {
  // fasten cin
  cin.tie(0);
  ios::sync_with_stdio(false);
  // implement
  ll n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, n) cin >> a.at(i);
  rep(i, min(k, (ll)50)) {
    vi flg(n + 1), next(n);
    vector<P> p(n);
    rep(l, n) {
      flg.at(max((ll)0, l - a.at(l)))++;
      flg.at(min(n - 1, l + a.at(l)) + 1)--;
    }
    next.at(0) = flg.at(0);
    rep(l, n - 1) next.at(l + 1) = flg.at(l + 1) + next.at(l);
    a = next;
  }
  rep(i, n) cout << a.at(i) << " ";
  cout << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
using vi = vector<int64_t>;
using vvi = vector<vi>;
using ll = long long;
using P = pair<ll, ll>;
ll INF = 1000000007;
// ll INF = 9223372036854775807;
// ll INF = 998244353;

int main() {
  // fasten cin
  cin.tie(0);
  ios::sync_with_stdio(false);
  // implement
  ll n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, n) cin >> a.at(i);
  rep(i, min(k, 50LL)) {
    vi next(n);
    rep(l, n) {
      for (ll j = l - a.at(l); j <= l + a.at(l); j++) {
        if (j < 0 || j >= n)
          continue;
        next.at(j)++;
      }
    }
    a = next;
  }
  rep(i, n) cout << a.at(i) << " ";
  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long a[300000], t1[300000], t2[300000];
int main() {
  long long n, k;
  cin >> n >> k;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
  }
  long long f = 1;
  while (k > 0 && f == 1) {
    f = 0;
    for (long long i = 0; i < n; ++i) {
      long long g = i + a[i] + 1;
      t1[i]++;
      if (g < n)
        t1[g]--;
    }
    for (long long i = 1; i < n; ++i) {
      long long g = i - a[i] - 1;
      t2[i - 1]++;
      if (g >= 0)
        t2[g]--;
    }
    for (long long i = 1; i < n; ++i) {
      t1[i] += t1[i - 1];
    }
    for (long long i = n - 2; i >= 0; i--) {
      t2[i] += t2[i + 1];
    }
    for (long long i = 0; i < n; ++i) {
      a[i] = t1[i] + t2[i];
      t1[i] = 0;
      t2[i] = 0;
      // printf("%lld ",a[i] );
      if (a[i] < n)
        f = 1;
    }
    // printf("\\n");
    k--;
  }
  if (f == 0) {
    for (long long i = 0; i < n; ++i) {
      // a[i] += k;
      printf("%lld ", n);
    }
    printf("\\n");
  } else {
    for (long long i = 0; i < n; ++i) {
      // a[i] += k;
      printf("%lld ", a[i]);
    }
    printf("\\n");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[300000], t[300000];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int f = 1;
  while (k > 0 && f == 1) {
    f = 0;
    // printf("k %d\\n",k );
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j <= a[i]; j++) {
        int g = i + j;
        if (g < n) {
          t[g]++;
        } else
          break;
      }
      for (int j = 1; j <= a[i]; j++) {
        int g = i - j;
        if (g >= 0) {
          t[g]++;
        } else
          break;
      }
    }
    for (int i = 0; i < n; ++i) {
      a[i] = t[i];
      t[i] = 0;
      if (a[i] < n - 1) {
        f = 1;
      }
    }
    k--;
  }
  for (int i = 0; i < n; ++i) {
    a[i] += k;
    printf("%d ", a[i]);
  }
  printf("\\n");

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, n, j) for (int i = (j); i < (n); ++i)
#define rrep(i, n) for (int i = (n); i >= 0; --i)
#define ssort(n) sort((n).begin(), (n).end())
using ll = long long;
using ld = long double;
typedef pair<int, int> P;
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define INF 10101010

int main() {
  int N;
  int K;
  cin >> N >> K;
  vector<int> a(N);

  bool result = true;

  rep(i, N) { cin >> a[i]; }

  rep(i, K) {
    vector<int> b(N + 1);
    rep(i, N + 1) b[i] = 0;

    rep(j, N) { b[j] = 0; }
    rep(j, N) {
      int l = max(0, j - a[j]);
      int r = min(N, j + a[j] + 1);
      b[l]++;
      b[r]--;
    }
    FOR(j, N, 1) b[j] += b[j - 1];

    b.pop_back();
    if (a == b)
      break;
    a = b;
  }

  rep(i, N) cout << a[i] << " ";
  cout << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, n, j) for (int i = (j); i < (n); ++i)
#define rrep(i, n) for (int i = (n); i >= 0; --i)
#define ssort(n) sort((n).begin(), (n).end())
using ll = long long;
using ld = long double;
typedef pair<int, int> P;
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define INF 10101010

int main() {
  int N;
  int K;
  cin >> N >> K;
  vector<int> a(N);
  vector<int> b(N + 1);
  bool result = true;

  rep(i, N) {
    cin >> a[i];
    b[i] = 0;
  }
  b[N] = 0;
  if (N == 1) {
    b[0] = 1;
  } else {
    rep(i, K) {
      rep(j, N) { b[j] = 0; }
      rep(j, N) {
        int l = max(0, j - a[j]);
        int r = min(N, j + a[j] + 1);
        b[l]++;
        b[r]--;
      }
      FOR(j, N, 1) b[j] += b[j - 1];
      if (a == b)
        break;
      rep(j, N) { a[j] = b[j]; }
    }
  }

  rep(i, N) cout << b[i] << " ";
  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  while (K--) {
    vector<int> tmp(N + 1);
    for (int i = 0; i < N; i++) {
      tmp[max(0, i - A[i])]++;
      tmp[min(N, i + A[i] + 1)]--;
    }
    partial_sum(tmp.begin(), tmp.end(), tmp.begin());
    A = tmp;
    if (count(A.begin(), A.end() - 1, N) == N)
      break;
  }
  for (int i = 0; i < N; i++) {
    cout << A[i] << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  while (K--) {
    vector<int> tmp(N + 1);
    for (int i = 0; i < N; i++) {
      tmp[max(0, i - A[i])]++;
      tmp[min(N, i + A[i]) + 1]--;
    }
    partial_sum(tmp.begin(), tmp.end(), tmp.begin());
    A = tmp;
    if (count(A.begin(), A.end() - 1, N) == N)
      break;
  }
  for (int i = 0; i < N; i++) {
    cout << A[i] << endl;
  }
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
// Begin Header {{{
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const long double PI = acos(-1);
// }}} End Header

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  rep(ki, k) {
    vector<ll> b(n + 1);
    rep(i, n) {
      ll l = max(ll(0), i - a[i]);
      ll r = min(n, i + a[i] + 1);
      b[l]++;
      b[r]--;
    }
    rep(i, n) b[i + 1] += b[i];
    b.pop_back();
    if (a == b)
      break;
    a = b;
  }
  rep(i, n - 1) cout << a[i] << " ";
  cout << a[n - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// Begin Header {{{
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const long double PI = acos(-1);
// }}} End Header

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  rep(ki, k) {
    vector<ll> b(n + 1);
    rep(i, n) {
      ll l = max(ll(0), i - a[i]);
      ll r = min(n - 1, i + a[i]);
      b[l]++;
      b[r + 1]--;
    }
    loop(i, 1, n) b[i] += b[i - 1];
    a.resize(sizeof(b));
    copy(all(b), a.begin());
  }
  rep(i, n - 1) cout << a[i] << " ";
  cout << a[n - 1] << 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;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int mn, mx, a;
  for (int j = 1; j <= K; j++) {
    vector<int> B(N + 1, 0);
    for (int i = 0; i < N; i++) {
      a = A.at(i);
      mn = max(i - a, 0);
      mx = min(i + a, N - 1);
      B.at(mn)++;
      B.at(mx + 1)--;
    }
    for (int i = 1; i <= N; i++) {
      B.at(i) = B.at(i - 1) + B.at(i);
    }

    B.pop_back();
    if (A == B) {
      break;
    }
    A = B;
  }

  for (int i = 0; i < N; i++) {
    if (i != 0) {
      cout << " ";
    }
    cout << A.at(i);
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int mn, mx, a;
  vector<int> B(N, 0);
  for (int j = 1; j <= min(K, N); j++) {
    for (int i = 0; i < N; i++) {
      a = A.at(i);
      if (a == 0) {
        B.at(i)++;
      } else {
        mn = max(i - a, 0);
        mx = min(i + a, N - 1);
        for (int k = mn; k <= mx; k++) {
          B.at(k)++;
        }
      }
    }
    copy(B.begin(), B.end(), A.begin());
    fill(B.begin(), B.end(), 0);
  }

  for (int i = 0; i < N; i++) {
    if (i != 0) {
      cout << " ";
    }
    cout << A.at(i);
  }
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
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;
}
using ll = long long;
using P = pair<int, int>;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

ll n, k;

int main() {
  cin >> n >> k;
  vector<ll> A(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> A.at(i);
  }

  for (int i = 0; i < min(k, 45LL); ++i) {
    vector<ll> B(400400, 0);
    for (int i = 0; i < n; ++i) {
      ll left = i - A.at(i);
      ll right = i + A.at(i);
      B.at(max(0LL, left))++;
      B.at(right + 1)--;
    }
    for (int i = 0; i < n; ++i) {
      B.at(i + 1) += B.at(i);
    }
    A = B;
  }

  for (int i = 0; i < n; ++i) {
    if (i == n - 1)
      cout << A.at(i) << endl;
    else
      cout << A.at(i) << " ";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
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;
}
using ll = long long;
using P = pair<int, int>;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

ll n, k;

int main() {
  cin >> n >> k;
  vector<ll> A(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> A.at(i);
  }

  while (k--) {
    vector<ll> B(400400, 0);
    for (int i = 0; i < n; ++i) {
      ll left = i - A.at(i);
      ll right = i + A.at(i);
      B.at(max(0LL, left))++;
      B.at(right + 1)--;
    }
    for (int i = 0; i < n; ++i) {
      B.at(i + 1) += B.at(i);
    }
    A = B;
  }

  for (int i = 0; i < n; ++i) {
    if (i == n - 1)
      cout << A.at(i) << endl;
    else
      cout << A.at(i) << " ";
  }
}