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;
typedef pair<int, int> P;
#define SORT(a) sort((a).begin(), (a).end())
#define rSORT(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// Write From this Line

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

  k = min(1000, k);
  rep(j, k) {
    vector<int> l(n), r(n);
    priority_queue<int, vector<int>, greater<int>> que;
    int L = 0, R = 0;
    rep(i, n) {
      while (que.size() && que.top() < (i + 1)) {
        L--;
        que.pop();
      }
      l[i] = L;
      L++;
      que.push((i + 1) + a[i]);
    }

    priority_queue<int> que2;
    for (int i = n - 1; i >= 0; i--) {
      while (que2.size() && que2.top() > (i + 1)) {
        R--;
        que2.pop();
      }
      r[i] = R;
      R++;
      que2.push((i + 1) - a[i]);
    }
    rep(i, n) { a[i] = l[i] + r[i] + 1; }
  }
  for (auto x : a) {
    cout << x << " ";
  }
  cout << endl;
}


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

using namespace std;
using ll = long long;
typedef pair<int, int> P;
#define SORT(a) sort((a).begin(), (a).end())
#define rSORT(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// Write From this Line

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

  k = min(50, k);
  rep(j, k) {
    vector<int> l(n), r(n);
    priority_queue<int, vector<int>, greater<int>> que;
    int L = 0, R = 0;
    rep(i, n) {
      while (que.size() && que.top() < (i + 1)) {
        L--;
        que.pop();
      }
      l[i] = L;
      L++;
      que.push((i + 1) + a[i]);
    }

    priority_queue<int> que2;
    for (int i = n - 1; i >= 0; i--) {
      while (que2.size() && que2.top() > (i + 1)) {
        R--;
        que2.pop();
      }
      r[i] = R;
      R++;
      que2.push((i + 1) - a[i]);
    }
    rep(i, n) { a[i] = l[i] + r[i] + 1; }
  }
  for (auto x : a) {
    cout << x << " ";
  }
  cout << endl;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

using ll = long long;

ll N, K;
vector<ll> A;
vector<ll> B;

