prithivMLmods/Coder-Stat huggingface dataset:
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, n) for (int i = s; i < (int)(n); i++)

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, k) {
    vector<ll> imos(n, 0);
    rep(j, 0, n) {
      if (j - a[j] >= 0)
        imos[j - a[j]]++;
      else
        imos[0]++;
      if (j + a[j] + 1 < n)
        imos[j + a[j] + 1]--;
    }
    a[0] = imos[0];
    rep(j, 1, n) a[j] = a[j - 1] + imos[j];
  }
  rep(i, 0, n) {
    cout << a[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << "" << endl;
  }
}

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, k) {
    vector<ll> imos(n, 0);
    rep(j, 0, n) {
      if (j - a[j] >= 0)
        imos[j - a[j]]++;
      else
        imos[0]++;
      if (j + a[j] + 1 < n)
        imos[j + a[j] + 1]--;
    }
    a[0] = imos[0];
    int c = 0;
    if (a[0] == n)
      c++;
    rep(j, 1, n) {
      a[j] = a[j - 1] + imos[j];
      if (a[j] == n)
        c++;
    }
    if (c == n)
      break;
  }
  rep(i, 0, n) {
    cout << a[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << "" << endl;
  }
}
Problem p02647 (C++)

Buggy submission (status: 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 (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;
}


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 (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;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define cvector vector<char>
#define svector vector<string>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k, pos;
  cin >> n >> k;
  lvector A(n);
  rep(i, n) cin >> A[i];
  ll lim = min(50ll, k);
  lvector B(n, 0);
  rep(i, n) {
    pos = max(0ll, i - A[i]);
    B[pos]++;
    if (i + A[i] + 1 < n)
      B[i + A[i] + 1]--;
  }
  rep(i, n - 1) B[i + 1] += B[i];
  rep(i, lim - 1) {
    lvector tmp(n, 0);
    rep(j, n) {
      pos = max(0ll, j - B[j]);
      tmp[pos]++;
      if (i + B[j] + 1 < n)
        tmp[j + B[j] + 1]--;
    }
    rep(j, n - 1) tmp[j + 1] += tmp[j];
    rep(j, n) B[j] = tmp[j];
  }
  rep(i, n - 1) cout << B[i] << ' ';
  print(B[n - 1]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define cvector vector<char>
#define svector vector<string>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k, pos;
  cin >> n >> k;
  lvector A(n);
  rep(i, n) cin >> A[i];
  ll lim = min(50ll, k);
  lvector B(n, 0);
  rep(i, n) {
    pos = max(0ll, i - A[i]);
    B[pos]++;
    if (i + A[i] + 1 < n)
      B[i + A[i] + 1]--;
  }
  rep(i, n - 1) B[i + 1] += B[i];
  rep(i, lim - 1) {
    lvector tmp(n, 0);
    rep(j, n) {
      pos = max(0ll, j - B[j]);
      tmp[pos]++;
      if (j + B[j] + 1 < n)
        tmp[j + B[j] + 1]--;
    }
    rep(j, n - 1) tmp[j + 1] += tmp[j];
    rep(j, n) B[j] = tmp[j];
  }
  rep(i, n - 1) cout << B[i] << ' ';
  print(B[n - 1]);
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

// typedef
// -----------------------------------------------------------------------------
typedef long long ll;
typedef long double ld;

// container util
// -----------------------------------------------------------------------------
#define ALL(c) (c).begin(), (c).end()
#define SZ(c) ((ll)(c).size())
#define MAX(c) *max_element(ALL(c))
#define MIN(c) *min_element(ALL(c))
#define SORT(c) sort((c).begin(), (c).end())

// repetition
// -----------------------------------------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
// -----------------------------------------------------------------------------
const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1.0);

// debug
// -----------------------------------------------------------------------------
#define DUMP(x) cerr << #x << " = " << (x) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  int N, K;
  cin >> N >> K;
  vector<int> A(N + 1, 0);
  FOR(i, 1, N + 1) cin >> A[i];
  REP(i, K) {
    vector<int> tmp(N + 1, 0);
    FOR(i, 1, N + 1) {
      int d = A[i];
      int b = (i - d < 0) ? 0 : i - d;
      tmp[b]++;
      if (i + d + 1 <= N) {
        int e = i + d + 1;
        tmp[e]--;
      }
    }
    A[0] = tmp[0];
    FOR(i, 1, N + 1) { A[i] = A[i - 1] + tmp[i]; }
  }
  FOR(i, 1, N + 1) { cout << A[i] << " "; }
  cout << "\n";
}

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

// typedef
// -----------------------------------------------------------------------------
typedef long long ll;
typedef long double ld;

// container util
// -----------------------------------------------------------------------------
#define ALL(c) (c).begin(), (c).end()
#define SZ(c) ((ll)(c).size())
#define MAX(c) *max_element(ALL(c))
#define MIN(c) *min_element(ALL(c))
#define SORT(c) sort((c).begin(), (c).end())

// repetition
// -----------------------------------------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
// -----------------------------------------------------------------------------
const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1.0);

