prithivMLmods/Coder-Stat huggingface dataset:
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> inp(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> inp.at(i);
    // inp.at(i) = 0;
  }

  vector<int> data(n + 2, 0);

  if (k >= 3000) {
    k = 3000;
  }

  for (int j = 0; j < k; j++) {
    for (int i = 1; i <= n; i++) {
      int left = max(i - inp.at(i), 1);
      int right = min(i + inp.at(i) + 1, n + 1);
      data.at(left)++;
      data.at(right)--;
    }
    int temp = 0;
    for (int i = 1; i <= n; i++) {
      temp += data.at(i);
      data.at(i) = 0;
      inp.at(i) = temp;
    }
  }

  // 確認用
  for (int i = 1; i <= n; i++) {
    cout << inp.at(i);
    if (i != n) {
      cout << " ";
    }
  }
  // cout << " " << j+1 << endl;
  cout << endl;
}

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

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

  vector<int> inp(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> inp.at(i);
    // inp.at(i) = 0;
  }

  vector<int> data(n + 2, 0);

  if (k >= 2000) {
    k = 2000;
  }

  for (int j = 0; j < k; j++) {
    for (int i = 1; i <= n; i++) {
      int left = max(i - inp.at(i), 1);
      int right = min(i + inp.at(i) + 1, n + 1);
      data.at(left)++;
      data.at(right)--;
    }
    int temp = 0;
    for (int i = 1; i <= n; i++) {
      temp += data.at(i);
      data.at(i) = 0;
      inp.at(i) = temp;
    }
  }

  // 確認用
  for (int i = 1; i <= n; i++) {
    cout << inp.at(i);
    if (i != n) {
      cout << " ";
    }
  }
  // cout << " " << j+1 << endl;
  cout << endl;
}
Problem p02647 (C++)

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

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

bool done(vector<int> &A, int N) {
  for (auto x : A)
    if (x != N)
      return false;
  return true;
}

vector<int> eval(vector<int> &A, int N) {
  map<int, int> cumul;
  for (int i = 0; i < N; i++)
    cumul[max(0, i - A[i])]++, cumul[i + A[i] + 1]--;
  vector<int> ret(N, 0);
  ret[0] = cumul[0];
  for (int i = 1; i < N; i++)
    ret[i] = ret[i - 1] + cumul[i];
  return ret;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  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++) {
    if (done(A, N))
      break;
    A = eval(A, N);
  }
  for (int i = 0; i < N; i++)
    cout << A[i] << " ";
  cout << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

bool done(vector<int> &A, int N) {
  for (auto x : A)
    if (x != N)
      return false;
  return true;
}

vector<int> eval(vector<int> &A, int N) {
  vector<int> cumul(2 * N, 0);
  for (int i = 0; i < N; i++)
    cumul[max(0, i - A[i])]++, cumul[i + A[i] + 1]--;
  vector<int> ret(N, 0);
  ret[0] = cumul[0];
  for (int i = 1; i < N; i++)
    ret[i] = ret[i - 1] + cumul[i];
  return ret;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  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++) {
    if (done(A, N))
      break;
    A = eval(A, N);
  }
  for (int i = 0; i < N; i++)
    cout << A[i] << " ";
  cout << "\n";
  return 0;
}
Problem p02647 (C++)

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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T> inline void chkmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <typename T> inline void chkmin(T &x, T y) {
  if (x > y)
    x = y;
}

inline int read() {
#define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48)
    c = nc;
  while (c > 47)
    x = x * 10 + (c ^ 48), c = nc;
  return x;
#undef nc
}

typedef long long ll;
const int maxn = 2e5 + 10;
int n, k, A[maxn], tmp[maxn];

void solve() {
  cin >> n >> k;
  rep(i, 1, n) A[i] = read();
  rep(T, 1, k) {
    memset(tmp, 0, (n + 1) << 2);
    rep(i, 1, n) {
      int l = i - A[i], r = i + A[i];
      tmp[l]++, tmp[r + 1]--;
    }
    bool flg = 1;
    rep(i, 1, n) {
      tmp[i] += tmp[i - 1];
      A[i] = tmp[i];
      flg &= A[i] == n;
    }
    if (flg)
      break;
  }
  rep(i, 1, n) printf("%d ", A[i]);
}