int main() {
  cin >> N >> K;
  A.resize(N);
  B.resize(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll logN = log2(N) + 100000;
  for (ll i = 0; i < min(K, logN); i++) {
    fill(B.begin(), B.end(), 0LL);
    for (ll j = 0; j < N; j++) {
      B[max(j - A[j], 0LL)]++;
      if (j + A[j] < N - 1)
        B[j + A[j] + 1]--;
    }
    for (ll j = 0; j < N; j++) {
      A[j] = B[j];
      B[j + 1] += B[j];
    }
  }
  for (ll i = 0; i < N; i++) {
    cout << B[i];
    if (i == N - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

using ll = long long;

ll N, K;
vector<ll> A;
vector<ll> B;

int main() {
  cin >> N >> K;
  A.resize(N);
  B.resize(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll logN = log2(N) + 100;
  for (ll i = 0; i < min(K, logN); i++) {
    fill(B.begin(), B.end(), 0LL);
    for (ll j = 0; j < N; j++) {
      B[max(j - A[j], 0LL)]++;
      if (j + A[j] < N - 1)
        B[j + A[j] + 1]--;
    }
    for (ll j = 0; j < N; j++) {
      A[j] = B[j];
      B[j + 1] += B[j];
    }
  }
  for (ll i = 0; i < N; i++) {
    cout << B[i];
    if (i == N - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int x[200010], s[200010];
int main() {
  freopen("input.txt", "r", stdin);
  int a, b;
  scanf("%d%d", &a, &b);
  for (int i = 1; i <= a; i++)
    scanf("%d", &x[i]);

  for (int j = 1; j <= 50 && j <= b; j++) {
    for (int i = 1; i <= a; i++) {
      int L = i - x[i];
      int R = i + x[i];
      if (L < 1)
        L = 1;
      if (R > a)
        R = a;
      s[L]++, s[R + 1]--;
    }

    for (int i = 1; i <= a; i++)
      x[i] = x[i - 1] + s[i];
    for (int i = 1; i <= a + 1; i++)
      s[i] = 0;
  }
  for (int i = 1; i <= a; i++)
    printf("%d ", x[i]);
}

Fixed submission:
#include <stdio.h>

int x[200010], s[200010];
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  for (int i = 1; i <= a; i++)
    scanf("%d", &x[i]);

  for (int j = 1; j <= 50 && j <= b; j++) {
    for (int i = 1; i <= a; i++) {
      int L = i - x[i];
      int R = i + x[i];
      if (L < 1)
        L = 1;
      if (R > a)
        R = a;
      s[L]++, s[R + 1]--;
    }

    for (int i = 1; i <= a; i++)
      x[i] = x[i - 1] + s[i];
    for (int i = 1; i <= a + 1; i++)
      s[i] = 0;
  }
  for (int i = 1; i <= a; i++)
    printf("%d ", x[i]);
}
Problem p02647 (C++)

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

const int MAXN = 1e5 + 10;

int N, K;
int A[MAXN];

int sum[MAXN];
int n_count = 0;

void do_operation() {
  int cost = 0;
  for (int i = 1; i <= N; ++i)
    sum[i] = 0;
  for (int i = 1; i <= N; ++i) {
    sum[max(1, i - A[i])]++;
    sum[min(N + 1, i + A[i] + 1)]--;
  }
  n_count = 0;
  for (int i = 1; i <= N; ++i) {
    cost += sum[i];
    A[i] = cost;
    if (cost == N)
      ++n_count;
  }
}

void solve() {
  for (int i = 1; i <= N; ++i)
    if (A[i] == N)
      n_count += 1;
  for (int k = 1; k <= K; ++k) {
    if (n_count == N)
      break;
    do_operation();
  }
}

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 1; i <= N; ++i)
    scanf("%d", &A[i]);
  solve();
  for (int i = 1; i <= N; ++i)
    printf("%d ", A[i]);
  printf("\n");
  return 0;
}

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

const int MAXN = 2e5 + 10;

int N, K;
int A[MAXN];

int sum[MAXN];
int n_count = 0;

void do_operation() {
  int cost = 0;
  for (int i = 1; i <= N; ++i)
    sum[i] = 0;
  for (int i = 1; i <= N; ++i) {
    sum[max(1, i - A[i])]++;
    sum[min(N + 1, i + A[i] + 1)]--;
  }
  n_count = 0;
  for (int i = 1; i <= N; ++i) {
    cost += sum[i];
    A[i] = cost;
    if (cost == N)
      ++n_count;
  }
}

void solve() {
  for (int i = 1; i <= N; ++i)
    if (A[i] == N)
      n_count += 1;
  for (int k = 1; k <= K; ++k) {
    if (n_count == N)
      break;
    do_operation();
  }
}

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 1; i <= N; ++i)
    scanf("%d", &A[i]);
  solve();
  for (int i = 1; i <= N; ++i)
    printf("%d ", A[i]);
  printf("\n");
  return 0;
}
Problem p02647 (C++)

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

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

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

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

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

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

  return 0;
}


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

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

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

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

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

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

  return 0;
}
Problem p02647 (C++)

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


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

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

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

  vector<ll> imos(n + 1);

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

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

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

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

  return 0;
}

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

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

  vector<ll> imos(n + 1);

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

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

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

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

  return 0;
}
Problem p02647 (C++)

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

#define ll long long
#define VI vector<int>
#define VLL vector<ll>
#define PII pair<int, int>
#define mp make_pair
#define pb push_back
#define PI acos(-1)
#define ld long double
const int MxN = 100001;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)

/* VARIABLES */
int n, k;
VI a(MxN);

/* END OF VARIABLES */

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

  cin >> n >> k;
  FOR(i, 0, n)
  cin >> a[i];
  if (k >= 3 * log2((ld)n)) {
    FOR(i, 0, n)
    cout << n << ' ';
    return 0;
  }
  FOR(i, 0, k) {
    VI tmp(n + 1);
    FOR(j, 0, n) {
      int l = max(0, j - a[j]);
      int r = min(n - 1, j + a[j]);
      tmp[l]++;
      if (r + 1 < n)
        tmp[r + 1]--;
    }
    FOR(j, 1, n) { tmp[j] += tmp[j - 1]; }
    FOR(j, 0, n) { a[j] = tmp[j]; }
  }

  FOR(i, 0, n)
  cout << a[i] << " ";

  return 0;
}


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

#define ll long long
#define VI vector<int>
#define VLL vector<ll>
#define PII pair<int, int>
#define mp make_pair
#define pb push_back
#define PI acos(-1)
#define ld long double
const int MxN = 200001;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)

/* VARIABLES */
int n, k;
VI a(MxN);

