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

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

#define NDEBUG

std::vector<int> next(std::vector<int> v) {
  std::vector<int> vec(v.size() + 1);
  for (int i = 0; i < (int)v.size(); i++) {
    int L = std::max(0, i - v[i]);
    int R = std::min((int)v.size() - 1, i + v[i]);

    vec[L]++;
    vec[R + 1]--;
  }
  for (int i = 0; i < (int)v.size() - 1; i++)
    vec[i + 1] += vec[i];
  vec.pop_back();
  return vec;
};

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  std::vector<int> a(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

#ifndef NDEBUG
  cout << 0 << ": ";
  for (auto v : a)
    cout << v << " ";
  cout << endl;

  for (int i = 0; i < k; i++) {
    a = next(a);

    cout << i + 1 << ": ";
    for (auto v : a)
      cout << v << " ";
    cout << endl;
  }
#endif

  for (int i = 0; i < k; i++) {
    auto v = next(a);

    if (v == a)
      break;
    a.swap(v);
  }

  for (auto v : a)
    cout << v << " ";
  cout << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

#define NDEBUG

std::vector<int> next(std::vector<int> v) {
  std::vector<int> vec(v.size() + 1);
  for (int i = 0; i < (int)v.size(); i++) {
    int L = std::max(0, i - v[i]);
    int R = std::min((int)v.size() - 1, i + v[i]);

    vec[L]++;
    vec[R + 1]--;
  }
  for (int i = 0; i < (int)v.size() - 1; i++)
    vec[i + 1] += vec[i];
  return vec;
};

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  std::vector<int> a(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

#ifndef NDEBUG
  cout << 0 << ": ";
  for (auto v : a)
    cout << v << " ";
  cout << endl;

  for (int i = 0; i < k; i++) {
    a = next(a);

    cout << i + 1 << ": ";
    for (auto v : a)
      cout << v << " ";
    cout << endl;
  }
#endif

  for (int i = 0; i < k; i++) {
    auto v = next(a);

    if (v == a)
      break;
    a.swap(v);
  }

  for (auto v : a)
    cout << v << " ";
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

// T2020C2.cpp : このファイルには 'main'
// 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//

// #include "pch.h"

#include <algorithm> // copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 2);
  vector<int> b(n + 2);
  vector<int> c_sum(n + 2);
  for (int i = 1; i <= n; i++) {
    cin >> a.at(i);
  }
  for (int j = 0; j < k; j++) {

    for (int i = 1; i <= n; i++) {
      int lp = i - a.at(i);
      if (lp < 0) {
        lp = 0;
      }
      b.at(lp) += 1;
      int rp = i + a.at(i) + 1;
      if (rp > n + 1) {
        rp = n + 1;
      }
      b.at(rp) -= 1;
    }
    c_sum.at(0) = b.at(0);
    for (int i = 1; i <= n; i++) {
      c_sum.at(i) = c_sum.at(i - 1) + b.at(i);
    }
    if (a == c_sum) {
      break;
    } else {
      a = c_sum;
      fill(b.begin(), b.end(), 0);
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << c_sum.at(i) << " ";
  }
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

//
//  main.cpp
//  T2020C
#include <iostream>
#include <vector>
using namespace std;

int main(int argc, const char *argv[]) {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  vector<int> b(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a.at(i);
  }
  for (int count = 1; count <= k; count++) {
    for (int j = 1; j <= n; j++) {
      int dl = j - a.at(j);
      if (dl < 0) {
        dl = 0;
      }
      int dr = j + a.at(j);
      if (dr > n) {
        dr = n;
      }
      for (int k = dl; k <= dr; k++) {
        b.at(k)++;
      }
    }
    for (int j = 1; j <= n; j++) {
      a.at(j) = b.at(j);
      b.at(j) = 0;
    }
  }
  for (int m = 1; m <= n; m++) {
    cout << a.at(m) << " ";
  }
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

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

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

  for (int i = 0; i < k; i++) {
    vector<int> B(n, 0);
    vector<int> C(n, 0);
    for (int j = 0; j < n; j++) {
      int s = max(0, j - A[j]);
      int e = min(j + A[j], n - 1);
      B[s]++;
      if (e + 1 < n)
        B[e + 1]--;
    }
    C.at(0) = B.at(0);
    for (int j = 1; j < n; j++)
      C[j] = C[j - 1] + B[j];

    if (A == C) {
      for (int j = 0; j < n; j++) {
        if (j)
          cout << " ";
        cout << A[j];
      }
      cout << endl;
      return 0;
    }

    for (int j = 0; j < n; j++)
      A[j] = C[j];
  }

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

  return 0;
}

Error: 0

Fixed submission:

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

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

  for (int i = 0; i < k; i++) {
    vector<int> B(n, 0);
    for (int j = 0; j < n; j++) {
      for (int l = 0; l < A[j] + 1; l++) {
        if (j + l < n + 1)
          B[j + l]++;
        if (l != 0 && j - l >= 0)
          B[j - l]++;
      }
    }
    for (int j = 0; j < n; j++)
      A[j] = B[j];
  }

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

  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: 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[i];
  }
  for (int i = 0; i < k; i++) {
    auto a_old = a;
    vector<int> b(n);
    for (int j = 0; j < n; j++) {
      int rmin = max(0, j - a[j]);
      int rmax = min(n - 1, j + a[j]);
      b[rmin]++;
      if (rmax + 1 < n) {
        b[rmax + 1]--;
      }
    }
    for (int j = 0; j < n - 1; j++) {
      b[j + 1] += b[j];
    }
    for (int j = 0; j < n; j++) {
      a[j] = b[j];
    }
    if (a_old == a) {
      break;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    cout << a[i] << " ";
  }
  cout << a[n - 1] << endl;
  return 0;
}


Error: TLE

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[i];
  }
  for (int i = 0; i < k; i++) {
    auto a_old = a;
    vector<int> b(n);
    for (int j = 0; j < n; j++) {
      int rmin = max(0, j - a[j]);
      int rmax = min(n - 1, j + a[j]);
      for (int p = rmin; p <= rmax; p++) {
        b[p]++;
      }
    }
    for (int j = 0; j < n; j++) {
      a[j] = b[j];
    }
    if (a_old == a) {
      break;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    cout << a[i] << " ";
  }
  cout << a[n - 1] << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ve vector

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ve vector

int main() {
  int n, k;
  cin >> n >> k;
  ve<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, k) {
    ve<int> b(n);
    swap(a, b);
    rep(j, n) {
      int l = max(0, (int)j - b[j]);
      a[l]++;
      if (b[j] + j + 1 >= n)
        continue;
      else
        a[b[j] + j + 1]--;
    }
    rep(j, n - 1) a[j + 1] += a[j];
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

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

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

signed main() {

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

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

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

  return 0;
}

Error: WA

Fixed submission:

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

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

signed main() {

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

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

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

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

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> v(N);
  rep(i, N) { cin >> v.at(i); }

  if (K > 40) {
    rep(i, N) { std::cout << N << ' '; }
    std::cout << endl;
    return 0;
  }

  bool f = true;
  vector<int> w(N);
  vector<int> n(N);
  int a = 0, b = 0, c = 0, x, y, z;
  rep(i, K) {
    rep(j, N) {
      if (f) {
        x = max(0, j - v.at(j));
        y = min(N - 1, j + v.at(j));
        w.at(x)++;
        if (y + 1 < N)
          w.at(y + 1)--;
      } else {
        x = max(0, j - w.at(j));
        y = min(N - 1, j + w.at(j));
        v.at(x)++;
        if (y + 1 < N)
          v.at(y + 1)--;
      }
    }
    rep1(j, N) {
      if (f) {
        w.at(j) += w.at(j - 1);
      } else {
        v.at(j) += v.at(j - 1);
      }
    }
    rep(j, N) {
      if (f) {
        v.at(j) = 0;
      } else {
        w.at(j) = 0;
      }
    }
    f = !f;
  }

  if (f) {
    rep(i, N) { std::cout << v.at(i) << ' '; }
  } else {
    rep(i, N) { std::cout << w.at(i) << ' '; }
  }
  std::cout << endl;
}


Error: TLE

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> v(N);
  rep(i, N) { cin >> v.at(i); }

  if (K >= 50) {
    rep(i, N) { std::cout << N << ' '; }
    std::cout << endl;
    return 0;
  }

  bool f = true;
  vector<int> w(N);
  vector<int> n(N);
  int p = 0;
  rep(i, K) {
    rep(j, N) {
      if (f) {
        if (n.at(j) == 1) {
          v.at(j) = 0;
          continue;
        }
        if (v.at(j) == N || (v.at(j) + j > N && -v.at(j) + j < 0)) {
          p++;
          n.at(j) = 1;
        } else {
          for (int k = -v.at(j); k <= v.at(j); k++) {
            if (j + k < 0) {
              k = -j - 1;
              continue;
            } else if (j + k >= N)
              break;
            w.at(j + k)++;
          }
        }
        v.at(j) = 0;
      } else {
        if (n.at(j) == 1) {
          v.at(j) = 0;
          continue;
        }
        if (w.at(j) == N || (w.at(j) + j > N && -w.at(j) + j < 0)) {
          p++;
          n.at(j) = 1;
        } else {
          for (int k = -w.at(j); k <= w.at(j); k++) {
            if (j + k < 0) {
              k = -j - 1;
              continue;
            } else if (j + k >= N)
              break;
            v.at(j + k)++;
          }
        }
        w.at(j) = 0;
      }
    }
    rep(j, N) {
      if (f) {
        w.at(j) += p;
      } else {
        v.at(j) += p;
      }
    }
    f = !f;
  }

  if (f) {
    rep(i, N) { std::cout << v.at(i) << ' '; }
  } else {
    rep(i, N) { std::cout << w.at(i) << ' '; }
  }
  std::cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

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

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

using namespace std;

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

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

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

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

  return 0;
}


Error: WA

Fixed submission:

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

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

using namespace std;

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

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

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

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

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

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

int main(int argc, char **argv) {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll MAX = 200010;
  vector<ll> imos(MAX + 10, 0);

  REP(k, K) {

    REP(i, N) {
      ll l = i - A[i];
      ll r = i + A[i] + 1;
      imos[max(l, 0ll)]++;
      imos[min(r, MAX)]--;
    }

    REP(i, N)
    imos[i + 1] += imos[i];

    if (A == imos)
      break;
    A = imos;
    imos.assign(MAX + 10, 0);
  }

  REP(i, N)
  std::cout << A[i] << " ";
  std::cout << std::endl;
}


Error: TLE

Fixed submission:

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

int main(int argc, char **argv) {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll MAX = 200010;
  vector<ll> imos(MAX + 10, 0);

  REP(k, K) {
    REP(i, N) {
      ll l = i - A[i];
      ll r = i + A[i] + 1;
      imos[max(l, 0ll)]++;
      imos[min(r, MAX)]--;
    }

    REP(i, N)
    imos[i + 1] += imos[i];

    A = imos;
    imos.assign(MAX + 10, 0);
  }

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

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

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

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

Error: WA

Fixed submission:

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

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

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
const int maxn = 3000050;
using namespace std;
typedef long long ll;
int n, k;
int a[maxn], b[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  k = min(k, 100);
  while (k--) {
    for (int i = 0; i < n; i++)
      b[i] = 0;
    for (int i = 0; i < n; i++) {
      int l = max(0, i - a[i]);
      int r = min(n, i + a[i] + 1);
      b[l]++;
      b[r]--;
    }
    a[0] = b[0];
    for (int i = 1; i < n; i++) {
      b[i] += b[i - 1];
      a[i] = b[i];
    }
  }
  for (int i = 0; i < n; i++)
    cout << a[i] << " ";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

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

  rep(k, K) {
    vector<int> B(N, 0);
    rep(i, N) {
      int l = max(0, i - A[i]);
      int r = min(N - 1, i + A[i]);
      B[l]++;

      if (r + 1 < N) {
        B[r + 1]--;
      }
    }

    A[0] = B[0];
    for (int i = 1; i < N; i++) {
      B[i] += B[i - 1];
      A[i] = B[i];
    }
  }

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

  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

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

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

  if (n == 1) {
    cout << min(n, k) << endl;
  }

  else {
    for (int l = 1; l <= min(k, 50); l++) {
      vector<int> B(n, 0);
      for (int i = 0; i < n; i++) {
        int l = max(0, i - A[i]);
        int r = min(i + A[i], n - 1);
        B[l]++;
        if (r + 1 < n) {
          B[r + 1]--;
        }
      }

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

      rep(i, n) {
        A[i] = B[i]; // new A
      }
    }
  }

  rep(i, n) {
    if (i == n - 1) {
      if (A[i] > n) {
        cout << n << endl;
      } else {
        cout << A[i] << endl;
      }
    } else {
      if (A[i] > n) {
        cout << n << endl;
      } else {
        cout << A[i] << " ";
      }
    }
  }
}


Error: 0

Fixed submission:

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

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

  if (n == 1) {
    cout << min(n, k) << endl;
  }

  else {
    for (int l = 1; l <= min(k, 50); l++) {
      vector<int> B(n, 0);
      for (int i = 0; i < n; i++) {
        int l = max(0, i - A[i]);
        int r = min(i + A[i], n - 1);
        B[l]++;
        if (B[r + 1] < n) {
          B[r + 1]--;
        }
      }

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

      rep(i, n) {
        A[i] = B[i]; // new A
      }
    }
  }

  rep(i, n) {
    if (i == n - 1) {
      if (A[i] > n) {
        cout << n << endl;
      } else {
        cout << A[i] << endl;
      }
    } else {
      if (A[i] > n) {
        cout << n << endl;
      } else {
        cout << A[i] << " ";
      }
    }
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < N; i++)
typedef long long ll;
#define dump(x) cerr << #x << "=" << x << endl
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1, 0);
  rep(i, n) cin >> a.at(i + 1);

  k = min(50, k);
  vector<int> b(n + 2, 0);
  for (int j = 0; j < k; j++) {
    rep(i, n) { b.at(i + 1) = 0; }

    for (int i = 1; i <= n; i++) {
      int l = max(1, i - a.at(i));
      int r = min(n, i + a.at(i));

      b.at(l)++;
      if (r < n)
        b.at(r + 1)--;
    }

    for (int i = 2; i <= n; i++) {
      b.at(i) += b.at(i - 1);
    }

    rep(i, n) { a.at(i + 1) = b.at(i + 1); }
  }

  rep(i, n) { cout << a.at(i + 1) << " "; }
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < N; i++)
typedef long long ll;
#define dump(x) cerr << #x << "=" << x << endl
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1, 0);
  rep(i, n) cin >> a.at(i + 1);

  k = min(50, k);
  for (int j = 0; j < k; j++) {
    vector<int> b(n + 2, 0);
    for (int i = 1; i <= n; i++) {
      int l = max(1, i - a.at(i));
      int r = min(n, i + a.at(i));

      b.at(l)++;
      if (r < n)
        b.at(r + 1)--;
    }

    for (int i = 2; i <= n; i++) {
      b.at(i) += b.at(i - 1);
    }

    rep(i, n) {
      dump(b.at(i));
      a.at(i + 1) = b.at(i + 1);
    }
  }

  rep(i, n) { cout << a.at(i + 1) << " "; }
  cout << endl;

  return 0;
}