int main() {
  //  int T = read();
  //  while (T--) solve();
  solve();
  return 0;
}


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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T> inline void chkmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <typename T> inline void chkmin(T &x, T y) {
  if (x > y)
    x = y;
}

inline int read() {
#define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48)
    c = nc;
  while (c > 47)
    x = x * 10 + (c ^ 48), c = nc;
  return x;
#undef nc
}

typedef long long ll;
const int maxn = 2e5 + 10;
int n, k, A[maxn], tmp[maxn];

void solve() {
  cin >> n >> k;
  rep(i, 1, n) A[i] = read();
  rep(T, 1, k) {
    memset(tmp, 0, (n + 1) << 2);
    rep(i, 1, n) {
      int l = i - A[i], r = i + A[i];
      chkmax(l, 1), chkmin(r, n);
      tmp[l]++, tmp[r + 1]--;
    }
    bool flg = 1;
    rep(i, 1, n) {
      tmp[i] += tmp[i - 1];
      A[i] = tmp[i];
      flg &= A[i] == n;
    }
    if (flg)
      break;
  }
  rep(i, 1, n) printf("%d ", A[i]);
}

int main() {
  //  int T = read();
  //  while (T--) solve();
  solve();
  return 0;
}
Problem p02647 (Python)

Buggy submission (status: Time Limit Exceeded):
from itertools import accumulate


def main():
    number, operations = [int(x) for x in input().split()]
    dists = [int(x) for x in input().split()]

    for ope_count in range(operations):
        temp = [0] * (number + 1)
        for i, v in enumerate(dists):
            left = max(i - v, 0)
            right = min(i + v + 1, number)
            temp[left] += 1
            temp[right] -= 1
        templi = list(accumulate(temp))
        if templi == dists:
            dists = templi
            break
        else:
            dists = templi

    print(*list(dists)[:-1])


if __name__ == "__main__":
    main()


Fixed submission:
from itertools import accumulate


def main():
    number, operations = [int(x) for x in input().split()]
    dists = [int(x) for x in input().split()]

    for ope_count in range(operations):
        temp = [0] * (number + 1)
        for i, v in enumerate(dists):
            left = max(i - v, 0)
            right = min(i + v + 1, number)
            temp[left] += 1
            temp[right] -= 1
        templi = list(accumulate(temp))
        if templi == dists:
            break
        else:
            dists = templi

    print(*list(dists)[:-1])


if __name__ == "__main__":
    main()
Problem p02647 (C++)

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

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  VL a(n + 1);
  rep(i, n) cin >> a[i];
  if (k > n) {
    rep(i, n) cout << n << " ";
    cout << "\n";
    return 0;
  }
  VL sk(n + 1);
  rep(q, k) {
    fill(all(sk), 0);
    rep(i, n) {
      int st = max<ll>(0, i - a[i]);
      int ed = min<ll>(n, i + a[i] + 1);
      sk[st] += 1;
      sk[ed] -= 1;
    }
    rep(i, n) sk[i + 1] += sk[i];
    // rep(i, n) cout << sk[i] << " "; dbg("q=",q);
    a = sk;
    if (count(sk.begin(), sk.begin() + k, n) == n)
      break;
  }
  rep(i, n) cout << sk[i] << " ";
  cout << "\n";
  return 0;
}


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

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  VL a(n + 1);
  rep(i, n) cin >> a[i];
  if (k > n) {
    rep(i, n) cout << n << " ";
    cout << "\n";
    return 0;
  }
  VL sk(n + 1);
  rep(q, k) {
    fill(all(sk), 0);
    rep(i, n) {
      int st = max<ll>(0, i - a[i]);
      int ed = min<ll>(n, i + a[i] + 1);
      sk[st] += 1;
      sk[ed] -= 1;
    }
    rep(i, n) sk[i + 1] += sk[i];
    // rep(i, n) cout << sk[i] << " "; dbg("q=",q);
    a = sk;
    if (count(sk.begin(), sk.begin() + k, n) == n)
      break;
    if (q * n > 20000000)
      break;
  }
  rep(i, n) cout << sk[i] << " ";
  cout << "\n";
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

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