/* END OF VARIABLES */

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

  cin >> n >> k;
  FOR(i, 0, n)
  cin >> a[i];
  if (k >= 3 * log2((ld)n)) {
    FOR(i, 0, n)
    cout << n << ' ';
    return 0;
  }
  FOR(i, 0, k) {
    VI tmp(n + 1);
    FOR(j, 0, n) {
      int l = max(0, j - a[j]);
      int r = min(n - 1, j + a[j]);
      tmp[l]++;
      if (r + 1 < n)
        tmp[r + 1]--;
    }
    FOR(j, 1, n) { tmp[j] += tmp[j - 1]; }
    FOR(j, 0, n) { a[j] = tmp[j]; }
  }

  FOR(i, 0, n)
  cout << a[i] << " ";

  return 0;
}
Problem p02647 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int it = max(k, (int)floor(log(k)));
  for (int i = 0; i < it; i++) {
    vector<int> b(n + 1);
    for (int j = 0; j < n; j++) {
      int l = max(0, j - a[j]);
      int r = min(n - 1, j + a[j]);
      b[l]++;
      b[r + 1]--;
    }
    for (int j = 0; j < n; j++) {
      b[j + 1] += b[j];
      a[j] = b[j];
    }
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << ' ';
  }
  cout << '\n';
  return 0;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  k = min(k, 100);
  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(n - 1, j + a[j]);
      b[l]++;
      b[r + 1]--;
    }
    for (int j = 0; j < n; j++) {
      b[j + 1] += b[j];
      a[j] = b[j];
    }
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << ' ';
  }
  cout << '\n';
  return 0;
}
Problem p02647 (C++)

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

void solve() {
  int n, m, k, i, j, count = 0, sum = 0, maxi = 0, mini = 1e18, flag = 0;
  cin >> n >> k;

  vector<int> a(n + 1);
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }

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

    for (j = 1; j <= n; j++) {
      if (freq[j] != n) {
        flag = 1;
        break;
      }
    }

    a = freq;

    if (flag == 0)
      break;
  }

  for (i = 1; i <= n; i++) {
    cout << a[i] << " ";
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

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

void solve() {
  int n, m, k, i, j, count = 0, sum = 0, maxi = 0, mini = 1e18, flag = 0;
  cin >> n >> k;

  vector<int> a(n + 1);
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }

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

    for (j = 1; j <= n; j++) {
      if (freq[j] != n) {
        flag = 1;
        break;
      }
    }

    a = freq;

    if (flag == 0)
      break;
  }

  for (i = 1; i <= n; i++) {
    cout << a[i] << " ";
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
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)
using ll = long long;
const ll MOD = 1000000007; // 998244353;
int dp[200001][500];
void solve() {
  int n, k;
  cin >> n >> k;
  rep(i, n) cin >> dp[0][i];
  int j = 0;
  while (k--) {
    rep(i, n) {
      dp[j + 1][max(i - dp[j][i], 0)]++;
      dp[j + 1][min(i + dp[j][i] + 1, n)]--;
    }
    rep(i, n) { dp[j + 1][i + 1] += dp[j + 1][i]; }
    j++;
    if (dp[j][0] == n && dp[j][n - 1] == n)
      break;
  }
  rep(i, n) cout << dp[j][i] << " ";
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
const ll MOD = 1000000007; // 998244353;
int dp[400][200001];
void solve() {
  int n, k;
  cin >> n >> k;
  rep(i, n) cin >> dp[0][i];
  int j = 0;
  while (k--) {
    rep(i, n) {
      dp[j + 1][max(i - dp[j][i], 0)]++;
      dp[j + 1][min(i + dp[j][i] + 1, n)]--;
    }
    rep(i, n) { dp[j + 1][i + 1] += dp[j + 1][i]; }
    j++;
    if (dp[j][0] == n && dp[j][n - 1] == n)
      break;
  }
  rep(i, n) cout << dp[j][i] << " ";
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}
Problem p02647 (C++)

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define taskname ""
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef vector<cd> vcd;
typedef vector<int> vi;

template <class T> using v2d = vector<vector<T>>;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 10;

int n, k;
ll a[maxN], b[maxN];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> k;
  for1(i, n) { cin >> a[i]; }
  uin(k, 20000000 / n);
  while (k--) {
    fill(b + 1, b + n + 1, 0);
    for1(i, n) {
      int l = max(i - a[i], 1LL), r = min(i + a[i], (ll)n);
      ++b[l];
      if (r < n) {
        --b[r + 1];
      }
    }
    for1(i, n) {
      b[i] += b[i - 1];
      a[i] = b[i];
    }
  }
  for1(i, n) { cout << a[i] << ' '; }
  return 0;
}


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

