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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  rep(i, k) {
    bool end_flag = true;
    vector<int> s(n, 0); // a[i] = s[0] + s[1] + ... + s[i]
    rep(i, n) {
      s[max(0, i - a[i])]++;
      if (i + a[i] + 1 < n)
        s[i + a[i] + 1]--;
    }
    a[0] = s[0];
    rep(i, n - 1) { a[i + 1] = a[i] + s[i + 1]; }
    rep(i, n) if (a[i + 1] != n) end_flag = false;
    if (end_flag)
      break;
  }
  rep(i, n) { cout << a[i] << endl; }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  rep(i, k) {
    bool end_flag = true;
    vector<int> s(n, 0); // a[i] = s[0] + s[1] + ... + s[i]
    rep(i, n) {
      s[max(0, i - a[i])]++;
      if (i + a[i] + 1 < n)
        s[i + a[i] + 1]--;
    }
    a[0] = s[0];
    rep(i, n - 1) { a[i + 1] = a[i] + s[i + 1]; }
    rep(i, n) if (a[i] != n) end_flag = false;
    if (end_flag)
      break;
  }
  rep(i, n) { cout << a[i] << endl; }
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
int n, k;
int it[200001];
int pre[200001];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> it[i];
  }
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n + 1; j++) {
      pre[j] = 0;
    }
    for (int j = 0; j < n; j++) {
      pre[max(j - it[j], 0)] += 1;
      pre[min(j + it[j] + 1, n)] -= 1;
    }
    int x = pre[0];
    int st = 0;
    int su = 0;
    for (int j = 0; j < n; j++) {
      su += pre[j];
      if (su + j < n - 1 or su - j > 0) {
        st = 1;
      }
      it[j] = su;
    }
    if (st == 0) {
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << it[i] << " ";
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
int n, k;
int it[200001];
int pre[200001];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> it[i];
  }
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n + 1; j++) {
      pre[j] = 0;
    }
    for (int j = 0; j < n; j++) {
      pre[max(j - it[j], 0)] += 1;
      pre[min(j + it[j] + 1, n)] -= 1;
    }
    int x = pre[0];
    int st = 0;
    int su = 0;
    for (int j = 0; j < n; j++) {
      su += pre[j];
      if (it[j] != x) {
        st = 1;
      }
      it[j] = su;
    }
    if (st == 0) {
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << it[i] << " ";
  }
  cout << endl;

  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const ll INF = 2e18;