const ll INF = 1e18;
const ll mod = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> cur(n + 1), next(n + 1);
  rep(i, n) { cin >> cur[i]; }
  rep(i, k) {
    bool all = true;
    rep(j, n) {
      ll pos1 = max(j - cur[j], 0LL);
      ll pos2 = min(j + cur[j] + 1, n);
      next[pos1]++;
      next[pos2]--;
    }
    rep(j, n + 1) {
      if (j) {
        next[j] += next[j - 1];
      }
    }
    rep(j, n + 1) {
      if (cur[j] != next[j])
        all = false;
    }
    cur = next;
    next = vector<ll>(n);
    if (all)
      break;
  }
  rep(i, n) {
    if (i)
      cout << " ";
    cout << cur[i];
  }
  cout << endl;
  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

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

const ll INF = 1e18;
const ll mod = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> cur(n + 1), next(n + 1);
  rep(i, n) { cin >> cur[i]; }
  rep(i, k) {
    bool all = true;
    rep(j, n) {
      ll pos1 = max(j - cur[j], 0LL);
      ll pos2 = min(j + cur[j] + 1, n);
      next[pos1]++;
      next[pos2]--;
    }
    rep(j, n + 1) {
      if (j) {
        next[j] += next[j - 1];
      }
    }
    rep(j, n) {
      if (cur[j] != next[j])
        all = false;
    }
    cur = next;
    next = vector<ll>(n);
    if (all)
      break;
  }
  rep(i, n) {
    if (i)
      cout << " ";
    cout << cur[i];
  }
  cout << endl;
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
constexpr int64_t MOD = 1e9 + 7;

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

  int32_t N, K;
  cin >> N >> K;
  vector<int32_t> A(N, 0);
  for (auto &a : A) {
    cin >> a;
  }
  for (int k = 0; k < K; k++) {
    vector<int> begin(N, 0);
    vector<int> end(N, 0);
    for (int i = 0; i < N; i++) {
      int b = max(0, i - A[i]);
      int e = min(N - 1, i + A[i]);
      begin[b]++;
      end[e]++;
    }
    int cum_beg = begin[0];
    int cum_end = 0;
    A[0] = cum_beg;
    for (int i = 1; i < N; i++) {
      cum_beg += begin[i];
      cum_end += end[i - 1];
      A[i] = cum_beg - cum_end;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << A[i] << " ";
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
constexpr int64_t MOD = 1e9 + 7;

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

  int32_t N, K;
  cin >> N >> K;
  vector<int32_t> A(N, 0);
  for (auto &a : A) {
    cin >> a;
  }
  for (int k = 0; k < K; k++) {
    vector<int> begin(N, 0);
    vector<int> end(N, 0);
    for (int i = 0; i < N; i++) {
      int b = max(0, i - A[i]);
      int e = min(N - 1, i + A[i]);
      begin[b]++;
      end[e]++;
    }
    int cum_beg = begin[0];
    int cum_end = 0;
    A[0] = cum_beg;
    for (int i = 1; i < N; i++) {
      cum_beg += begin[i];
      cum_end += end[i - 1];
      A[i] = cum_beg - cum_end;
    }
    if (begin[0] == N && end[N - 1] == N)
      break;
  }
  for (int i = 0; i < N; i++) {
    cout << A[i] << " ";
  }
  cout << endl;
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

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

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  forn(i, 0, n) cin >> a[i];

  while (k--) {
    vi b(n);
    for (int i = 0; i < n; i++) {
      int from = max(i - a[i], 0);
      int to = min(n - 1, i + a[i]);
      b[from] += 1;
      if (to < n - 1)
        b[to + 1] -= 1;
    }

    vi cpya = a;

    for (int i = 0; i < n; i++) {
      a[i] = b[i];
      b[i + 1] += b[i];
    }
    if (cpya == a)
      break;
    // for(int i =0;i<n;i+)
  }

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

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

  // int t;cin >> t;while(t--)
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

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

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  forn(i, 0, n) cin >> a[i];

  while (k--) {
    vi b(n + 1);
    for (int i = 0; i < n; i++) {
      int from = max(i - a[i], 0);
      int to = min(n - 1, i + a[i]);
      b[from] += 1;
      if (to < n - 1)
        b[to + 1] -= 1;
    }

    vi cpya = a;

    for (int i = 0; i < n; i++) {
      a[i] = b[i];
      b[i + 1] += b[i];
    }
    if (cpya == a)
      break;
    // for(int i =0;i<n;i+)
  }

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

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

  // int t;cin >> t;while(t--)
  solve();
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>
#define INF 1e16
#define N (1000000007)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, P> Q;

ll sum[200010];

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i + 1];
  for (int q = 0; q < k; q++) {
    for (int i = 1; i <= n; i++) {
      sum[max(1LL, i - a[i])]++;
      sum[min(n + 1, i + a[i] + 1)]--;
    }
    for (int i = 1; i <= n + 1; i++) {
      sum[i] += sum[i - 1];
    }
    for (int i = 1; i <= n; i++) {
      a[i] = sum[i];
    }
    for (int i = 0; i <= n + 1; i++)
      sum[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (i == n)
      cout << a[i] << endl;
    else
      cout << a[i] << " ";
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>
#define INF 1e16
#define N (1000000007)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, P> Q;

ll sum[200010];

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i + 1];
  for (int q = 0; q < k; q++) {
    for (int i = 1; i <= n; i++) {
      sum[max(1LL, i - a[i])]++;
      sum[min(n + 1, i + a[i] + 1)]--;
    }
    for (int i = 1; i <= n + 1; i++) {
      sum[i] += sum[i - 1];
    }
    for (int i = 1; i <= n; i++) {
      a[i] = sum[i];
    }
    bool flag = true;
    for (int i = 1; i <= n; i++) {
      if (a[i] != n)
        flag = false;
    }
    if (flag)
      break;
    for (int i = 0; i <= n + 1; i++)
      sum[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (i == n)
      cout << a[i] << endl;
    else
      cout << a[i] << " ";
  }
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 200005;

int n, k;
int a[N];

int b[N];

int q[N];

void solv() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= k; ++i) {
    for (int i = 1; i <= n; ++i)
      q[i] = 0;
    for (int i = 1; i <= n; ++i) {
      q[max(1, i - a[i])]++;
      q[min(n + 1, i + a[i] + 1)]--;
    }
    for (int i = 1; i <= n; ++i)
      q[i] += q[i - 1];
    for (int i = 1; i <= n; ++i)
      a[i] = q[i];
    bool z = true;
    for (int i = 1; i <= n; ++i) {
      if (a[i] != n) {
        z = false;
        break;
      }
    }
  }
  for (int i = 1; i <= n; ++i)
    printf("%d ", a[i]);
  printf("\n");
}

int main() {
#ifdef SOMETHING
  freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
  // ios_base::sync_with_stdio(false), cin.tie(0);
  solv();
  return 0;
}

// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 200005;

int n, k;
int a[N];

int b[N];

int q[N];

void solv() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= k; ++i) {
    for (int i = 1; i <= n; ++i)
      q[i] = 0;
    for (int i = 1; i <= n; ++i) {
      q[max(1, i - a[i])]++;
      q[min(n + 1, i + a[i] + 1)]--;
    }
    for (int i = 1; i <= n; ++i)
      q[i] += q[i - 1];
    for (int i = 1; i <= n; ++i)
      a[i] = q[i];
    bool z = true;
    for (int i = 1; i <= n; ++i) {
      if (a[i] != n) {
        z = false;
        break;
      }
    }
    if (z)
      break;
  }
  for (int i = 1; i <= n; ++i)
    printf("%d ", a[i]);
  printf("\n");
}