#define taskname ""
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef vector<cd> vcd;
typedef vector<int> vi;

template <class T> using v2d = vector<vector<T>>;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

const int maxN = 2e5 + 10;

int n, k;
ll a[maxN], b[maxN];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> k;
  for1(i, n) { cin >> a[i]; }
  uin(k, 20000000 / n);
  while (k--) {
    fill(b + 1, b + n + 1, 0);
    for1(i, n) {
      int l = max(i - a[i], 1LL), r = min(i + a[i], (ll)n);
      ++b[l];
      if (r < n) {
        --b[r + 1];
      }
    }
    for1(i, n) {
      b[i] += b[i - 1];
      a[i] = b[i];
    }
  }
  for1(i, n) { cout << a[i] << ' '; }
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;

int a[100010], c[100010];

int max(int a, int b) { return a > b ? a : b; }

inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}

inline void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

inline void writeln(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
  puts("");
}

signed main() {
  int n = read(), k = read();

  for (int i = 1; i <= n; i++)
    a[i] = read();

  for (int j = 1; j <= k; j++) {
    memset(c, 0, sizeof(c));
    for (int i = 1; i <= n; i++) {
      c[max(1, i - a[i])]++;
      c[min(n + 1, i + a[i] + 1)]--;
    }
    int s = 0, f = 1;

    for (int i = 1; i <= n; i++) {
      a[i] = s += c[i];
      if (a[i] < n)
        f = 0;
    }
    if (f)
      break;
  }

  for (int i = 1; i <= n; i++)
    write(a[i]), cout << " ";

  puts("");

  return 0;
}

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

int a[200010], c[200010];

int max(int a, int b) { return a > b ? a : b; }

inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}

inline void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

inline void writeln(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
  puts("");
}

signed main() {
  int n = read(), k = read();

  for (int i = 1; i <= n; i++)
    a[i] = read();

  for (int j = 1; j <= k; j++) {
    memset(c, 0, sizeof(c));
    for (int i = 1; i <= n; i++) {
      c[max(1, i - a[i])]++;
      c[min(n + 1, i + a[i] + 1)]--;
    }
    int s = 0, f = 1;

    for (int i = 1; i <= n; i++) {
      a[i] = s += c[i];
      if (a[i] < n)
        f = 0;
    }
    if (f)
      break;
  }

  for (int i = 1; i <= n; i++)
    write(a[i]), cout << " ";

  puts("");

  return 0;
}
Problem p02647 (C++)

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

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

  for (int i = 0; i < n; i++) {
    cin >> intensity[i];
  }
  for (int j = 0; j < k; j++) {
    vector<int> power(n + 1);
    for (int i = 0; i < n; i++) {
      int start = i - intensity[i];
      int end = i + intensity[i] + 1;
      if (start < 0)
        start = 0;
      if (end >= n)
        end = n;
      power[start] += 1;
      power[end] -= 1;
    }

    long long current = 0;
    for (int i = 0; i < n; i++) {
      current += power[i];
      intensity[i] = current;
    }
  }
  for (int x : intensity) {
    cout << x << " ";
  }
}


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

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

  for (int i = 0; i < n; i++) {
    cin >> intensity[i];
  }
  for (int j = 0; j < min(k, 50); j++) {
    vector<int> power(n + 1);
    for (int i = 0; i < n; i++) {
      int start = i - intensity[i];
      int end = i + intensity[i] + 1;
      if (start < 0)
        start = 0;
      if (end >= n)
        end = n;
      power[start] += 1;
      power[end] -= 1;
    }

    long long current = 0;
    for (int i = 0; i < n; i++) {
      current += power[i];
      intensity[i] = current;
    }
  }
  for (int x : intensity) {
    cout << x << " ";
  }
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug1: " << x << endl;
#define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl;
#define bug3(x, y, z)                                                          \
  cout << "zdongdebug3: " << x << " " << y << " " << z << endl;
using namespace std;
typedef long long ll;
const int maxn = 100005;
const int mod = 1000000007;

