alexjercan/bugnet huggingface dataset:
Problem p02647 (C++) — buggy submission, verdict: 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) {
      k = i;
      break;
    }
  }

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

Error: 0

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) {
      break;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << a[k][i] << (i == n - 1 ? "\\n" : " ");
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

ll N, K;
void perm(vector<ll> &A) {
  vector<ll> imos(N + 1, 0);
  for (ll i = 0; i < N; ++i) {
    ll a = A.at(i);
    imos.at(max(0ll, i - a)) += 1;
    imos.at(min(N, i + a + 1)) -= 1;
  }
  for (ll i = 0; i < N; ++i) {
    imos.at(i + 1) += imos.at(i);
  }
  bool all = true;
  for (ll i = 0; i < N; ++i) {
    A.at(i) = imos.at(i);
    if (imos.at(i) != N) {
      all = false;
    }
  }
  if (all) {
    A.at(0) = -1;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  for (ll i = 0; i < K; ++i) {
    perm(A);
    if (A.at(0) == -1) {
      for (ll i = 0; i < N; ++i) {
        cout << N << " ";
      }
      cout << "\\n";
      return 0;
    }
  }
  for (ll i = 0; i < N; ++i) {
    cout << A.at(i) << " ";
  }
  cout << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

ll N, K;
void perm(vector<ll> &A) {
  vector<ll> imos(N + 1, 0);
  for (ll i = 0; i < N; ++i) {
    ll a = A.at(i);
    imos.at(max(0ll, i - a)) += 1;
    imos.at(min(N, i + a + 1)) -= 1;
  }
  for (ll i = 0; i < N; ++i) {
    imos.at(i + 1) += imos.at(i);
  }
  bool all = true;
  for (ll i = 0; i < N; ++i) {
    A.at(i) = imos.at(i);
    if (imos.at(i) != N - 1) {
      all = false;
    }
  }
  if (all) {
    A.at(0) = -1;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  for (ll i = 0; i < K; ++i) {
    perm(A);
    if (A.at(0) == -1) {
      for (ll i = 0; i < N; ++i) {
        cout << N - 1 << " ";
      }
      cout << "\\n";
      return 0;
    }
  }
  for (ll i = 0; i < N; ++i) {
    cout << A.at(i) << " ";
  }
  cout << "\\n";
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  long long int n, k, count = 0, con = 0;

  cin >> n >> k;

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

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

  while (count < k) {
    for (int i = 0; i < n; i++) {
      if (i - a[i] < 0) {
        b[0]++;
      } else {
        b[i - a[i]]++;
      }
      if (i + a[i] + 1 >= n) {
        continue;
      }
      b[i + a[i] + 1]--;
    }
    for (int i = 0; i < n; i++) {
      b[i + 1] += b[i];
      a[i] = b[i];
      b[i] = 0;
    }
    count++;

    if (k - count >= n - a[0]) {
      con++;
    }

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

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

Error: TLE

Fixed submission:

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

int main(void) {
  long long int n, k, count = 0;

  cin >> n >> k;

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

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

  while (count < k) {
    for (int i = 0; i < n; i++) {
      if (i - a[i] < 0) {
        b[0]++;
      } else {
        b[i - a[i]]++;
      }
      if (i + a[i] + 1 >= n) {
        continue;
      }
      b[i + a[i] + 1]--;
    }
    for (int i = 0; i < n; i++) {
      b[i + 1] += b[i];
      a[i] = b[i];
      b[i] = 0;
    }
    count++;
  }

  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\\n";
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, k) {
    vector<ll> imos(n, 0);
    rep(j, 0, n) {
      if (j - a[j] >= 0)
        imos[j - a[j]]++;
      else
        imos[0]++;
      if (j + a[j] + 1 < n)
        imos[j + a[j] + 1]--;
    }
    a[0] = imos[0];
    rep(j, 1, n) a[j] = a[j - 1] + imos[j];
  }
  rep(i, 0, n) {
    cout << a[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << "" << endl;
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

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

int main() {
  cin >> N >> K;
  A.resize(N);
  B.resize(N + 1);
  for (ll &a : A)
    cin >> a;
  for (ll repeat = 0; repeat < K; ++repeat) {
    for (ll &b : B)
      b = 0;
    for (ll i = 0; i < N; ++i) {
      ++B[max(0ll, i - A[i])];
      --B[min(N, i + A[i] + 1)];
    }
    bool end = true;
    for (ll i = 0; i < N; ++i) {
      A[i] = B[i];
      if (i > 0)
        A[i] += A[i - 1];
      if (A[i] != N)
        end = false;
    }
    if (end)
      break;
  }
  for (ll i = 0; i < N; ++i)
    cout << (i == 0 ? "" : " ") << A[i];
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

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

int main() {
  cin >> N >> K;
  A.resize(N);
  B.resize(N + 1);
  for (ll &a : A)
    cin >> a;
  for (ll repeat = 0; repeat < K; ++repeat) {
    for (ll &b : B)
      b = 0;
    for (ll i = 0; i < N; ++i) {
      ++B[max(0ll, i - A[i])];
      --B[min(N, i + A[i] + 1)];
    }
    for (ll i = 1; i <= N; ++i) {
      B[i] += B[i - 1];
    }
    assert(B[N] == 0);
    A = B;
    bool end = true;
    for (ll a : A) {
      if (a != N)
        end = false;
    }
    if (end)
      break;
  }
  for (ll i = 0; i < N; ++i)
    cout << (i == 0 ? "" : " ") << A[i];
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, k;
  cin >> n >> 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(0, i - a[i]);
      int r = min(n, i + a[i] + 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] << " ";
  cout << endl;
  return 0;
}

Error: WA

Fixed submission:

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

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

#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
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 (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T div_floor(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a > 0 ? (a - 1) / b + 1 : a / b;
}

constexpr lint mod = 1e9 + 7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int n, K;
int a[MAX], b[MAX];

bool check() {
  For(i, 1, n + 1) if (a[i] != b[i]) return false;
  return true;
}

int main() {
  scanf("%d%d", &n, &K);
  For(i, 1, n + 1) scanf("%d", &a[i]);
  a[0] = a[n + 1] = 0;

  while (K) {
    --K;
    fill(b, b + n + 2, 0);
    For(i, 1, n + 1) {
      ++b[max(0, i - a[i])];
      --b[min(n + 1, i + a[i] + 1)];
    }
    partial_sum(b, b + n + 2, b);
    if (check())
      break;
    rep(i, n + 2) a[i] = b[i];
  }
  For(i, 1, n + 1) printf("%d ", a[i]);
  printf("\\n");
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
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 (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T div_floor(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T> T div_ceil(T a, T b) {
  if (b < 0)
    a *= -1, b *= -1;
  return a > 0 ? (a - 1) / b + 1 : a / b;
}

constexpr lint mod = 1e9 + 7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int n, K;
int a[MAX], b[MAX];

bool check() {
  rep(i, n) if (a[i] != b[i]) return false;
  return true;
}

int main() {
  scanf("%d%d", &n, &K);
  For(i, 1, n + 1) scanf("%d", &a[i]);
  a[0] = a[n + 1] = 0;

  while (K) {
    --K;
    fill(b, b + n + 2, 0);
    For(i, 1, n + 1) {
      ++b[max(0, i - a[i])];
      --b[min(n + 1, i + a[i] + 1)];
    }
    partial_sum(b, b + n + 2, b);
    if (check())
      break;
    For(i, 1, n + 2) a[i] = b[i];
  }
  For(i, 1, n + 1) printf("%d ", a[i]);
  printf("\\n");
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

typedef long long LL;

constexpr long long INFLL = 1e18;

using namespace std;

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

  for (int l = 0; l < k; l++) {
    vector<int> temp(n + 2, 0);
    for (int i = 1; i <= n; i++) {
      temp.at(max(0, i - a.at(i)))++;
      temp.at(min(n, i + a.at(i)) + 1)--;
    }
    for (int i = 1; i <= n; i++) {
      temp.at(i) += temp.at(i - 1);
    }
    if (a == temp) {

      break;
    }

    a = temp;
  }

  for (int i = 1; i <= n; i++) {
    cout << a.at(i) << " ";
  }
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

typedef long long LL;

constexpr long long INFLL = 1e18;

using namespace std;

int main() {
  int n;
  int k;
  cin >> n >> k;
  vector<int> a(n + 2);
  for (int i = 1; i <= n; i++) {
    cin >> a.at(i);
  }
  const int u_lim = 2 * 1e5;
  k = min((int)log(u_lim) + 1, k);

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

    a = temp;
  }

  for (int i = 1; i <= n; i++) {
    cout << a.at(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;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
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> b(n);
    rep(j, n) {
      int l, r; // 範囲
      l = max(0, j - a[j]);
      r = min(n - 1, j + a[j]);
      b[l]++;
      if (r + 1 < n)
        b[r + 1]--;
    }
    rep(j, n - 1) b[j + 1] += b[j];
    if (a == b)
      break;
    a = b;
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
}

Error: TLE

Fixed submission:

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

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

  if (k >= n) {
    rep(i, n) cout << n << " ";
    cout << endl;
  }
  for (int i = 1; i <= k; i++) {
    vector<ll> b(n);
    rep(j, n) {
      int l, r; // 範囲
      l = max((ll)0, j - a[j]);
      r = min(n - 1, j + a[j]);
      for (int k = l; k <= r; k++) {
        b[k]++;
      }
    }
    ll mina = MOD;
    a = b;
    rep(i, n) mina = min(b[i], mina);
    if (mina >= n)
      break;
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> v_lamps(N);

  for (int n = 0; n < N; ++n) {
    int temp;
    scanf("%d", &temp);
    v_lamps[n] = temp;
  }

  for (int ik = 0; ik < K; ++ik) {
    vector<int> b(N + 1, 0);
    for (int in = 0; in < N; ++in) {
      int l = max(0, in - v_lamps[in]);
      int r = min(in + v_lamps[in] + 1, N);
      b[l]++;
      b[r]--;
    }

    for (int in = 0; in < N; ++in) {
      b[in + 1] += b[in];
    }

    b.pop_back();
    if (v_lamps == b)
      break;
    v_lamps = b;
  }

  for (auto lamp : v_lamps) {
    cout << lamp << " ";
  }
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  if (K > 30)
    K = 30;
  vector<int> v_lamps(N);

  for (int n = 0; n < N; ++n) {
    int temp;
    scanf("%d", &temp);
    v_lamps[n] = temp;
  }

  vector<int> v_new_lamps(N, 0);
  for (int _k = 0; _k < K; _k++) {
    for (int _n = 0; _n < N; _n++) {
      const int radius = v_lamps[_n];
      for (int r = 0; r <= radius; r++) {
        if (r == 0) {
          v_new_lamps[_n]++;
          continue;
        }
        if (_n - r >= 0) {
          v_new_lamps[_n - r]++;
        }
        if (_n + r < N) {
          v_new_lamps[_n + r]++;
        }
      }
    }
    v_lamps = v_new_lamps;
    fill(v_new_lamps.begin(), v_new_lamps.end(), 0);
    // v_new_lamps = vector<int>(N, 0);
  }

  for (auto lamp : v_lamps) {
    cout << lamp << " ";
  }
  cout << endl;

  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
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(N, i + A[i] + 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] << " ";
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
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);
    rep(i, N) {
      int l = max(0, i - A[i]);
      int r = min(N - 1, i + A[i] + 1);
      B[l]++;
      if (r + 1 < N)
        B[r + 1]--;
    }
    rep(i, N) B[i + 1] += B[i];
    if (A == B)
      break;
    A = B;
  }

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

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;

vector<int> func(vector<int> v) {
  int n = v.size();
  vector<int> ret(n, 0);
  for (int i = 0; i < n; i++) {
    for (int j = max(0, i - v[i]); j <= min(n, i + v[i]); j++) {
      ret[j]++;
    }
  }
  return ret;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n), prev(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < k; i++) {
    prev = a;
    a = func(a);
    if (prev == a)
      break;
  }
  for (int j = 0; j < n; j++) {
    cout << a[j] << (j < n - 1 ? ' ' : '\\n');
  }
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 200000;
int n, k;
int las[maxn + 10];
int a[maxn + 10];
int temp[maxn + 10];
int main() {
  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 j = 1; j <= n; ++j) {
      ++temp[max(1, j - a[j])];
      --temp[min(n + 1, j + a[j] + 1)];
    }
    bool flag = 1;
    for (int j = 1; j <= n; ++j) {
      if (a[j] != n)
        flag = 0;
      temp[j] += temp[j - 1];
      a[j] = temp[j];
      temp[j - 1] = 0;
    }
    temp[n] = 0;
    if (flag)
      break;
  }
  for (int i = 1; i <= n; ++i)
    printf("%d ", a[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 200000;
int n, k;
int las[maxn + 10];
int a[maxn + 10];
int temp[maxn + 10];
int main() {
  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 j = 1; j <= n; ++j) {
      for (int l = max(1, j - a[j]); l <= min(n, j + a[j]); ++l)
        ++temp[l];
    }
    bool flag = 1;
    for (int j = 1; j <= n; ++j) {
      if (a[j] != n)
        flag = 0;
      a[j] = temp[j];
      temp[j] = 0;
    }
    if (flag)
      break;
  }
  for (int i = 1; i <= n; ++i)
    printf("%d ", a[i]);
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int accum[N], bulbs[N], n, k;
int main() {
  cin >> n >> k;
  k = min(41, k);
  for (int i = 1; i <= n; ++i)
    cin >> bulbs[i];
  while (k--) {
    memset(accum, 0, sizeof accum);
    for (int i = 1; i <= n; ++i) {
      int l = max(1, i - bulbs[i]);
      int r = min(n, i + bulbs[i]);
      accum[l]++;
      accum[r + 1]--;
    }
    for (int i = 1; i <= n; ++i) {
      accum[i] += accum[i - 1];
    }
    memcpy(bulbs, accum, sizeof accum);
  }
  for (int i = 1; i <= n; ++i) {
    cout << bulbs[i] << " ";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int d[N], n[N], m[N];
int nn, k, sum;
int main() {
  cin >> nn >> k;
  for (int i = 1; i <= nn; ++i) {
    cin >> n[i];
  }
  memset(m, 0, sizeof m);
  for (int i = 1; i <= k; ++i) {
    sum = 0;
    memset(d, 0, sizeof d);
    for (int j = 1; j <= nn; ++j) {
      sum += d[j];
      m[j]++;
      m[j] += sum;
      if (n[j] > 0) {
        d[j + 1]++;
        d[j + n[j] + 1 >= N ? N - 1 : j + n[j] + 1]--;
      }
      // cout << "j:" << j << " d[j]:" << d[j] << " sum:" <<sum << endl;
    }
    /**
    cout << "k1:" << i << endl;
    for (int i = 1; i <= nn; ++i) {
        cout << m[i] << " " ;
    }
    cout << endl;
    **/
    memset(d, 0, sizeof d);
    sum = 0;
    for (int j = nn; j >= 1; --j) {
      sum += d[j];
      // m[j]++;
      m[j] += sum;
      if (n[j] > 0) {
        d[j - 1]++;
        d[j - (n[j] + 1) < 0 ? 0 : j - (n[j] + 1)]--;
      }
    }
    memcpy(n, m, sizeof m);
    memset(m, 0, sizeof m);
    // cout << "k2:" << i << endl;
    /**
    for (int i = 1; i <= nn; ++i) {
        cout << n[i] << " " ;
    }
    cout << endl;
    **/
  }
  for (int i = 1; i <= nn; ++i) {
    cout << n[i] << " ";
  }
  return 0;
}