int main() {
#ifdef SOMETHING
  freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
  // ios_base::sync_with_stdio(false), cin.tie(0);
  solv();
  return 0;
}

// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef long double ld;

ll N, K;
ll A[200000];
ll sum[200001];
ll ans[200001];

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

  rep(j, K) {
    rep(i, N) {
      sum[max((ll)0, i - A[i])]++;
      sum[min(i + A[i] + 1, N)]--;
    }
    repn(i, N - 1) { sum[i] += sum[i - 1]; }
    rep(i, N) { A[i] = sum[i]; }
    fill(sum, sum + N, 0);
  }

  rep(i, N) cout << A[i] << ' ';
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef long double ld;

ll N, K;
ll A[200000];
ll sum[200001];
ll ans[200001];

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

  rep(j, K) {
    rep(i, N) {
      sum[max((ll)0, i - A[i])]++;
      sum[min(i + A[i] + 1, N)]--;
    }
    repn(i, N - 1) { sum[i] += sum[i - 1]; }
    rep(i, N) { A[i] = sum[i]; }
    fill(sum, sum + N, 0);
    bool flag = true;
    rep(i, N) { flag &= (A[i] == N); }
    if (flag)
      break;
  }

  rep(i, N) cout << A[i] << ' ';
  cout << endl;
}
Problem p02647 (C++)

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

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

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