int n, m;
int a[maxn], b[maxn];

bool f() {
  bool sign = 0;
  for (int i = 1; i <= n; i++) {
    b[i] = 0;
    if (a[i] < n)
      sign = 1;
  }
  if (!sign) {
    return true;
  }
  for (int i = 1; i <= n; i++) {
    int l = i - a[i];
    int r = i + a[i] + 1;
    l = max(l, 1);
    r = min(r, n + 1);
    b[l]++;
    b[r]--;
    a[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    a[i] = a[i - 1] + b[i];
  }
  return false;
}

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++) {
    if (f()) {
      break;
    }
  }
  for (int i = 1; i < n; i++)
    cout << a[i] << " ";
  cout << a[n] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug1: " << x << endl;
#define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl;
#define bug3(x, y, z)                                                          \
  cout << "zdongdebug3: " << x << " " << y << " " << z << endl;
using namespace std;
typedef long long ll;
const int maxn = 200005;
const int mod = 1000000007;

int n, m;
int a[maxn], b[maxn];

bool f() {
  bool sign = 0;
  for (int i = 1; i <= n; i++) {
    b[i] = 0;
    if (a[i] < n)
      sign = 1;
  }
  if (!sign) {
    return true;
  }
  for (int i = 1; i <= n; i++) {
    int l = i - a[i];
    int r = i + a[i] + 1;
    l = max(l, 1);
    r = min(r, n + 1);
    b[l]++;
    b[r]--;
    a[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    a[i] = a[i - 1] + b[i];
  }
  return false;
}

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++) {
    if (f()) {
      break;
    }
  }
  for (int i = 1; i < n; i++)
    cout << a[i] << " ";
  cout << a[n] << endl;
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repI(i, d, n) for (int i = (d); i < (n); ++i)
#define reps1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using M = map<int, int>;
using Ml = map<ll, ll>;
ll const INF = 1ll << 61;
double pi = 3.141592653589793238;

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

  rep(j, k) {
    rep(i, n + 1) table[i] = 0;

    rep(i, n) {
      table[max(0ll, i - a[i])]++;
      table[min(n, i + a[i] + 1)]--;
    }

    rep(i, n) {
      if (0 < i)
        table[i] += table[i - 1];
    }

    rep(i, n) a[i] = table[i];
  }

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repI(i, d, n) for (int i = (d); i < (n); ++i)
#define reps1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using M = map<int, int>;
using Ml = map<ll, ll>;
ll const INF = 1ll << 61;
double pi = 3.141592653589793238;

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

  rep(j, k) {
    rep(i, n + 1) table[i] = 0;

    rep(i, n) {
      table[max(0ll, i - a[i])]++;
      table[min(n, i + a[i] + 1)]--;
    }

    rep(i, n) {
      if (0 < i)
        table[i] += table[i - 1];
    }

    rep(i, n) a[i] = table[i];

    bool flag = true;
    rep(i, n) {
      if (a[i] != n) {
        flag = false;
        break;
      }
    }
    if (flag)
      break;
  }

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

  return 0;
}
Problem p02647 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  if (log10(N) * 8 - 2 <= K) {
    for (int i = 0; i < N; i++) {
      cout << N;
      if (i == N - 1)
        cout << endl;
      else
        cout << ' ';
    }
    return 0;
  }
  vector<vector<int>> A(K + 1, vector<int>(N, 0));
  for (int &i : A[0])
    cin >> i;
  for (int i = 0; i < K; i++) {
    for (int k = 0; k < N; k++) {
      int l = min(N - 1, k + A[i][k]);
      for (int j = max(0, k - A[i][k]); j <= l; j++) {
        A[i + 1][j]++;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << A[K][i];
    if (i == N - 1)
      cout << endl;
    else
      cout << ' ';
  }
}

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

int main() {
  int N, K;
  cin >> N >> K;
  if (log10(N) * 8 - 2 <= K) {
    for (int i = 0; i < N; i++) {
      cout << N;
      if (i == N - 1)
        cout << endl;
      else
        cout << ' ';
    }
    return 0;
  }
  vector<vector<int>> A(K + 1, vector<int>(N, 0));
  for (int &i : A[0])
    cin >> i;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < N; j++) {
      A[i + 1][max(0, j - A[i][j])]++;
      A[i + 1][min(N - 1, j + A[i][j]) + 1]--;
    }
    for (int j = 1; j < N; j++) {
      A[i + 1][j] += A[i + 1][j - 1];
    }
  }
  for (int i = 0; i < N; i++) {
    cout << A[K][i];
    if (i == N - 1)
      cout << endl;
    else
      cout << ' ';
  }
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<int>;
using P = pair<int, int>;
const int INF = 1e9 + 7;

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

  rep(ki, k) {
    vec b(n);
    rep(i, n) {
      int l = max(0, i - a[i]);
      int r = min(n, i + a[i] + 1);
      b[l]++;
      if (r < n)
        b[r]--;
    }
    bool f = true;
    rep(i, n) { b[i + 1] += b[i]; }

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

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


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<int>;
using P = pair<int, int>;
const int INF = 1e9 + 7;

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

  rep(ki, k) {
    vec b(n);
    rep(i, n) {
      int l = max(0, i - a[i]);
      int r = min(n, i + a[i] + 1);
      b[l]++;
      if (r < n)
        b[r]--;
    }
    bool f = true;
    rep(i, n - 1) { b[i + 1] += b[i]; }

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

  rep(i, n) { cout << a[i] << endl; }
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

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

  ll A[N];
  ll area[N + 1]; // N + 1は無視
  ll areaSum;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (ll rep = 0; rep < K; rep++) {
    for (ll i = 0; i < N; i++) {
      area[i] = 0;
    }
    for (ll i = 0; i < N; i++) {
      area[max(i - A[i], (ll)0)]++;
      area[min(i + A[i] + 1, N)]--;
    }
    areaSum = 0;
    for (ll i = 0; i < N; i++) {
      areaSum += area[i];
      A[i] = areaSum;
    }
  }

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

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

  ll A[N];
  ll area[N + 1]; // N + 1は無視
  ll areaSum;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }

  ll tryMin = 1000;
  ll tryMinN = 1;
  while (tryMinN <= N) {
    tryMinN *= 2;
    tryMin++;
  }
  tryMin = min(tryMin, K);
  for (ll rep = 0; rep < tryMin; rep++) {
    for (ll i = 0; i < N; i++) {
      area[i] = 0;
    }
    for (ll i = 0; i < N; i++) {
      area[max(i - A[i], (ll)0)]++;
      area[min(i + A[i] + 1, N)]--;
    }
    areaSum = 0;
    for (ll i = 0; i < N; i++) {
      areaSum += area[i];
      A[i] = areaSum;
    }
  }

  for (ll 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):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
#define vec(s) vector
;
#define vvec(s) vector<vector
>;
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define YN(x) cout << (bool x ? "Yes" : "No") << endl;
#define out(s) cout << s << endl;
#define pb(s) push_back(s);
#define sp " ";
#define INF 10000000000
#define all(s) s.begin(), s.end()
void vout(vi v) {
  for (int i = 0; i < v.size(); i++)
    cout << v.at(i) << endl;
}
int main() {
  lli n, k;
  cin >> n >> k;
  vi ans(n + 2, 0), cnt(n + 2, 0), meme(n + 2, 0);
  rep(i, n) cin >> ans[i + 1];

  rep(i, k) {
    swap(ans, cnt);
    ans = meme;
    for (int j = 1; j <= n; j++) {
      (j - cnt[j] > 0 ? ans[j - cnt[j]] += 1 : ans[0] += 1);
      (j + cnt[j] + 1 < n + 1 ? ans[j + cnt[j] + 1] -= 1 : ans[n + 1] -= 1);
    }
    for (int j = 1; j <= n; j++) {
      ans[j] = ans[j] + ans[j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i];
    if (i != n)
      cout << " ";
    else
      cout << endl;
  }
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
#define vec(s) vector
;
#define vvec(s) vector<vector
>;
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define YN(x) cout << (bool x ? "Yes" : "No") << endl;
#define out(s) cout << s << endl;
#define pb(s) push_back(s);
#define sp " ";
#define INF 10000000000
#define all(s) s.begin(), s.end()
void vout(vi v) {
  for (int i = 0; i < v.size(); i++)
    cout << v.at(i) << endl;
}
int main() {
  lli n, k;
  cin >> n >> k;
  vi ans(n + 2, 0), cnt(n + 2, 0), meme(n + 2, 0);
  rep(i, n) cin >> ans[i + 1];

  rep(i, min(k, (lli)41)) {
    swap(ans, cnt);
    ans = meme;
    for (int j = 1; j <= n; j++) {
      (j - cnt[j] > 0 ? ans[j - cnt[j]] += 1 : ans[0] += 1);
      (j + cnt[j] + 1 < n + 1 ? ans[j + cnt[j] + 1] -= 1 : ans[n + 1] -= 1);
    }
    for (int j = 1; j <= n; j++) {
      ans[j] = ans[j] + ans[j - 1];
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i];
    if (i != n)
      cout << " ";
    else
      cout << endl;
  }
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t)
    fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);

signed main() {

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

  auto dp = make_vec<int>(111, n + 1);
  dp[0] = a;
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < n; j++) {
      dp[i + 1][max(0LL, j - dp[i][j])]++;
      dp[i + 1][min(n, j + dp[i][j] + 1)]--;
    }
    for (int j = 0; j < n - 1; j++) {
      dp[i + 1][j + 1] += dp[i + 1][j];
    }
  }

  for (int i = 0; i < n; i++) {
    cout << dp[k][i] << " ";
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}
template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t)
    fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);