template <class T> void print(const T &t) { cout << t << endl; }
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;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n, k;
  cin >> n >> k;
  vl a(n + 2);
  a[0] = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  vl dp(n + 1, 0);
  rep(i, k) {
    for (ll j = 1; j <= n; j++) {
      ll in = j - a[j];
      if (in <= 0)
        in = 1;
      ll out = j + a[j] + 1;
      if (out > n)
        out = n + 1;
      dp[in]++;
      dp[out]--;
    }
    ll count = 0;
    for (ll j = 1; j <= n; j++) {
      count += dp[j];
      a[j] = count;
      dp[j] = 0;
    }
  }
  for (ll i = 1; i <= n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const ll INF = 2e18;
template <class T> void print(const T &t) { cout << t << endl; }
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;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n, k;
  cin >> n >> k;
  vl a(n + 2);
  a[0] = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  if (k >= 10000) {
    for (ll i = 1; i <= n; i++) {
      cout << n << " ";
    }
    cout << endl;
    return 0;
  }
  vl dp(n + 1, 0);
  rep(i, k) {
    for (ll j = 1; j <= n; j++) {
      ll in = j - a[j];
      if (in <= 0)
        in = 1;
      ll out = j + a[j] + 1;
      if (out > n)
        out = n + 1;
      dp[in]++;
      dp[out]--;
    }
    ll count = 0;
    for (ll j = 1; j <= n; j++) {
      count += dp[j];
      a[j] = count;
      dp[j] = 0;
    }
  }
  for (ll i = 1; i <= n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author NikuKyabe
 */

#include "bits/stdc++.h"

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

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

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


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

#include "bits/stdc++.h"

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

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

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

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

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

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

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

    if (ok)
      break;
  }

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

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

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

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

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

    if (ok)
      break;
  }

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

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

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) {
  int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 200005;

int N, K;
int A[2][MAX_N];

signed main() {
  cin >> N >> K;
  REP(n, N) { cin >> A[0][n]; }

  int now, next;
  REP(k, min((LL)50, K)) {
    now = k % 2;
    next = (k + 1) % 2;
    REP(n, N) { memset(A[next], 0, sizeof(A[next])); }
    REP(n, N) {
      int l = max((LL)0, n - A[now][n]);
      int r = min((LL)N, n + A[now][n] + 1);
      A[next][l]++;
      A[next][r]--;
    }
    REP(n, N) { A[next][n + 1] += A[next][n]; }
  }

  REP(n, N) {
    if (0 < n) {
      cout << " ";
    }
    cout << A[next][n];
  }
  cout << endl;

  return 0;
}


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

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) {
  int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 200005;

int N, K;
int A[2][MAX_N];

signed main() {
  cin >> N >> K;
  REP(n, N) { cin >> A[0][n]; }

  int now, next;
  REP(k, min((LL)50, K)) {
    now = k % 2;
    next = (k + 1) % 2;

    memset(A[next], 0, sizeof(A[next]));

    REP(n, N) {
      int l = max((LL)0, n - A[now][n]);
      int r = min((LL)N, n + A[now][n] + 1);
      A[next][l]++;
      A[next][r]--;
    }
    REP(n, N) { A[next][n + 1] += A[next][n]; }
  }

  REP(n, N) {
    if (0 < n) {
      cout << " ";
    }
    cout << A[next][n];
  }
  cout << endl;

  return 0;
}
Problem p02647 (C++)

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

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  vector<int> imo(n + 1);
  /*いもす法*/
  rep(j, min(k, (int)log2(n) + 100)) {
    imo = vector<int>(n + 1);
    rep(i, n) {
      imo[max(0, i - v[i])]++;
      imo[i + min(v[i] + 1, n)]--;
    }
    int bef = 0;
    rep(i, n) {
      bef += imo[i];
      v[i] = bef;
    }
  }
  rep(i, n) cout << v[i] << " ";
  cout << endl;
  return 0;
}

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

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  vector<int> imo(n + 1);
  /*いもす法*/
  rep(j, min(k, (int)log2(n) + 100)) {
    imo = vector<int>(n + 1);
    rep(i, n) {
      imo[max(0, i - v[i])]++;
      imo[min(i + (v[i] + 1), n)]--;
    }
    int bef = 0;
    rep(i, n) {
      bef += imo[i];
      v[i] = bef;
    }
  }
  rep(i, n) cout << v[i] << " ";
  cout << endl;
  return 0;
}
Problem p02647 (C++)

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

int main() {

  ll N, K;
  cin >> N >> K;
  vl A(N);
  rep(i, N) cin >> A[i];

  ll n = 0;
  while (n < K) {
    vl B(N + 1, 0);
    rep(i, N) {
      if (i - A[i] <= 0)
        B[0]++;
      else
        B[i - A[i]]++;
      if (i + A[i] > N)
        B[N]--;
      else
        B[i + A[i] + 1]--;
    }
    vl C(N, 0);
    bool ok = true;
    rep(i, N) {
      if (i > 0)
        C[i] = C[i - 1];
      C[i] += B[i];
      if (C[i] < N)
        ok = false;
    }
    if (ok) {
      rep(i, N) { cout << N << " "; }
      cout << endl;
      return 0;
    }
    n++;
    A = C;
  }

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

  return 0;
}


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

int main() {

  ll N, K;
  cin >> N >> K;
  vl A(N);
  rep(i, N) cin >> A[i];

  ll n = 0;
  while (n < K) {
    vl 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[N]--;
      else
        B[i + A[i] + 1]--;
    }
    vl C(N, 0);
    bool ok = true;
    rep(i, N) {
      if (i > 0)
        C[i] = C[i - 1];
      C[i] += B[i];
      if (C[i] < N)
        ok = false;
    }
    if (ok) {
      rep(i, N) { cout << N << " "; }
      cout << endl;
      return 0;
    }
    n++;
    A = C;
  }

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

  return 0;
}
Problem p02647 (C++)

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

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define PI (acos(-1.0))
#define MODN 1000000007
#define MODN2 998244353
#define ALL(V) (V).begin(), (V).end()
#define INT_MAX_HALF (INT_MAX / 2)
#define EPS (1e-10)