typedef long long int ll;
typedef pair<ll, ll> P;

ll a[212345], b[212345];

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

  for (int i = 0; i < k; i++) {
    for (int m = 0; m < n + 1; m++) {
      b[m] = 0;
    }
    for (int m = 0; m < n; m++) {
      b[max((ll)0, m - a[m])]++;
      b[min(n - 1, m + a[m]) + 1]--;
    }
    ll count = 0;
    for (int m = 0; m < n; m++) {
      count += b[m];
      a[m] = count;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }

  return 0;
}


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

typedef long long int ll;
typedef pair<ll, ll> P;

ll a[212345], b[212345];

int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (pow(2, k - 50) > n) {
    for (int i = 0; i < n; i++) {
      cout << n << " ";
    }
    return 0;
  }
  for (int i = 0; i < k; i++) {
    for (int m = 0; m < n + 1; m++) {
      b[m] = 0;
    }
    for (int m = 0; m < n; m++) {
      b[max((ll)0, m - a[m])]++;
      b[min(n - 1, m + a[m]) + 1]--;
    }
    ll count = 0;
    for (int m = 0; m < n; m++) {
      count += b[m];
      a[m] = count;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }

  return 0;
}
Problem p02647 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);
  // naive
  for (int i = 1; i <= k; i++) {
    vector<int> imos(n + 1, 0);
    for (int j = 0; j < n; j++) {
      int pp = max(0, j - a.at(j));
      int mp = min(n, j + a.at(j) + 1);
      imos.at(pp)++;
      imos.at(mp)--;
    }
    for (int j = 1; j < n; j++)
      imos.at(j) += imos.at(j - 1);
    for (int j = 0; j < n; j++)
      a.at(j) = imos.at(j);
  }
  for (int i = 0; i < n; i++) {
    if (i == 0)
      cout << a.at(i);
    else
      cout << " " << a.at(i);
  }
}

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);
  // naive
  for (int i = 1; i <= k; i++) {
    vector<int> imos(n + 1, 0);
    for (int j = 0; j < n; j++) {
      int pp = max(0, j - a.at(j));
      int mp = min(n, j + a.at(j) + 1);
      imos.at(pp)++;
      imos.at(mp)--;
    }
    for (int j = 1; j < n; j++)
      imos.at(j) += imos.at(j - 1);
    bool x = false;
    for (int j = 0; j < n; j++) {
      if (a.at(j) != imos.at(j)) {
        x = true;
        break;
      }
    }
    if (!x)
      break;
    for (int j = 0; j < n; j++)
      a.at(j) = imos.at(j);
  }
  for (int i = 0; i < n; i++) {
    if (i == 0)
      cout << a.at(i);
    else
      cout << " " << a.at(i);
  }
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 1e5 + 5;

int a[sz], b[sz];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  while (k--) {
    for (int i = 0; i < n; i++) {
      int x = max(0, i - a[i]);
      int y = min(n, i + a[i] + 1);
      b[x]++, b[y]--;
    }
    for (int i = 0, j = 0; i < n; i++) {
      j += b[i];
      a[i] = j, b[i] = 0;
    }
    bool no = 1;
    for (int i = 0; i < n; i++)
      if (a[i] < n)
        no = 0;
    if (no)
      break;
  }
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", a[i]);
  }
  puts("");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 2e5 + 5;