// debug
// -----------------------------------------------------------------------------
#define DUMP(x) cerr << #x << " = " << (x) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  int N, K;
  cin >> N >> K;
  K = min(K, 100);
  vector<int> A(N + 1, 0);
  FOR(i, 1, N + 1) cin >> A[i];
  REP(i, K) {
    vector<int> tmp(N + 1, 0);
    FOR(i, 1, N + 1) {
      int d = A[i];
      int b = (i - d < 0) ? 0 : i - d;
      tmp[b]++;
      if (i + d + 1 <= N) {
        int e = i + d + 1;
        tmp[e]--;
      }
    }
    A[0] = tmp[0];
    FOR(i, 1, N + 1) { A[i] = A[i - 1] + tmp[i]; }
  }
  FOR(i, 1, N + 1) { cout << A[i] << " "; }
  cout << "\n";
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  auto upd = [](const vector<int> &v) {
    vector<int> imos((int)v.size() + 2);
    for (int i = 1; i <= (int)v.size(); i++) {
      int l = max(0, i - v[i - 1]);
      int r = min((int)v.size() + 2, i + v[i - 1] + 1);
      imos[l]++;
      imos[r]--;
    }
    for (int i = 1; i < (int)imos.size(); i++) {
      imos[i] += imos[i - 1];
    }
    vector<int> res((int)v.size());
    for (int i = 1; i <= (int)v.size(); i++) {
      res[i - 1] = imos[i];
    }
    return res;
  };
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < min(50, k); i++) {
    a = upd(a);
  }
  for (int i = 0; i < n; i++) {
    if (i > 0)
      cout << " ";
    cout << a[i];
  }
  cout << endl;
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  auto upd = [](const vector<int> &v) {
    vector<int> imos((int)v.size() + 2);
    for (int i = 1; i <= (int)v.size(); i++) {
      int l = max(0, i - v[i - 1]);
      int r = min((int)v.size() + 1, i + v[i - 1] + 1);
      imos[l]++;
      imos[r]--;
    }
    for (int i = 1; i < (int)imos.size(); i++) {
      imos[i] += imos[i - 1];
    }
    vector<int> res((int)v.size());
    for (int i = 1; i <= (int)v.size(); i++) {
      res[i - 1] = imos[i];
    }
    return res;
  };
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < min(50, k); i++) {
    a = upd(a);
  }
  for (int i = 0; i < n; i++) {
    if (i > 0)
      cout << " ";
    cout << a[i];
  }
  cout << endl;
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <memory.h>
using namespace std;

typedef long long ll;
ll bulb[300010];
ll bkt[300010];

ll operate(int n) {
  memset(bkt, 0, sizeof bkt);
  for (int i = 0; i < n; ++i) {
    ll op1 = i - bulb[i];
    ll op2 = i + bulb[i];
    if (op1 < 0)
      op1 = 0;
    ++bkt[op1];
    if (op2 >= n)
      op2 = n - 1;
    --bkt[op2 + 1];
  }
  bulb[0] = bkt[0];
  for (int i = 1; i < n; ++i)
    bulb[i] = bulb[i - 1] + bkt[i];
  return *min_element(bulb, bulb + n);
}