using namespace std;
typedef long long ll;

int main() {

  ll n, k;
  cin >> n >> k;

  vector<int> light(n + 2);

  rep(i, n) { cin >> light[i + 1]; }

  // imos法をつかう
  vector<int> count(n + 2);

  for (int j = 0; j < k; j++) {

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

      if (i - light[i] < 1) {
        count[0]++;
      } else {
        count[i - light[i]]++;
      }

      if (i + light[i] + 1 > n) {
        count[n + 1]--;
      } else {
        count[i + light[i] + 1]--;
      }
    }

    ll sum = 0;

    for (int i = 1; i <= n + 1; i++) {
      count[i] = count[i] + count[i - 1];
      light[i] = count[i];

      if (i != n)
        sum += count[i];
    }

    if (sum == n * n)
      break;

    rep(i, n + 2) { count[i] = 0; }
  }

  for (int i = 1; i <= n; i++) {
    cout << light[i] << " ";
  }

  cout << endl;

  return 0;
}


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

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define PI (acos(-1.0))
#define MODN 1000000007
#define MODN2 998244353
#define ALL(V) (V).begin(), (V).end()
#define INT_MAX_HALF (INT_MAX / 2)
#define EPS (1e-10)

using namespace std;
typedef long long ll;

int main() {

  ll n, k;
  cin >> n >> k;

  vector<int> light(n + 2);

  rep(i, n) { cin >> light[i + 1]; }

  // imos法をつかう
  vector<int> count(n + 2);

  for (int j = 0; j < k; j++) {

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

      if (i - light[i] < 1) {
        count[0]++;
      } else {
        count[i - light[i]]++;
      }

      if (i + light[i] + 1 > n) {
        count[n + 1]--;
      } else {
        count[i + light[i] + 1]--;
      }
    }

    ll sum = 0;

    for (int i = 1; i <= n + 1; i++) {
      count[i] = count[i] + count[i - 1];
      light[i] = count[i];

      if (i != n + 1)
        sum += count[i];
    }

    if (sum == n * n)
      break;

    rep(i, n + 2) { count[i] = 0; }
  }

  for (int i = 1; i <= n; i++) {
    cout << light[i] << " ";
  }

  cout << endl;

  return 0;
}
Problem p02647 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n, 0);
  vector<pair<int, int>> x(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    int left = i - a[i];
    int right = i + a[i];
    if (left < 0)
      left = 0;
    if (right > n - 1)
      right = n - 1;
    x[i] = make_pair(left, right);
  }
  // sort(x.begin(),x.end());
  vector<int> ans(n + 1, 0);
  for (int i = 0; i < n; ++i) {
    ans[x[i].first]++;
    ans[x[i].second + 1]--;
  }
  for (int i = 0; i < n; ++i) {
    ans[i + 1] += ans[i];
  }
  k--;
  bool ok = false;
  vector<int> tmp(n + 1, 0);
  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < n; ++j) {
      if (ok)
        break;
      int left = j - ans[j];
      int right = j + ans[j];
      if (left < 0)
        left = 0;
      if (right > n - 1)
        right = n - 1;
      tmp[left]++;
      tmp[right + 1]--;
    }
    for (int m = 0; m < n; ++m) {
      tmp[m + 1] += tmp[m];
      ans[m + 1] = tmp[m + 1];
    }
    ans[0] = tmp[0];
    for (int l = 0; l < n; ++l) {
      if (ans[l] >= n) {
        ans[l] = n;
        ok = true;
      } else {
        ok = false;
        break;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (ans[i] > n)
      ans[i] = n;
    if (i != n - 1) {
      cout << ans[i] << " ";
    } else {
      cout << ans[i] << endl;
    }
  }
  return 0;
}


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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n, 0);
  vector<pair<int, int>> x(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    int left = i - a[i];
    int right = i + a[i];
    if (left < 0)
      left = 0;
    if (right > n - 1)
      right = n - 1;
    x[i] = make_pair(left, right);
  }
  // sort(x.begin(),x.end());
  vector<int> ans(n + 1, 0);
  for (int i = 0; i < n; ++i) {
    ans[x[i].first]++;
    ans[x[i].second + 1]--;
  }
  for (int i = 0; i < n; ++i) {
    ans[i + 1] += ans[i];
  }
  k--;
  bool ok = false;
  vector<int> tmp(n + 1, 0);
  for (int i = 0; i < k; ++i) {
    if (ok)
      break;
    tmp = vector<int>(n + 1, 0);
    for (int j = 0; j < n; ++j) {
      if (ok)
        break;
      int left = j - ans[j];
      int right = j + ans[j];
      if (left < 0)
        left = 0;
      if (right > n - 1)
        right = n - 1;
      tmp[left]++;
      tmp[right + 1]--;
    }
    for (int m = 0; m < n; ++m) {
      tmp[m + 1] += tmp[m];
      ans[m + 1] = tmp[m + 1];
    }
    ans[0] = tmp[0];
    for (int l = 0; l < n; ++l) {
      if (ans[l] >= n) {
        ans[l] = n;
        ok = true;
      } else {
        ok = false;
        break;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (ans[i] > n)
      ans[i] = n;
    if (i != n - 1) {
      cout << ans[i] << " ";
    } else {
      cout << ans[i] << endl;
    }
  }
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int a[200005] = {0}, b[200005] = {0};
int main() {
  int n, k, l, r, cnt = 0;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  for (int i = 1; i <= k; i++) {
    cnt = 0;
    for (int j = 1; j <= n; j++) {
      if (a[j] == n)
        cnt++;
    }
    if (cnt == n)
      break;
    memset(b, 0, sizeof(b));
    for (int j = 1; j <= n; j++) {
      l = j - a[j];
      r = j + a[j];
      if (l < 1)
        l = 1;
      if (r > n)
        r = n;
      for (int ii = l; ii <= r; ii++) {
        b[ii]++;
      }
    }
    for (int j = 1; j <= n; j++) {
      a[j] = b[j];
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d ", b[i]);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int a[200005] = {0}, b[200005] = {0};
int main() {
  int n, k, l, r, cnt = 0;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  for (int i = 1; i <= k; i++) {
    cnt = 0;
    for (int j = 1; j <= n; j++) {
      if (a[j] == n)
        cnt++;
    }
    if (cnt == n)
      break;
    memset(b, 0, sizeof(b));
    for (int j = 1; j <= n; j++) {
      l = j - a[j];
      r = j + a[j];
      if (l < 1)
        l = 1;
      if (r > n)
        r = n;
      b[l]++;
      if (r + 1 <= n)
        b[r + 1]--;
    }
    for (int j = 2; j <= n; j++) {
      b[j] += b[j - 1];
    }
    for (int j = 1; j <= n; j++) {
      a[j] = b[j];
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d ", b[i]);
  }
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using ULL = unsigned long long;
using LL = long long;
using String = std::string;
using Ints = std::vector<int>;
using Doubles = std::vector<double>;
using Bools = std::vector<bool>;
using Strings = std::vector<std::string>;
using ULLs = std::vector<ULL>;
using LLs = std::vector<LL>;

// pythonのdictionary相当
template <typename T1, typename T2> using Dict = std::unordered_map<T1, T2>;

int main(void) {
  int n, k;
  std::cin >> n >> k;
  Ints A(n);
  for (int i = 0; i < n; i++) {
    int _a;
    std::cin >> _a;
    A[i] = _a;
  }

  for (int i = 0; i < k; i++) {
    Ints B(n + 1, 0);
    for (int j = 0; j < n; j++) {
      B[std::max(0, j - A[j])]++;
      B[std::min(n, j + A[j] + 1)]--;
    }
    for (int j = 0; j < n; j++) {
      B[j + 1] += B[j];
    }
    A = B;
  }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using ULL = unsigned long long;
using LL = long long;
using String = std::string;
using Ints = std::vector<int>;
using Doubles = std::vector<double>;
using Bools = std::vector<bool>;
using Strings = std::vector<std::string>;
using ULLs = std::vector<ULL>;
using LLs = std::vector<LL>;

// pythonのdictionary相当
template <typename T1, typename T2> using Dict = std::unordered_map<T1, T2>;

int main(void) {
  int n, k;
  std::cin >> n >> k;
  Ints A(n);
  for (int i = 0; i < n; i++) {
    int _a;
    std::cin >> _a;
    A[i] = _a;
  }

  for (int i = 0; i < std::min(50, k); i++) {
    Ints B(n + 1, 0);
    for (int j = 0; j < n; j++) {
      B[std::max(0, j - A[j])]++;
      B[std::min(n, j + A[j] + 1)]--;
    }
    for (int j = 0; j < n; j++) {
      B[j + 1] += B[j];
    }
    A = B;
  }

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

  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(v)                                                               \
  cout << #v << ":";                                                           \
  for (auto x : v) {                                                           \
    cout << x << ' ';                                                          \
  }                                                                            \
  cout << endl;
#define INF 1000000000
#define mod 1000000007
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

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

  rep(_, k) {
    if (*min_element(ALL(a)) == n) {
      rep(i, n) cout << n << (i + 1 == n ? "\n" : " ");
      return 0;
    }

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

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

    int p = 0;
    rep(i, n) {
      p += imos[i];
      b[i] = p;
    }
    swap(a, b);
  }
  rep(i, n) cout << a[i] << (i + 1 == n ? "\n" : " ");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(v)                                                               \
  cout << #v << ":";                                                           \
  for (auto x : v) {                                                           \
    cout << x << ' ';                                                          \
  }                                                                            \
  cout << endl;
#define INF 1000000000
#define mod 1000000007
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

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

  rep(_, k) {
    if (*min_element(ALL(a)) == n) {
      rep(i, n) cout << n << (i + 1 == n ? "\n" : " ");
      return 0;
    }

    vector<int> imos(n + 1, 0), b(n);

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

    int p = 0;
    rep(i, n) {
      p += imos[i];
      b[i] = p;
    }
    swap(a, b);
  }
  rep(i, n) cout << a[i] << (i + 1 == n ? "\n" : " ");
  return 0;
}
Problem p02647 (C++)

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int lld;
typedef unsigned long long int llu;
#define INF (lld)(9223372036854775807)
#define IINF (int)(2147483647)

using namespace std;

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> a;
  a.push_back(vector<int>(n + 1));
  for (int i = 0; i < n; i++) {
    cin >> a[0][i];
  }
  for (int i = 1; i <= k; i++) {
    int ms = 0;
    a.push_back(vector<int>(n + 1));
    for (int j = 0; j < n; j++) {
      a[i][max(0, j - a[i - 1][j])]++;

      a[i][min(n, j + a[i - 1][j] + 1)]--;
    }
    int c = 0;
    for (int j = 0; j < n; j++) {
      c += a[i][j];
      a[i][j] = c;
      if (c == n) {
        ms++;
      }
    }
    if (ms == n) {
      break;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << a[k][i] << (i == n - 1 ? "\n" : " ");
  }
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long int lld;
typedef unsigned long long int llu;
#define INF (lld)(9223372036854775807)
#define IINF (int)(2147483647)

using namespace std;

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> a;
  a.push_back(vector<int>(n + 1));
  for (int i = 0; i < n; i++) {
    cin >> a[0][i];
  }
  for (int i = 1; i <= k; i++) {
    int ms = 0;
    a.push_back(vector<int>(n + 1));
    for (int j = 0; j < n; j++) {
      a[i][max(0, j - a[i - 1][j])]++;

      a[i][min(n, j + a[i - 1][j] + 1)]--;
    }
    int c = 0;
    for (int j = 0; j < n; j++) {
      c += a[i][j];
      a[i][j] = c;
      if (c == n) {
        ms++;
      }
    }
    if (ms == n) {
      k = i;
      break;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << a[k][i] << (i == n - 1 ? "\n" : " ");
  }
}
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++)
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> c(n, n);
  rep(i, n) { cin >> a[i]; }
  rep(j, k) {
    vector<int> d(n + 1);
    rep(i, n) {
      d[max(i - a[i], 0)]++;
      d[min(i + a[i] + 1, n)]--;
    }
    vector<int> e(n, d[0]);
    rep(i, n) { e[i + 1] = e[i] + d[i + 1]; }
    a = e;
    if (c == e) {
      break;
    }
  }
  rep(i, n - 1) { cout << a[i] << ' '; }
  cout << a[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> c(n, n);
  rep(i, n) { cin >> a[i]; }
  rep(j, k) {
    vector<int> d(n + 1);
    rep(i, n) {
      d[max(i - a[i], 0)]++;
      d[min(i + a[i] + 1, n)]--;
    }
    vector<int> e(n, d[0]);
    rep(i, n - 1) { e[i + 1] = e[i] + d[i + 1]; }
    a = e;
    if (c == e) {
      break;
    }
  }
  rep(i, n - 1) { cout << a[i] << ' '; }
  cout << a[n - 1] << endl;
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define int long long

void operation(vector<int> &a) {
  vector<int> b(a.size(), 0);
  for (int i = 0; i < (long long)a.size(); i++) {
    int start = i - a[i];
    int end = i + a[i] + 1;
    if (start < 0)
      start = 0;
    b[start]++;
    if (end <= (long long)a.size())
      b[end]--;
  }

  for (int i = 1; i < (long long)a.size(); i++) {
    b[i] += b[i - 1];
  }

  a = b;
}

signed main() {
  cin.tie(0);
  ios::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++) {
    operation(a);
    if (a == vector<int>(n, n))
      break;
  }

  for (auto &&ai : a) {
    cout << ai << " ";
  }
  cout << endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define int long long

void operation(vector<int> &a) {
  vector<int> b(a.size(), 0);
  for (int i = 0; i < (long long)a.size(); i++) {
    int start = i - a[i];
    int end = i + a[i] + 1;
    if (start < 0)
      start = 0;
    b[start]++;
    if (end < (long long)a.size())
      b[end]--;
  }

  for (int i = 1; i < (long long)a.size(); i++) {
    b[i] += b[i - 1];
  }

  a = b;
}

signed main() {
  cin.tie(0);
  ios::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++) {
    operation(a);
    if (a == vector<int>(n, n))
      break;
  }

  for (auto &&ai : a) {
    cout << ai << " ";
  }
  cout << endl;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long int ll;
typedef unsigned long long ull;
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;
}
typedef pair<ll, ll> P;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> tasu(n);
  vector<int> h(n);

  rep(i, n) {
    cin >> a[i];
    vector<ll> tasu(n);
    vector<ll> h(n);
  }
  if (n <= k) {
    rep(i, n) cout << n << " ";
    cout << endl;
    return 0;
  }
  rep(t, k) {
    tasu = h;
    rep(i, n) {
      tasu[max(0, i - a[i])]++;
      int tasuin = i + a[i] + 1;
      if (tasuin < n)
        tasu[tasuin]--;
    }
    ll now = 0;
    bool finish = true;
    rep(i, n - 1) { tasu[i + 1] += tasu[i]; }
    if (tasu == a)
      break;
    a = tasu;
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long int ll;
typedef unsigned long long ull;
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;
}
typedef pair<ll, ll> P;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> tasu(n);
  vector<int> h(n);

  rep(i, n) {
    cin >> a[i];
    tasu[i];
    h[i];
  }
  if (n <= k) {
    rep(i, n) cout << n << " ";
    cout << endl;
    return 0;
  }
  rep(t, k) {
    tasu = h;
    rep(i, n) {
      tasu[max(0, i - a[i])]++;
      int tasuin = i + a[i] + 1;
      if (tasuin < n)
        tasu[tasuin]--;
    }
    ll now = 0;
    bool finish = true;
    rep(i, n - 1) { tasu[i + 1] += tasu[i]; }
    if (tasu == a)
      break;
    a = tasu;
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <numeric>

const int MaxN = 100005;

int n, A[MaxN];

bool trans() {
  static int B[MaxN];
  std::fill(B, B + n, 0);
  for (int i = 0; i < n; ++i) {
    int l = std::max(0, i - A[i]);
    int r = std::min(n - 1, i + A[i]);
    B[l] += 1;
    B[r + 1] -= 1;
  }
  bool flag = true;
  for (int i = 0; i < n; ++i) {
    flag &= A[i] == B[i];
    A[i] = B[i];
    B[i + 1] += B[i];
  }
  return flag;
}

int main() {
  int q;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; ++i)
    scanf("%d", A + i);
  while (q--) {
    if (trans())
      break;
  }
  for (int i = 0; i < n; ++i)
    printf("%d ", A[i]);
  puts("");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <numeric>

const int MaxN = 200005;

int n, A[MaxN];

bool trans() {
  static int B[MaxN];
  std::fill(B, B + n, 0);
  for (int i = 0; i < n; ++i) {
    int l = std::max(0, i - A[i]);
    int r = std::min(n - 1, i + A[i]);
    B[l] += 1;
    B[r + 1] -= 1;
  }
  bool flag = true;
  for (int i = 0; i < n; ++i) {
    flag &= A[i] == B[i];
    A[i] = B[i];
    B[i + 1] += B[i];
  }
  return flag;
}

int main() {
  int q;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; ++i)
    scanf("%d", A + i);
  while (q--) {
    if (trans())
      break;
  }
  for (int i = 0; i < n; ++i)
    printf("%d ", A[i]);
  puts("");
  return 0;
}
Problem p02647 (C++)

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

#define FOR(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, N) FOR(i, 0, N)

#define MOD 1000000007
#define INF 1000000000

using ll = long long int;
using namespace std;

typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

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

  rep(i, K) {
    bool flag = true;
    vi B(N, 0);
    rep(j, N) {
      if (flag && A[j] != N - 1)
        flag = false;
      B[max(0, (int)j - A[j])]++;
      if ((int)j + A[j] < N - 1)
        B[(int)j + A[j] + 1]--;
    }

    rep(j, N - 1) B[j + 1] += B[j];
    rep(j, N) A[j] = B[j];
    if (flag)
      break;
  }

  rep(i, N) { cout << A[i] << "\n"; }
  return 0;
}


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

#define FOR(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, N) FOR(i, 0, N)

#define MOD 1000000007
#define INF 1000000000

using ll = long long int;
using namespace std;

typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

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

  rep(i, K) {
    bool flag = true;
    vi B(N, 0);
    rep(j, N) {
      if (flag && A[j] < N)
        flag = false;
      B[max(0, (int)j - A[j])]++;
      if ((int)j + A[j] < N - 1)
        B[(int)j + A[j] + 1]--;
    }

    rep(j, N - 1) B[j + 1] += B[j];
    rep(j, N) A[j] = B[j];
    if (flag)
      break;
  }

  rep(i, N) { cout << A[i] << "\n"; }
  return 0;
}
Problem p02647 (C++)

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

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)

const int mxN = 1e5 + 5;
int N, K, A[mxN];
int ft[mxN];

void u(int i, int x) {
  for (; i <= N; i += i & -i)
    ft[i] += x;
}
void update(int i, int j, int x) {
  u(i, x);
  u(j + 1, -x);
}
int query(int i) {
  int r = 0;
  for (; i; i -= i & -i)
    r += ft[i];
  return r;
}

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

  cin >> N >> K;
  FOR(i, 1, N) {
    cin >> A[i];
    update(max(1, i - A[i]), min(N, i + A[i]), 1);
  }

  FOR(i, 1, K) {
    bool brk = 1;
    FOR(j, 1, N) {
      A[j] = query(j);
      brk &= A[j] == N;
    }
    if (brk)
      break;
    memset(ft, 0, sizeof ft);
    FOR(j, 1, N) { update(max(1, j - A[j]), min(N, j + A[j]), 1); }
  }
  FOR(i, 1, N) { cout << A[i] << ' '; }
  cout << '\n';
}


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

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)

const int mxN = 2e5 + 5;
int N, K, A[mxN];
int ft[mxN];

void u(int i, int x) {
  for (; i <= N; i += i & -i)
    ft[i] += x;
}
void update(int i, int j, int x) {
  u(i, x);
  u(j + 1, -x);
}
int query(int i) {
  int r = 0;
  for (; i; i -= i & -i)
    r += ft[i];
  return r;
}

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

  cin >> N >> K;
  FOR(i, 1, N) {
    cin >> A[i];
    update(max(1, i - A[i]), min(N, i + A[i]), 1);
  }

  FOR(i, 1, K) {
    bool brk = 1;
    FOR(j, 1, N) {
      A[j] = query(j);
      brk &= A[j] == N;
    }
    if (brk)
      break;
    memset(ft, 0, sizeof ft);
    FOR(j, 1, N) { update(max(1, j - A[j]), min(N, j + A[j]), 1); }
  }
  FOR(i, 1, N) { cout << A[i] << ' '; }
  cout << '\n';
}
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 REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
using P = pair<int, int>;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  REP(i, K) {
    vector<int> imos(N + 1, 0);
    REP(j, N) {
      imos.at(max(0, j - A[j]))++;
      imos.at(min(N, j + A[j] + 1))--;
    }
    int sum = 0;
    REP(j, N) {
      sum += imos.at(j);
      A[j] = sum;
    }
  }

  REP(i, N) {
    if (i != N - 1) {
      cout << A[i] << " ";
    } else {
      cout << A[i];
    }
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
using P = pair<int, int>;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  REP(i, K) {
    vector<int> imos(N + 1, 0);
    REP(j, N) {
      imos.at(max(0, j - A[j]))++;
      imos.at(min(N, j + A[j] + 1))--;
    }
    int sum = 0;
    REP(j, N) {
      sum += imos.at(j);
      A[j] = sum;
    }
    int flag = 0;
    REP(j, N) {
      if (A[j] != N) {
        flag = 1;
      }
    }
    if (flag == 0) {
      break;
    }
  }

  REP(i, N) {
    if (i != N - 1) {
      cout << A[i] << " ";
    } else {
      cout << A[i];
    }
  }
  cout << endl;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

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

  if (n * 4 <= k) {
    for (int i = 0; i < n; i++)
      cout << n << " ";
    cout << endl;
    return 0;
  }

  for (int i = 0; i < k; i++) {
    for (int j = 0; j <= n; j++) {
      task[j] = 0;
    }
    int temp = 0;
    for (int now = 0; now < n; now++) {
      task[max(0, now - a[now])] += 1;
      task[min(n, now + a[now] + 1)] -= 1;
    }
    for (int now = 0; now < n; now++) {
      temp += task[now];
      a[now] = temp;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

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

  if (k > 1000) {
    for (int i = 0; i < n; i++)
      cout << n << " ";
    cout << endl;
    return 0;
  }

  for (int i = 0; i < k; i++) {
    for (int j = 0; j <= n; j++) {
      task[j] = 0;
    }
    int temp = 0;
    for (int now = 0; now < n; now++) {
      task[max(0, now - a[now])] += 1;
      task[min(n, now + a[now] + 1)] -= 1;
    }
    for (int now = 0; now < n; now++) {
      temp += task[now];
      a[now] = temp;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ll long long
#define inf 0x3f3f3f3f
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int N = 3e5 + 10;
const ll mod = 1e9 + 7;
ll pre[N], arr[N], k, n;
int main() {
  fast cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  while (k--) {

    for (ll i = 1; i <= n; i++) {
      ll x = max(1ll, i - arr[i]);
      ll y = min(n, arr[i] + i);
      pre[x] += 1;
      pre[y + 1] -= 1;
    }
    for (ll i = 1; i <= n; i++)
      pre[i] += pre[i - 1];
    for (int i = 1; i <= n; i++) {
      arr[i] = pre[i];
      pre[i] = 0;
    }
  }
  for (ll i = 1; i <= n; i++)
    cout << arr[i] << " ";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define endl "\n"
#define se second
#define ll long long
#define inf 0x3f3f3f3f
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int N = 3e5 + 10;
const ll mod = 1e9 + 7;
ll pre[N], arr[N], k, n;
int main() {
  fast cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  while (k--) {
    bool ans = true;
    for (int i = 1; i <= n; i++)
      if (arr[i] != n)
        ans = false;
    if (ans)
      break;
    for (ll i = 1; i <= n; i++) {
      ll x = max(1ll, i - arr[i]);
      ll y = min(n, arr[i] + i);
      pre[x] += 1;
      pre[y + 1] -= 1;
    }
    for (ll i = 1; i <= n; i++)
      pre[i] += pre[i - 1];
    for (int i = 1; i <= n; i++) {
      arr[i] = pre[i];
      pre[i] = 0;
    }
  }
  for (ll i = 1; i <= n; i++)
    cout << arr[i] << " ";
}