int a[sz], b[sz];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  while (k--) {
    for (int i = 0; i < n; i++) {
      int x = max(0, i - a[i]);
      int y = min(n, i + a[i] + 1);
      b[x]++, b[y]--;
    }
    for (int i = 0, j = 0; i < n; i++) {
      j += b[i];
      a[i] = j, b[i] = 0;
    }
    bool no = 1;
    for (int i = 0; i < n; i++)
      if (a[i] < n)
        no = 0;
    if (no)
      break;
  }
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", a[i]);
  }
  puts("");
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
 * Since g++10 is released, some characters is not valid inside #if 0 :(
 * So, why not using clang++? :D

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

 * Solution:
  To be updated after "Accept".

 * Digression:

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

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

typedef long long ll;

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

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

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


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

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

 * Solution:
  To be updated after "Accept".

 * Digression:

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

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

typedef long long ll;

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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  int n, k;
  cin >> n >> k;
  vint a(n);
  rep(i, n) cin >> a[i];
  rep(i, k) {
    vint c(n + 1, 0);
    rep(j, n) {
      c[max(j - a[j], 0)]++;
      c[min(j + a[j] + 1, n)]--;
    }
    vint a_tmp(n);
    a_tmp[0] = c[0];
    REP(i, n) a_tmp[i] = a_tmp[i - 1] + c[i];
    swap(a_tmp, a);
    bool ok = true;
    // rep(i,n-1) {
    //     if (a[i] != a[i+1]) ok = false;
    // }
    // if (ok) break;
  }
  rep(i, n) cout << a[i] << " ";
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  int n, k;
  cin >> n >> k;
  vint a(n);
  rep(i, n) cin >> a[i];
  rep(i, k) {
    vint c(n + 1, 0);
    rep(j, n) {
      c[max(j - a[j], 0)]++;
      c[min(j + a[j] + 1, n)]--;
    }
    vint a_tmp(n);
    a_tmp[0] = c[0];
    REP(i, n) a_tmp[i] = a_tmp[i - 1] + c[i];
    swap(a_tmp, a);
    bool ok = true;
    rep(i, n - 1) {
      if (a[i] != a[i + 1])
        ok = false;
    }
    if (ok && a[0] == n)
      break;
  }
  rep(i, n) cout << a[i] << " ";
}
Problem p02647 (C++)

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

lint gcd(lint x, lint y) {
  if (x == 0) {
    return y;
  } else {
    return gcd(y % x, x);
  }
}

lint lcm(lint x, lint y) { return x / gcd(x, y) * y; }

lint C(lint n, lint k) {
  if (n == k) {
    return 1;
  } else if (n < k) {
    return 0;
  } else if (k == 0) {
    return 1;
  } else if (k == 1) {
    return n;
  } else
    return C(n - 1, k - 1) + C(n - 1, k);
}

lint P(lint n, lint k) {
  if (k == 1) {
    return n;
  }
  return (n * (P(n - 1, k - 1) % 1000000007) % 1000000007);
}

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

  for (int i = 0; i < k; i++) {
    vector<lint> c(n + 1, 0);
    for (int j = 0; j < n; j++) {
      c[max(0LL, j - a[j])]++;
      c[min(j + a[j] + 1, n)]--;
    }

    a[0] = c[0];
    for (int j = 1; j < n; j++) {
      a[j] = c[j] + a[j - 1];
    }
  }

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


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

lint gcd(lint x, lint y) {
  if (x == 0) {
    return y;
  } else {
    return gcd(y % x, x);
  }
}

lint lcm(lint x, lint y) { return x / gcd(x, y) * y; }

lint C(lint n, lint k) {
  if (n == k) {
    return 1;
  } else if (n < k) {
    return 0;
  } else if (k == 0) {
    return 1;
  } else if (k == 1) {
    return n;
  } else
    return C(n - 1, k - 1) + C(n - 1, k);
}