int main() {
  int n, k, i;
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; ++i)
    scanf("%lld", bulb + i);
  for (i = 0; i < k; ++i)
    if (operate(n) > n)
      break;
  for (int i = 0; i < n; ++i)
    printf("%lld%c", bulb[i], " \n"[i == n - 1]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <memory.h>
using namespace std;

typedef long long ll;
ll bulb[300010];
ll bkt[300010];

ll operate(int n) {
  memset(bkt, 0, sizeof bkt);
  for (int i = 0; i < n; ++i) {
    ll op1 = i - bulb[i];
    ll op2 = i + bulb[i];
    if (op1 < 0)
      op1 = 0;
    ++bkt[op1];
    if (op2 >= n)
      op2 = n - 1;
    --bkt[op2 + 1];
  }
  bulb[0] = bkt[0];
  for (int i = 1; i < n; ++i)
    bulb[i] = bulb[i - 1] + bkt[i];
  return *min_element(bulb, bulb + n);
}

int main() {
  int n, k, i;
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; ++i)
    scanf("%lld", bulb + i);
  for (i = 0; i < k; ++i)
    if (operate(n) >= n)
      break;
  for (int i = 0; i < n; ++i)
    printf("%lld%c", bulb[i], " \n"[i == n - 1]);
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repA(i, n) for (int i = n; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (ll)(a.size())
typedef long long ll;
typedef long double ld;
const ll mod = 998244353; // 1e9+7

int solve() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  while (k--) {
    vector<int> b(n + 1, 0);
    rep(i, n) {
      int from = max(0, i - a[i]);
      int to = min(n - 1, i + a[i]);
      b[from]++;
      b[to + 1]--;
    }
    bool changed = false;
    rep(i, n) {
      changed |= (a[i] != b[i]);
      a[i] = b[i];
      b[i + 1] += b[i];
    }
    if (!changed)
      break;
  }
  rep(i, n) cout << a[i] << ' ';
  cout << '\n';
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ll t = 1;
  //  cin>>t;
  rep(i, t) {
    solve();
    //  cout<<'\n';
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repA(i, n) for (int i = n; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (ll)(a.size())
typedef long long ll;
typedef long double ld;
const ll mod = 998244353; // 1e9+7

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  while (k--) {
    vector<int> b(n + 1, 0);
    rep(i, n) {
      int from = max(0, i - a[i]);
      int to = min(n - 1, i + a[i]);
      b[from]++;
      b[to + 1]--;
    }
    bool changed = false;
    rep(i, n) {
      changed |= (a[i] != b[i]);
      a[i] = b[i];
      b[i + 1] += b[i];
    }
    if (!changed)
      break;
  }
  rep(i, n) cout << a[i] << ' ';
  cout << '\n';
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ll t = 1;
  //  cin>>t;
  rep(i, t) {
    solve();
    //  cout<<'\n';
  }
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vi = vector<int>;
#define _GLIBCXX_DEBUG
#define IO_STREAM                                                              \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define all(x) x.begin(), x.end()
#define rep(i, sta, end) for (ll i = sta; i < end; ++i)
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define pb push_back
#define INF 1000000000000000LL
const ll MOD = 1000000007;
const double PI = acos(-1);
//||
#define DBG(a, b, c, d) // cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<endl
// DBG("","","","");

signed main() {
  IO_STREAM;
  int N, K;
  cin >> N >> K;
  vi A(N);
  rep(i, 0, N) { cin >> A[i]; }
  rep(cnt, 0, K) {
    vi B(N + 1);
    rep(i, 0, N) {
      int st, ed;
      st = max((int)(i - A[i]), 0);
      ed = min((int)(i + A[i] + 1), N);
      B[st]++;
      B[ed]--;
    }
    rep(i, 0, N) { B[i + 1] += B[i]; }
    //		if(A==B) break;
    A = B;
    B[N] = 0;
  }
  rep(i, 0, N) { cout << A[i] << ' '; }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vi = vector<int>;
#define _GLIBCXX_DEBUG
#define IO_STREAM                                                              \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define all(x) x.begin(), x.end()
#define rep(i, sta, end) for (ll i = sta; i < end; ++i)
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define pb push_back
#define INF 1000000000000000LL
const ll MOD = 1000000007;
const double PI = acos(-1);
//||
#define DBG(a, b, c, d) // cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<endl
// DBG("","","","");

signed main() {
  IO_STREAM;
  int N, K;
  cin >> N >> K;
  vi A(N);
  rep(i, 0, N) { cin >> A[i]; }
  rep(cnt, 0, K) {
    vi B(N + 1);
    rep(i, 0, N) {
      int st, ed;
      st = max((int)(i - A[i]), 0);
      ed = min((int)(i + A[i] + 1), N);
      B[st]++;
      B[ed]--;
    }
    rep(i, 0, N) { B[i + 1] += B[i]; }
    if (A == B)
      break;
    A = B;
    B[N] = 0;
  }
  rep(i, 0, N) { cout << A[i] << ' '; }
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, k;
int a[200005];
int sum[200005];

int main(void) {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i < k; i++) {
    memset(sum, 0, sizeof(sum));
    bool flag = true;
    for (int j = 0; j < n; j++) {
      if (a[j] != n) {
        flag = false;
      }
    }
    for (int j = 0; j < n; j++) {
      sum[max(0, j - a[j])]++;
      sum[min(n, j + a[j] + 1)]--;
    }
    for (int j = 0; j < n; j++) {
      sum[j + 1] += sum[j];
      a[j] = sum[j];
    }
  }
  for (int i = 0; i < n; i++) {
    printf("%d%c", a[i], i + 1 == n ? '\n' : ' ');
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, k;
int a[200005];
int sum[200005];

int main(void) {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i < k; i++) {
    memset(sum, 0, sizeof(sum));
    bool flag = true;
    for (int j = 0; j < n; j++) {
      if (a[j] != n) {
        flag = false;
      }
    }
    if (flag)
      break;
    for (int j = 0; j < n; j++) {
      sum[max(0, j - a[j])]++;
      sum[min(n, j + a[j] + 1)]--;
    }
    for (int j = 0; j < n; j++) {
      sum[j + 1] += sum[j];
      a[j] = sum[j];
    }
  }
  for (int i = 0; i < n; i++) {
    printf("%d%c", a[i], i + 1 == n ? '\n' : ' ');
  }
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#define re register
const int MAXN = 2e5 + 10;
int n, K, s[MAXN], a[MAXN], timee;
char fin[10010], *p1 = fin, *p2 = fin;
char getc() {
  return p1 == p2 && (p2 = (p1 = fin) + fread(fin, 1, 10000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
int read() {
  re int x = 0;
  re char ch = 0;
  while (ch < '0' || ch > '9')
    ch = getc();
  while ('0' <= ch && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getc();
  return x;
}
inline void insert(int x, int w) {
  for (; x <= n; x += x & -x)
    s[x] += w;
}
inline int query(int x) {
  re int res = 0;
  for (; x; x -= x & -x)
    res += s[x];
  return res;
}
int main() {
  freopen("t2.in", "r", stdin);
  // freopen("t2.out","w",stdout);
  n = read();
  K = read();
  for (re int i = 1; i <= n; i++)
    a[i] = read();
  while (K--) {
    re bool flag = 1;
    memset(s, 0, (n + 5) * sizeof(int));
    for (re int i = 1; i <= n; i++)
      insert(std::max(1, i - a[i]), 1), insert(std::min(n, i + a[i]) + 1, -1);
    for (re int i = 1; i <= n; i++)
      a[i] = query(i), flag &= (a[i] == n);
    if (flag)
      break;
  }
  for (re int i = 1; i <= n; i++)
    printf("%d ", a[i]);
  printf("\n");
  return 0;
}
/*
5 1
1 0 0 1 0
*/

Fixed submission:
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#define re register
const int MAXN = 2e5 + 10;
int n, K, s[MAXN], a[MAXN], timee;
char fin[10010], *p1 = fin, *p2 = fin;
char getc() {
  return p1 == p2 && (p2 = (p1 = fin) + fread(fin, 1, 10000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
int read() {
  re int x = 0;
  re char ch = 0;
  while (ch < '0' || ch > '9')
    ch = getc();
  while ('0' <= ch && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getc();
  return x;
}
inline void insert(int x, int w) {
  for (; x <= n; x += x & -x)
    s[x] += w;
}
inline int query(int x) {
  re int res = 0;
  for (; x; x -= x & -x)
    res += s[x];
  return res;
}
int main() {
  // freopen("t2.out","w",stdout);
  n = read();
  K = read();
  for (re int i = 1; i <= n; i++)
    a[i] = read();
  while (K--) {
    re bool flag = 1;
    memset(s, 0, (n + 5) * sizeof(int));
    for (re int i = 1; i <= n; i++)
      insert(std::max(1, i - a[i]), 1), insert(std::min(n, i + a[i]) + 1, -1);
    for (re int i = 1; i <= n; i++)
      a[i] = query(i), flag &= (a[i] == n);
    if (flag)
      break;
  }
  for (re int i = 1; i <= n; i++)
    printf("%d ", a[i]);
  printf("\n");
  return 0;
}
/*
5 1
1 0 0 1 0
*/
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#define re register
const int MAXN = 2e5 + 10;
int n, K, s[MAXN], a[MAXN], timee;
char fin[10010], *p1 = fin, *p2 = fin;
char getc() {
  return p1 == p2 && (p2 = (p1 = fin) + fread(fin, 1, 10000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
int read() {
  re int x = 0;
  re char ch = 0;
  while (ch < '0' || ch > '9')
    ch = getc();
  while ('0' <= ch && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getc();
  return x;
}
inline void insert(int x, int w) {
  for (; x <= n; x += x & -x)
    s[x] += w;
}
inline int query(int x) {
  re int res = 0;
  for (; x; x -= x & -x)
    res += s[x];
  return res;
}
int main() {
  freopen("t2.in", "r", stdin);
  // freopen("t2.out","w",stdout);
  n = read();
  K = read();
  for (re int i = 1; i <= n; i++)
    a[i] = read();
  if (K >= 41) {
    for (re int i = 1; i <= n; i++)
      printf("%d ", n);
    return 0;
  }
  while (K--) {
    re bool flag = 1;
    memset(s, 0, (n + 5) * sizeof(int));
    for (re int i = 1; i <= n; i++)
      insert(std::max(1, i - a[i]), 1), insert(std::min(n, i + a[i]) + 1, -1);
    for (re int i = 1; i <= n; i++)
      a[i] = query(i), flag &= (a[i] == n);
    if (flag)
      break;
  }
  for (re int i = 1; i <= n; i++)
    printf("%d ", a[i]);
  printf("\n");
  return 0;
}
/*
5 1
1 0 0 1 0
*/

Fixed submission:
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#define re register
const int MAXN = 2e5 + 10;
int n, K, s[MAXN], a[MAXN], timee;
char fin[10010], *p1 = fin, *p2 = fin;
char getc() {
  return p1 == p2 && (p2 = (p1 = fin) + fread(fin, 1, 10000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
int read() {
  re int x = 0;
  re char ch = 0;
  while (ch < '0' || ch > '9')
    ch = getc();
  while ('0' <= ch && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getc();
  return x;
}
inline void insert(int x, int w) {
  for (; x <= n; x += x & -x)
    s[x] += w;
}
inline int query(int x) {
  re int res = 0;
  for (; x; x -= x & -x)
    res += s[x];
  return res;
}
int main() {
  // freopen("t2.out","w",stdout);
  n = read();
  K = read();
  for (re int i = 1; i <= n; i++)
    a[i] = read();
  if (K >= 41) {
    for (re int i = 1; i <= n; i++)
      printf("%d ", n);
    return 0;
  }
  while (K--) {
    re bool flag = 1;
    memset(s, 0, (n + 5) * sizeof(int));
    for (re int i = 1; i <= n; i++)
      insert(std::max(1, i - a[i]), 1), insert(std::min(n, i + a[i]) + 1, -1);
    for (re int i = 1; i <= n; i++)
      a[i] = query(i), flag &= (a[i] == n);
    if (flag)
      break;
  }
  for (re int i = 1; i <= n; i++)
    printf("%d ", a[i]);
  printf("\n");
  return 0;
}
/*
5 1
1 0 0 1 0
*/
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <string>
typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define len(s) s.length()
#define all(v) v.begin(), v.end()
const ll INF = LONG_MAX;
const int N = 2e5 + 5;
const ll mod = 1000000007;
using namespace std;

int a[N], b[N];
int n;
void solve() {
  int i;
  for (i = 0; i <= n; i++)
    b[i] = 0;

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

    b[l]++;
    b[r]--;
  }

  for (i = 1; i <= n; i++)
    b[i] += b[i - 1];

  for (i = 0; i < n; i++) {
    a[i] = b[i];
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);

  ll tc = 1;
  // cin >> tc;

  while (tc--) {

    int m, k, i, j;
    int x, y;

    cin >> n >> k;

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

    int ok = 0;

    // for (i = 0; i < n; i++) {
    // 	// printf("%d ", a[i]);
    // 	cout << a[i] << " ";
    // }
    // cout << endl;

    while (k--) {
      for (i = 0; i < n; i++) {
        ok &= (a[i] == n);
      }

      if (ok)
        break;

      solve();
    }

    for (i = 0; i < n; i++) {
      printf("%d ", a[i]);
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <string>
typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define len(s) s.length()
#define all(v) v.begin(), v.end()
const ll INF = LONG_MAX;
const int N = 2e5 + 5;
const ll mod = 1000000007;
using namespace std;

int a[N], b[N];
int n;
void solve() {
  int i;
  for (i = 0; i <= n; i++)
    b[i] = 0;

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

    b[l]++;
    b[r]--;
  }

  for (i = 1; i <= n; i++)
    b[i] += b[i - 1];

  for (i = 0; i < n; i++) {
    a[i] = b[i];
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);

  ll tc = 1;
  // cin >> tc;

  while (tc--) {

    int m, k, i, j;
    int x, y;

    cin >> n >> k;

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

    int ok = 0;

    // for (i = 0; i < n; i++) {
    // 	// printf("%d ", a[i]);
    // 	cout << a[i] << " ";
    // }
    // cout << endl;

    while (k--) {
      ok = 1;
      for (i = 0; i < n; i++) {
        ok &= (a[i] == n);
      }

      if (ok)
        break;

      solve();
    }

    for (i = 0; i < n; i++) {
      printf("%d ", a[i]);
    }
  }
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

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

  for (int i = 0; i < k; ++i) {
    vector<int> tmp(n, 0);
    for (int j = 0; j < n; ++j) {
      int l = max(0, j - a[j]);
      int r = j + a[j] + 1;
      tmp[l] += 1;
      if (n > r)
        tmp[r] -= 1;
    }
    for (int i = 0; i < n - 1; ++i) {
      a[i] = tmp[i];
      tmp[i + 1] += tmp[i];
    }
    a[n - 1] = tmp[n - 1];
  }
  for (auto &&e : a) {
    cout << e << " ";
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

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

  for (int i = 0; i < min(k, 100); ++i) {
    vector<int> tmp(n, 0);
    for (int j = 0; j < n; ++j) {
      int l = max(0, j - a[j]);
      int r = j + a[j] + 1;
      tmp[l] += 1;
      if (n > r)
        tmp[r] -= 1;
    }
    for (int i = 0; i < n - 1; ++i) {
      a[i] = tmp[i];
      tmp[i + 1] += tmp[i];
    }
    a[n - 1] = tmp[n - 1];
  }
  for (auto &&e : a) {
    cout << e << " ";
  }
  cout << endl;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, n, m) for (int i = n; i < m; i++)
#define elif else if
#define INF 1000000007
#define pi 3.141592653589793
using namespace std;
int saidaikouyakusuu(int a, int b) {
  if (b == 0)
    return a;
  return saidaikouyakusuu(b, a % b);
}
int saisyoukoubaisuu(int a, int b) { return a * b / saidaikouyakusuu(a, b); }
int n, m, cnt = 0, aa[220000], ab[220000];
pair<int, int> pp[220000];
string s, ss[220000];
char c, ca;
queue<int> que;
signed main() {
  cin >> n >> m;
  rep(i, n) {
    cin >> aa[i];
    ab[max((long long)0, i - aa[i])]++;
    ab[min(n, i + aa[i] + 1)]--;
  }
  rep(i, min(m, n - 1)) {
    rep(j, n) {
      aa[j] = ab[j] + aa[j - 1];
      ab[j] = 0;
    }
    rep(j, n) {
      ab[max((long long)0, j - aa[j])]++;
      ab[min(n, j + aa[j] + 1)]--;
    }
  }
  rep(i, n) { cout << aa[i] << ' '; }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, n, m) for (int i = n; i < m; i++)
#define elif else if
#define INF 1000000007
#define pi 3.141592653589793
using namespace std;
int saidaikouyakusuu(int a, int b) {
  if (b == 0)
    return a;
  return saidaikouyakusuu(b, a % b);
}
int saisyoukoubaisuu(int a, int b) { return a * b / saidaikouyakusuu(a, b); }
int n, m, cnt = 0, aa[220000], ab[220000];
pair<int, int> pp[220000];
string s, ss[220000];
char c, ca;
queue<int> que;
signed main() {
  cin >> n >> m;
  rep(i, n) {
    cin >> aa[i];
    ab[max((long long)0, i - aa[i])]++;
    ab[min(n, i + aa[i] + 1)]--;
  }
  rep(i, min(m, (long long)100)) {
    rep(j, n) {
      aa[j] = ab[j] + aa[j - 1];
      ab[j] = 0;
    }
    rep(j, n) {
      ab[max((long long)0, j - aa[j])]++;
      ab[min(n, j + aa[j] + 1)]--;
    }
  }
  rep(i, n) { cout << aa[i] << ' '; }
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

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

static const ll INF = 1e15;

static const ll mod = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> prev(n);
  rep(i, n) cin >> prev[i];
  vector<ll> current(n);
  rep(_, k) {
    vector<ll> sums(n + 1, 0);
    const auto &source = prev;
    for (int i = 0; i < n; i++) {
      auto t = source[i];
      auto front = max(0LL, i - t);
      auto back = min(n, i + t + 1);
      sums[front]++;
      sums[back]--;
    }
    // cout << "back array" << endl;
    // rep(i, n + 1) {
    //     cout << sums[i] << ",";
    // }
    // cout << endl;
    ll s = 0;
    // cout << "current" << endl;
    rep(i, n) {
      s += sums[i];
      // cout << s << ",";
      current[i] = s;
    }
    bool finish = true;
    rep(i, n) {
      if (current[i] != n) {
        finish = false;
        break;
      }
    }
    swap(prev, current);
  }
  rep(i, n) { cout << prev[i] << " "; }
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

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

static const ll INF = 1e15;

static const ll mod = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> prev(n);
  rep(i, n) cin >> prev[i];
  vector<ll> current(n);
  rep(_, k) {
    vector<ll> sums(n + 1, 0);
    const auto &source = prev;
    for (int i = 0; i < n; i++) {
      auto t = source[i];
      auto front = max(0LL, i - t);
      auto back = min(n, i + t + 1);
      sums[front]++;
      sums[back]--;
    }
    // cout << "back array" << endl;
    // rep(i, n + 1) {
    //     cout << sums[i] << ",";
    // }
    // cout << endl;
    ll s = 0;
    // cout << "current" << endl;
    rep(i, n) {
      s += sums[i];
      // cout << s << ",";
      current[i] = s;
    }
    bool finish = true;
    rep(i, n) {
      if (current[i] != n) {
        finish = false;
        break;
      }
    }
    swap(prev, current);
    if (finish)
      break;
  }
  rep(i, n) { cout << prev[i] << " "; }
  cout << endl;
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 200010;
int n, m, minn = 1 << 30;
int a[N], sum[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int k = 1; k <= n; k++)
    scanf("%d", &a[k]), minn = min(minn, a[k]);
  for (int k = 1; k <= m && minn < n; k++) {
    memset(sum, 0, sizeof sum);
    for (int i = 1; i <= n; i++) {
      sum[i - a[i]]++;
      sum[i + a[i] + 1]--;
    }
    for (int i = 1; i <= n; i++)
      sum[i] += sum[i - 1];
    minn = 1 << 30;
    for (int i = 1; i <= n; i++)
      a[i] = sum[i], minn = min(minn, a[i]);
  }
  for (int k = 1; k <= n; k++)
    printf("%d ", a[k]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 200010;
int n, m, minn = 1 << 30;
int a[N], sum[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int k = 1; k <= n; k++)
    scanf("%d", &a[k]), minn = min(minn, a[k]);
  for (int k = 1; k <= m && minn < n; k++) {
    memset(sum, 0, sizeof sum);
    for (int i = 1; i <= n; i++) {
      sum[max(1, i - a[i])]++;
      sum[min(i + a[i] + 1, n + 1)]--;
    }
    for (int i = 1; i <= n; i++)
      sum[i] += sum[i - 1];
    minn = 1 << 30;
    for (int i = 1; i <= n; i++)
      a[i] = sum[i], minn = min(minn, a[i]);
  }
  for (int k = 1; k <= n; k++)
    printf("%d ", a[k]);
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define all(v) v.begin(), v.end()
// c
int main() {

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

  rep(i, k) {

    vector<int> memo(n + 1);

    rep(j, n) {
      int min, max;
      min = j - a[i][j];
      max = j + 1 + a[i][j];
      if (min < 0)
        min = 0;
      if (max > n)
        max = n;
      memo[min]++;
      memo[max]--;
    }

    int cnt = 0;

    rep(j, n) {
      cnt += memo[j];
      a[i + 1][j] = cnt;
    }

    bool fin = true;

    rep(j, n) {
      if (a[i][j] != a[i + 1][j])
        fin = false;
    }

    if (fin) {
      k = i + 1;
      break;
    }
  }

  rep(j, n - 1) { cout << a[k][j] << " "; }
  cout << a[k][n - 1] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define all(v) v.begin(), v.end()
// c
int main() {

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

  rep(i, k) {

    vector<int> memo(n + 1);

    rep(j, n) {
      int min, max;
      min = j - a[i][j];
      max = j + 1 + a[i][j];
      if (min < 0)
        min = 0;
      if (max > n)
        max = n;
      memo[min]++;
      memo[max]--;
    }

    int cnt = 0;

    rep(j, n) {
      cnt += memo[j];
      a[i + 1][j] = cnt;
    }

    bool fin = true;

    rep(j, n) {
      if (a[i][j] != a[i + 1][j])
        fin = false;
    }

    if (fin) {
      k = i + 1;
      break;
    }
  }

  rep(j, n - 1) { cout << a[k][j] << " "; }
  cout << a[k][n - 1] << endl;

  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
#define INF (1e9)

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

  while (K--) {
    vi B(N + 1);
    for (int i = 0; i < N; i++) {
      B[max(0, i - A[i])]++;
      B[min(N, i + A[i]) + 1]--;
    }
    int s = 0;
    bool f = true;
    for (int i = 0; i < N; i++) {
      s += B[i];
      A[i] = s;
      f &= s == N;
    }
    if (f)
      break;
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
#define INF (1e9)

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

  while (K--) {
    vi B(N + 1);
    for (int i = 0; i < N; i++) {
      B[max(0, i - A[i])]++;
      B[min(N, i + A[i] + 1)]--;
    }
    int s = 0;
    bool f = true;
    for (int i = 0; i < N; i++) {
      s += B[i];
      A[i] = s;
      f &= s == N;
    }
    if (f)
      break;
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
#define N 200005
int a[N], d[N];

int check(int n) {
  int i;
  for (i = 1; i <= n; i++) {
    if (i != n)
      return 0;
  }
  return 1;
}

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

  while (k--) {
    memset(d, 0, sizeof(d));
    for (i = 1; i <= n; i++) {
      l = i - a[i];
      r = i + a[i];
      if (l < 1)
        l = 1;
      if (r > n)
        r = n;
      d[r + 1] -= 1;
      d[l] += 1;
    }
    a[1] = d[1];
    for (i = 2; i <= n; i++) {
      d[i] += d[i - 1];
      a[i] = d[i];
    }
    if (check(n)) {
      for (i = 1; i <= n; i++) {
        cout << n << " ";
      }
      return 0;
    }
  }
  for (i = 1; i <= n; i++) {
    cout << a[i] << " ";
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
#define N 200005
int a[N], d[N];

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

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

  while (k--) {
    memset(d, 0, sizeof(d));
    for (i = 1; i <= n; i++) {
      l = i - a[i];
      r = i + a[i];
      if (l < 1)
        l = 1;
      if (r > n)
        r = n;
      d[r + 1] -= 1;
      d[l] += 1;
    }
    a[1] = d[1];
    for (i = 2; i <= n; i++) {
      d[i] += d[i - 1];
      a[i] = d[i];
    }
    if (check(n)) {
      for (i = 1; i <= n; i++) {
        cout << n << " ";
      }
      return 0;
    }
  }
  for (i = 1; i <= n; i++) {
    cout << a[i] << " ";
  }
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int A[100100], B[100100];
priority_queue<int, vector<int>, greater<int>> PQ1;
priority_queue<int, vector<int>, less<int>> PQ2;
int main() {
  int N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  while (K--) {
    while (!PQ1.empty())
      PQ1.pop();
    for (int i = 0; i < N; i++) {
      PQ1.push(i + A[i]);
      while (PQ1.top() < i)
        PQ1.pop();
      B[i] = (int)(PQ1.size());
    }
    while (!PQ2.empty())
      PQ2.pop();
    for (int i = N - 1; i >= 0; i--) {
      PQ2.push(i - A[i]);
      while (PQ2.top() > i)
        PQ2.pop();
      B[i] += (int)(PQ2.size());
      B[i]--;
    }
    bool OK = true;
    for (int i = 0; i < N; i++) {
      if (B[i] != N)
        OK = false;
      A[i] = B[i];
    }
    if (OK)
      break;
  }
  for (int i = 0; i < N; i++)
    cout << A[i] << " ";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int A[200100], B[200100];
priority_queue<int, vector<int>, greater<int>> PQ1;
priority_queue<int, vector<int>, less<int>> PQ2;
int main() {
  int N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  while (K--) {
    while (!PQ1.empty())
      PQ1.pop();
    for (int i = 0; i < N; i++) {
      PQ1.push(i + A[i]);
      while (PQ1.top() < i)
        PQ1.pop();
      B[i] = (int)(PQ1.size());
    }
    while (!PQ2.empty())
      PQ2.pop();
    for (int i = N - 1; i >= 0; i--) {
      PQ2.push(i - A[i]);
      while (PQ2.top() > i)
        PQ2.pop();
      B[i] += (int)(PQ2.size());
      B[i]--;
    }
    bool OK = true;
    for (int i = 0; i < N; i++) {
      if (B[i] != N)
        OK = false;
      A[i] = B[i];
    }
    if (OK)
      break;
  }
  for (int i = 0; i < N; i++)
    cout << A[i] << " ";
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<ll, ll, ll> tl3;

const int BIG_NUM = 1e9;
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;

const int MAX = 1e5 + 5;

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);
    for (int j = 0; j < n; j++) {
      int l = max(0, j - a[j]);
      int r = min(n - 1, i + a[i]);
      b[l]++;
      if (r + 1 < n) {
        b[r + 1]--;
      }
    }
    for (int j = 1; j < n; j++) {
      b[j] += b[j - 1];
    }

    bool isEnd = true;
    for (int j = 0; j < n; j++) {
      if (a[j] != n) {
        isEnd = false;
        break;
      }
    }

    if (isEnd) {
      for (int j = 0; j < n; j++) {
        cout << n << " ";
      }
      cout << endl;
      return 0;
    }
    a = b;
  }

  for (int ai : a) {
    cout << ai << " ";
  }

  cout << endl;
}

Fixed submission:
#define _USE_MATH_DEFINES

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<ll, ll, ll> tl3;

const int BIG_NUM = 1e9;
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;

const int MAX = 1e5 + 5;

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);
    for (int j = 0; j < n; j++) {
      int l = max(0, j - a[j]);
      int r = min(n - 1, j + a[j]);
      b[l]++;
      if (r + 1 < n) {
        b[r + 1]--;
      }
    }
    for (int j = 1; j < n; j++) {
      b[j] += b[j - 1];
    }

    bool isEnd = true;
    for (int j = 0; j < n; j++) {
      if (a[j] != n) {
        isEnd = false;
        break;
      }
    }

    if (isEnd) {
      for (int j = 0; j < n; j++) {
        cout << n << " ";
      }
      cout << endl;
      return 0;
    }
    a = b;
  }

  for (int ai : a) {
    cout << ai << " ";
  }

  cout << endl;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define mod 1e9 + 7

int main()

{

  int n, k;

  cin >> n >> k;

  int a[n + 1], b[n + 1];

  for (int i = 1; i <= n; i++)

  {

    cin >> a[i];
  }

  while (k--)

  {

    memset(b, 0, sizeof(b));

    for (int i = 1; i <= n; i++)

    {

      if (a[i] + i < n)

      {

        b[i]++;

        b[i + a[i] + 1]--;

      }

      else

      {

        b[i]++;
      }

      if (i - a[i] >= 1)

      {

        b[i - a[i]]++;

        b[i]--;

      }

      else

      {

        b[1]++;

        b[i]--;
      }
    }

    a[1] = b[1];

    for (int i = 2; i <= n; i++)

    {

      a[i] = a[i - 1] + b[i];
    }
  }

  for (int i = 1; i <= n; i++)

  {

    cout << a[i] << " ";
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define mod 1e9 + 7

int main()

{

  int n, k;

  cin >> n >> k;

  int a[n + 1], b[n + 1];

  for (int i = 1; i <= n; i++)

  {

    cin >> a[i];
  }

  while (k--)

  {

    memset(b, 0, sizeof(b));

    for (int i = 1; i <= n; i++)

    {

      if (a[i] + i < n)

      {

        b[i]++;

        b[i + a[i] + 1]--;

      }

      else

      {

        b[i]++;
      }

      if (i - a[i] >= 1)

      {

        b[i - a[i]]++;

        b[i]--;

      }

      else

      {

        b[1]++;

        b[i]--;
      }
    }

    a[1] = b[1];

    for (int i = 2; i <= n; i++)

    {

      a[i] = a[i - 1] + b[i];
    }

    int flag = 0;

    for (int i = 1; i <= n; i++)

    {

      if (a[i] != n)

      {

        flag++;

        break;
      }
    }

    if (!flag)

    {

      break;
    }
  }

  for (int i = 1; i <= n; i++)

  {

    cout << a[i] << " ";
  }

  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[200005], b[200005];
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < k; ++i) {
    memset(b, 0, sizeof(b));
    for (int i = 0; i < n; ++i) {
      int l = max(0, i - a[i]);
      int r = min(n - 1, i + a[i]);
      b[l]++;
      if (r + 1 < n) {
        b[r + 1]--;
      }
    }
    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 << a[i] << ' ';
  }
  cout << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[200005], b[200005];
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  if (k > 50) {
    for (int i = 0; i < n; ++i) {
      cout << n << ' ';
    }
    cout << '\n';
    return 0;
  }
  for (int i = 0; i < k; ++i) {
    memset(b, 0, sizeof(b));
    for (int i = 0; i < n; ++i) {
      int l = max(0, i - a[i]);
      int r = min(n - 1, i + a[i]);
      b[l]++;
      if (r + 1 < n) {
        b[r + 1]--;
      }
    }
    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 << a[i] << ' ';
  }
  cout << '\n';
  return 0;
}