signed main() {

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

  auto dp = make_vec<int>(111, n + 1);
  dp[0] = a;
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < n; j++) {
      dp[i + 1][max(0LL, j - dp[i][j])]++;
      dp[i + 1][min(n, j + dp[i][j] + 1)]--;
    }
    for (int j = 0; j < n - 1; j++) {
      dp[i + 1][j + 1] += dp[i + 1][j];
    }
  }

  k = min(100LL, k);
  for (int i = 0; i < n; i++) {
    cout << dp[k][i] << " ";
  }
  cout << endl;

  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
// khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 200000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;

int k, n, a[N], ps[N];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= min(100, k); i++) {
    memset(ps, 0, sizeof ps);
    for (int j = 1; j <= n; j++) {
      //			cout << j - a[i] << '\n';
      ps[j - a[j]]++;
      ps[j + a[j] + 1]--;
    }
    for (int j = 1; j <= n; j++)
      ps[j] += ps[j - 1];
    for (int j = 1; j <= n; j++)
      a[j] = ps[j];
  }
  for (int i = 1; i <= n; i++)
    cout << a[i] << ' ';

  return 0;
}


Fixed submission:
// khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 200000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;

int k, n, a[N], ps[N];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= min(100, k); i++) {
    memset(ps, 0, sizeof ps);
    for (int j = 1; j <= n; j++) {
      //			cout << j - a[i] << '\n';
      ps[max(0, j - a[j])]++;
      ps[min(n + 1, j + a[j] + 1)]--;
    }
    for (int j = 1; j <= n; j++)
      ps[j] += ps[j - 1];
    for (int j = 1; j <= n; j++)
      a[j] = ps[j];
  }
  for (int i = 1; i <= n; i++)
    cout << a[i] << ' ';

  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(ki, k) {
    vector<int> b(n + 1);
    rep(i, n) {
      int l = max(0, i - a[i]);
      int r = min(i + a[i] + 1, n + 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) { cout << a[i] << endl; }
}


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

using namespace std;
using ll = long long;
using P = pair<int, int>;

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

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
// #define int ll

const int N = 1e5 + 10;
int a[N], b[N];

void add(int l, int r) {
  b[l]++;
  b[r + 1]--;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  for (int iter = 1; iter <= k; iter++) {
    for (int i = 1; i <= n; i++) {
      a[i] = b[i];
      b[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      add(max(1, i - a[i]), min(n, i + a[i]));
    }
    bool ok = true;
    for (int i = 1; i <= n; i++) {
      b[i] += b[i - 1];
      if (b[i] != n) {
        ok = false;
      }
    }
    if (ok) {
      break;
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << b[i] << ' ';
  }
  cout << '\n';
}

Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
// #define int ll

const int N = 2e5 + 10;
int a[N], b[N];

void add(int l, int r) {
  b[l]++;
  b[r + 1]--;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  for (int iter = 1; iter <= k; iter++) {
    for (int i = 1; i <= n; i++) {
      a[i] = b[i];
      b[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      add(max(1, i - a[i]), min(n, i + a[i]));
    }
    bool ok = true;
    for (int i = 1; i <= n; i++) {
      b[i] += b[i - 1];
      if (b[i] != n) {
        ok = false;
      }
    }
    if (ok) {
      break;
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << b[i] << ' ';
  }
  cout << '\n';
}