lint P(lint n, lint k) {
  if (k == 1) {
    return n;
  }
  return (n * (P(n - 1, k - 1) % 1000000007) % 1000000007);
}

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

  for (int i = 0; i < k; i++) {
    vector<lint> c(n + 1, 0);
    for (int j = 0; j < n; j++) {
      c[max(0LL, j - a[j])]++;
      c[min(j + a[j] + 1, n)]--;
    }

    a[0] = c[0];
    for (int j = 1; j < n; j++) {
      a[j] = c[j] + a[j - 1];
    }

    int check = 1;
    for (int j = 0; j < n; j++) {
      if (a[j] != n) {
        check *= 0;
      }
    }
    if (check == 1) {
      break;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define F first
#define S second

using namespace std;
typedef pair<int, int> P;

const int MOD = 1000000007;
int INF = 100100100100100;
int a[100010];
int imos[100010];
signed main() {
  int n, k;
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  rep(ima, k) {
    rep(i, n) {
      if (i - a[i] >= 0)
        imos[i - a[i]]++;
      else
        imos[0]++;
      if (i + a[i] <= n - 1)
        imos[i + a[i] + 1]--;
      else
        imos[n]--;
    }
    int cnt = 0;
    rep(i, n) {
      if (i == 0)
        a[0] = imos[0];
      else
        a[i] = a[i - 1] + imos[i];
      if (a[i] == n)
        cnt++;
      imos[i] = 0;
    }
    if (cnt == n) {
      break;
    }
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define F first
#define S second

using namespace std;
typedef pair<int, int> P;

const int MOD = 1000000007;
int INF = 100100100100100;
int a[200010];
int imos[200010];
signed main() {
  int n, k;
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  rep(ima, k) {
    rep(i, n) {
      if (i - a[i] >= 0)
        imos[i - a[i]]++;
      else
        imos[0]++;
      if (i + a[i] <= n - 1)
        imos[i + a[i] + 1]--;
      else
        imos[n]--;
    }
    int cnt = 0;
    rep(i, n) {
      if (i == 0)
        a[0] = imos[0];
      else
        a[i] = a[i - 1] + imos[i];
      if (a[i] == n)
        cnt++;
      imos[i] = 0;
    }
    if (cnt == n) {
      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;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
long double eps = 1e-9;
long double pi = acos(-1);

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  while (k--) {
    vector<int> sum(n);
    for (int i = 0; i < n; i++) {
      sum[max(0, i - a[i])]++;
      if (i + a[i] + 1 < n)
        sum[i + 1 + a[i]]--;
    }
    bool ok = 1;
    if (sum[0] != 0)
      ok = false;
    for (int i = 1; i < n; i++) {
      sum[i] += sum[i - 1];
      if (sum[i] != n)
        ok = false;
    }
    swap(a, sum);
    if (ok)
      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;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
long double eps = 1e-9;
long double pi = acos(-1);

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  while (k--) {
    vector<int> sum(n);
    for (int i = 0; i < n; i++) {
      sum[max(0, i - a[i])]++;
      if (i + a[i] + 1 < n)
        sum[i + 1 + a[i]]--;
    }
    bool ok = 1;
    if (sum[0] != n)
      ok = false;
    for (int i = 1; i < n; i++) {
      sum[i] += sum[i - 1];
      if (sum[i] != n)
        ok = false;
    }
    swap(a, sum);
    if (ok)
      break;
  }
  for (int i = 0; i < n; i++)
    cout << a[i] << " ";
  cout << endl;
}
Problem p02647 (C++)

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

int main() {
  int N, K;
  cin >> N;
  cin >> K;
  vector<int> A(N);
  vector<int> flash;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  rep(iterate, K) {
    flash = vector<int>(N, 0);
    rep(i, N) {
      int on = i - A[i];
      int off = i + A[i] + 1;
      if (on < 0) {
        flash[0] += 1;
      } else {
        flash[on] += 1;
      }
      if (off < N) {
        flash[off] -= 1;
      }
    }
    A[0] = flash[0];
    rep(i, N - 1) {
      A[i + 1] = A[i];
      A[i + 1] += flash[i + 1];
    }
  }

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


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

int main() {
  int N, K;
  cin >> N;
  cin >> K;
  vector<int> A(N);
  vector<int> flash;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  rep(iterate, K) {
    flash = vector<int>(N, 0);
    rep(i, N) {
      int on = i - A[i];
      int off = i + A[i] + 1;
      if (on < 0) {
        flash[0] += 1;
      } else {
        flash[on] += 1;
      }
      if (off < N) {
        flash[off] -= 1;
      }
    }
    A[0] = flash[0];
    rep(i, N - 1) {
      A[i + 1] = A[i];
      A[i + 1] += flash[i + 1];
    }
    if (accumulate(A.begin(), A.end(), 0) == N * N) {
      break;
    }
  }

  for (int i = 0; i < N - 1; i++) {
    cout << A.at(i) << " ";
  }
  cout << A.at(A.size() - 1) << endl;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PI 3.14159265358979
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge {
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
// using Graph = vector<vector<Edge>>;
using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (auto &it : A) {
    cin >> it;
  }
  vector<ll> ans = A;
  for (int k = 0; k < K; k++) {
    vector<ll> imos(N + 1, 0);
    for (int i = 0; i < N; i++) {
      ll l = max(0LL, i - ans[i]);
      ll r = min((ll)(N - 1), i + ans[i]);
      imos[l]++;
      imos[r + 1]--;
    }
    bool ok = true;
    int MAX = N;
    if (ans[0] != MAX)
      ok = false;
    ans[0] = imos[0];
    for (int i = 1; i < N; i++) {
      imos[i] += imos[i - 1];
      ans[i] = imos[i];
      if (ans[i] == MAX)
        ok = false;
    }
    if (ok) {
      for (auto it : ans)
        cout << it << " ";
      cout << endl;
      return 0;
    }
  }
  for (auto it : ans)
    cout << it << " ";
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PI 3.14159265358979
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge {
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
// using Graph = vector<vector<Edge>>;
using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (auto &it : A) {
    cin >> it;
  }
  vector<ll> ans = A;
  for (int k = 0; k < K; k++) {
    vector<ll> imos(N + 1, 0);
    for (int i = 0; i < N; i++) {
      ll l = max(0LL, i - ans[i]);
      ll r = min((ll)(N - 1), i + ans[i]);
      imos[l]++;
      imos[r + 1]--;
    }
    bool ok = true;
    int MAX = N;
    if (ans[0] != MAX)
      ok = false;
    ans[0] = imos[0];
    for (int i = 1; i < N; i++) {
      imos[i] += imos[i - 1];
      ans[i] = imos[i];
      if (ans[i] != MAX)
        ok = false;
    }
    if (ok) {
      for (auto it : ans)
        cout << it << " ";
      cout << endl;
      return 0;
    }
  }
  for (auto it : ans)
    cout << it << " ";
  cout << endl;
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

template <typename T> void cout_vec(vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i != 0) {
      cout << ' ';
    }
    cout << vec[i];
  }
  cout << endl;
}

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

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

  for (int i = 0; i < K; i++) {
    vector<int> nw(N + 1, 0);
    for (int j = 0; j < N; j++) {
      nw[max(0, j - vec[j])]++;
      nw[min(N, j + vec[j] + 1)]--;
    }
    ll sum_ = 0;
    for (int k = 0; k < N; k++) {
      nw[k + 1] += nw[k];
      sum_ += (ll)nw[k];
    }
    vec.swap(nw);
    if (sum_ == (ll)N * K) {
      break;
    }
    // cout << vec[0] << endl;
  }
  vec.pop_back();
  cout_vec(vec);
}

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

template <typename T> void cout_vec(vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i != 0) {
      cout << ' ';
    }
    cout << vec[i];
  }
  cout << endl;
}

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

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

  for (int i = 0; i < K; i++) {
    vector<int> nw(N + 1, 0);
    for (int j = 0; j < N; j++) {
      nw[max(0, j - vec[j])]++;
      nw[min(N, j + vec[j] + 1)]--;
    }
    ll sum_ = 0;
    for (int k = 0; k < N; k++) {
      nw[k + 1] += nw[k];
      sum_ += (ll)nw[k];
    }
    vec.swap(nw);
    if (sum_ == (ll)N * N) {
      // cout << i << endl;
      break;
    }
    // cout << vec[0] << endl;
  }
  vec.pop_back();
  cout_vec(vec);
}