alexjercan/bugnet huggingface dataset:
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

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

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

Error: TLE

Fixed submission:

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

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

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

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

  rep(i, N) cout << A[i] << ' ';
  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll sum[200010];

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


Error: TLE

Fixed submission:

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

ll sum[200010];

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i + 1];
  for (int q = 0; q < k; q++) {
    for (int i = 1; i <= n; i++) {
      sum[max(1LL, i - a[i])]++;
      sum[min(n + 1, i + a[i] + 1)]--;
    }
    for (int i = 1; i <= n + 1; i++) {
      sum[i] += sum[i - 1];
    }
    for (int i = 1; i <= n; i++) {
      a[i] = sum[i];
    }
    for (int i = 0; i <= n + 1; i++)
      sum[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (i == n)
      cout << a[i] << endl;
    else
      cout << a[i] << " ";
  }
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

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

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

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, r;
      l = max(0, i - a[i]);
      r = min(n, i + a[i]);
      ++b[l];
      --b[r + 1];
    }
    rep(i, n + 1) b[i + 1] += b[i];
    b.pop_back();
    if (a == b)
      break;
    else
      a = b;
  }

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

  return 0;
}

Error: 0

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++)

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, r;
      l = max(0, i - a[i]);
      r = min(n + 1, i + a[i]);
      ++b[l];
      --b[r + 1];
    }
    rep(i, n + 1) b[i + 1] += b[i];
    b.pop_back();
    if (a == b)
      break;
    else
      a = b;
  }

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

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

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

// #define int long long
typedef long long ll;
// const int INF = 2e9;
// const ll INF = 9e18;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<int> state(N + 1);
  for (int k = 0; k < min(K, 50); k++) {
    for (int i = 0; i < N + 1; i++) {
      state[i] = 0;
    }
    for (int i = 0; i < N; i++) {
      if (i - A[i] < 0)
        state[0]++;
      else
        state[i - A[i]]++;
      if (i + 1 + A[i] > N)
        state[N]--;
      else
        state[i + 1 + A[i]]--;
    }
    for (int i = 0; i < N; i++) {
      state[i + 1] += state[i];
    }
    for (int i = 0; i < N; i++) {
      A[i] = state[i];
    }
  }
  for (int i = 0; i < N; i++) {
    cout << A[i];
    if (i == N - 1)
      cout << "\\n";
    else
      cout << " ";
  }
}

Error: TLE

Fixed submission:

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

// #define int long long
typedef long long ll;
// const int INF = 2e9;
// const ll INF = 9e18;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<int> state(N + 1);
  for (int k = 0; k < K; k++) {
    for (int i = 0; i < N + 1; i++) {
      state[i] = 0;
    }
    for (int i = 0; i < N; i++) {
      if (i - A[i] < 0)
        state[0]++;
      else
        state[i - A[i]]++;
      if (i + 1 + A[i] > N)
        state[N]--;
      else
        state[i + 1 + A[i]]--;
    }
    for (int i = 0; i < N; i++) {
      state[i + 1] += state[i];
    }
    for (int i = 0; i < N; i++) {
      A[i] = state[i];
    }
  }
  for (int i = 0; i < N; i++) {
    cout << A[i];
    if (i == N - 1)
      cout << "\\n";
    else
      cout << " ";
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

int a_to_if(vector<ll> a, vector<ll> &i, vector<ll> &f, ll n) {
  ll ind = 0LL;
  for (auto x : a) {
    i[ind] = max(0LL, ind - x);
    f[ind] = min(n, ind + x);
    ++ind;
  }

  sort(i.begin(), i.end());
  sort(f.begin(), f.end());

  return 0;
}

int if_to_a(vector<ll> &a, vector<ll> i, vector<ll> f, ll n) {
  vector<ll> b(n, 0LL);
  for (ll ind = 0LL; ind < n; ind++) {
    b[ind] = (upper_bound(i.begin(), i.end(), ind) - i.begin()) -
             (upper_bound(f.begin(), f.end(), ind - 1) - f.begin());
  }
  a = b;
  return 0;
}

int update(vector<ll> &a, vector<ll> &i, vector<ll> &f, ll n) {

  a_to_if(a, i, f, n);
  if_to_a(a, i, f, n);
  return 0;
}

int check(vector<ll> q, ll n) {
  for (auto a : q) {
    if (a != n)
      return 0;
  }
  return 1;
}

int print_vec(vector<ll> v) {
  for (auto a : v)
    cout << a << " ";
  cout << endl;

  return 0;
}

int main(int argc, const char *argv[]) {

  //    std::ifstream in("input.txt");
  //    std::cin.rdbuf(in.rdbuf());

  cin.tie(0);
  ios::sync_with_stdio(false);

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

  vector<ll> a(n, 0LL);
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  vector<ll> i(n, 0LL);
  vector<ll> f(n, 0LL);

  for (ll j = 0LL; j < k; j++) {
    if (check(a, n))
      break;
    else
      update(a, i, f, n);
  }

  print_vec(a);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

vector<ll> update(vector<ll> &a, ll n) {
  vector<ll> c(n, 0);

  for (ll i = 0; i < n; i++) {
    for (ll j = i - a[i]; j <= i + a[i]; j++) {
      if (j < 0 || j >= n)
        continue;
      c[j]++;
    }
  }
  return c;
}

int check(vector<ll> q, ll n) {
  for (auto a : q) {
    if (a != n)
      return 0;
  }
  return 1;
}

int print_vec(vector<ll> v) {
  for (auto a : v)
    cout << a << " ";
  cout << endl;

  return 0;
}

int main(int argc, const char *argv[]) {

  //    std::ifstream in("input.txt");
  //    std::cin.rdbuf(in.rdbuf());

  cin.tie(0);
  ios::sync_with_stdio(false);

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

  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  vector<ll> b(n, 0LL);

  for (ll i = 0; i < k; i++) {
    b = a;

    if (check(b, n)) {
      print_vec(b);
      return 0;
    } else {
      a = update(b, n);
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
const int INF = 1LL << 60; // 10e18+10e17ちょっとくらい
const int MOD = 1000000007;
const int MAX_N = 200100;

int a[200010];
stack<int> q[200010];
unordered_map<int, bool> mp;
signed main() {
  //  以降 cin の入力元が 'input.txt' になる
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());
  int n, k;
  cin >> n >> k;
  rep(i, n) { cin >> a[i + 1]; }
  bool flg;
  rep(iii, k) {
    flg = true;
    rep(i, n) {
      int l = i + 1 - a[i + 1];
      int r = i + 1 + a[i + 1];
      l = max(1LL, l);
      l = min(l, n);
      r = max(1LL, r);
      r = min(r, n);
      q[l].push(i + 1);
      q[r].push(i + 1);
    }
    rep(i, n) {
      int now = 0;
      while (!q[i + 1].empty()) {
        int tmp = q[i + 1].top();
        q[i + 1].pop();
        if (mp.find(tmp) != mp.end()) {
          mp.erase(tmp);
          now++;
        } else {
          mp[tmp] = true;
        }
      }
      a[i + 1] = now + mp.size();
      if (a[i + 1] < n - 1)
        flg = false;
    }
    if (flg) {
      if (iii == k - 1) {
        flg = false;
      }
      break;
    }
  }
  if (flg) {
    rep(i, n) { cout << n << " "; }
  } else {
    rep(i, n) { cout << a[i + 1] << " "; }
  }
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
const int INF = 1LL << 60; // 10e18+10e17ちょっとくらい
const int MOD = 1000000007;
const int MAX_N = 200100;

int a[200010];
stack<int> q[200010];
map<int, bool> mp;
signed main() {
  //  以降 cin の入力元が 'input.txt' になる
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());
  int n, k;
  cin >> n >> k;
  rep(i, n) { cin >> a[i + 1]; }
  bool flg;
  rep(iii, k) {
    flg = true;
    rep(i, n) {
      int l = i + 1 - a[i + 1];
      int r = i + 1 + a[i + 1];
      l = max(1LL, l);
      l = min(l, n);
      r = max(1LL, r);
      r = min(r, n);
      q[l].push(i + 1);
      q[r].push(i + 1);
    }
    rep(i, n) {
      int now = 0;
      while (!q[i + 1].empty()) {
        int tmp = q[i + 1].top();
        q[i + 1].pop();
        if (mp.find(tmp) != mp.end()) {
          mp.erase(tmp);
          now++;
        } else {
          mp[tmp] = true;
        }
      }
      a[i + 1] = now + mp.size();
      if (a[i + 1] < n - 1)
        flg = false;
    }
    if (flg && iii != k - 1)
      break;
  }
  if (flg) {
    rep(i, n) { cout << n << " "; }
  } else {
    rep(i, n) { cout << a[i + 1] << " "; }
  }
  cout << endl;

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

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(q, k) {
    vector<int> b(n);
    rep(i, n) {
      int l = max(0, i - a[i]);
      int r = min(n, i + a[i] + 1);
      b[l]++;
      if (r < n)
        b[r]--;
    }
    for (int i = 1; i < n; i++)
      b[i] += b[i - 1];
    if (a == b)
      break;
    a = b;
  }
  rep(i, n) cout << a[i] << ((i == n - 1) ? '\\n' : ' ');
  return 0;
}

Error: TLE

Fixed submission:

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

// https://tsutaj.hatenablog.com/entry/2017/03/30/224339
struct LazySegmentTree {
private:
  int n;
  vector<ll> node, lazy;

public:
  LazySegmentTree(vector<ll> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 2];
  }

  void eval(int k, int l, int r) {
    if (lazy[k] != 0) {
      node[k] += lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k] / 2;
        lazy[2 * k + 2] += lazy[k] / 2;
      }
      lazy[k] = 0;
    }
  }

  void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] += (r - l) * x;
      eval(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }

  ll getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return node[k];
    ll vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  LazySegmentTree seg1(a), seg2(vector<ll>(n, 0)), nu(vector<ll>(n, 0));

  rep(q, min(k, 50)) {
    seg2 = nu;
    int al = 0;
    rep(i, n) {
      int now = seg1.getsum(i, i + 1);
      if (now >= n - 1 - min(i, n - i - 1)) {
        al++;
        // st.erase(i);
      }
      seg2.add(max(0, (int)(i - now)), min(n, (int)(i + now + 1)), 1);
    }
    seg1 = seg2;
    if (al == n) {
      rep(i, n) cout << n << ((i == n - 1) ? '\\n' : ' ');
      return 0;
    }
  }
  rep(i, n) cout << seg1.getsum(i, i + 1) << ((i == n - 1) ? '\\n' : ' ');
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n), a(n + 1);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  while (k--) {
    for (int i = 0; i < n; i++) {
      a[max(0, i - v[i])]++;
      a[min(n, i + v[i] + 1)]--;
    }
    for (int i = 1; i < n; i++) {
      a[i] += a[i - 1];
    }
    for (int i = 0; i < n; i++)
      v[i] = a[i];
    for (int i = 0; i < n + 1; i++)
      a[i] = 0;
    int flag = 0;
    for (int i = 0; i < n; i++) {
      if (v[i] != n) {
        flag = 1;
        break;
      }
    }
    if (flag == 0)
      break;
  }
  for (int i = 0; i < n; i++) {
    cout << v[i] << ' ';
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n), a(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  while (k--) {
    for (int i = 0; i < n; i++) {
      for (int j = max(0, i - v[i]); j <= min(n - 1, i + v[i]); j++) {
        a[j]++;
      }
    }
    for (int i = 0; i < n; i++)
      v[i] = a[i];
    for (int i = 0; i < n; i++)
      a[i] = 0;
    int flag = 0;
    for (int i = 0; i < n; i++) {
      if (v[i] != n) {
        flag = 1;
        break;
      }
    }
    if (flag == 0)
      break;
  }
  for (int i = 0; i < n; i++) {
    cout << v[i] << ' ';
  }

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

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::cpp_int;
using namespace std;

#if __has_include("print.hpp")
#include "print.hpp"
#endif

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007

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

typedef long long ll;
typedef pair<ll, ll> p;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, n) cin >> v[i];

  vector<int> res(n);
  for (int i = 0; i < min(k, 100); ++i) {
    vector<int> tmp(n + 10);
    for (int j = 0; j < n; ++j) {
      tmp[max(j + 1 - v[j], 0)]++;
      tmp[min(j + 1 + v[j] + 1, n + 3)]--;
    }
    for (int j = 0; j < n; ++j) {
      tmp[j + 1] += tmp[j];
      v[j] = tmp[j + 1];
    }
  }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::cpp_int;
using namespace std;

#if __has_include("print.hpp")
#include "print.hpp"
#endif

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007

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

typedef long long ll;
typedef pair<ll, ll> p;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, n) cin >> v[i];

  vector<int> res(n);
  for (int i = 0; i < k; ++i) {
    vector<int> tmp(n + 10);
    for (int j = 0; j < n; ++j) {
      tmp[max(j + 1 - v[j], 0)]++;
      tmp[min(j + 1 + v[j] + 1, n + 3)]--;
    }
    for (int j = 0; j < n; ++j) {
      tmp[j + 1] += tmp[j];
      v[j] = tmp[j + 1];
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << v[i] << ' ';
  }
  cout << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

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

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 < min(k, 50); i++) {
    vector<int> imos(n); // I'll begin the imos method
    for (int j = 0; j < n; j++) {
      int l = max(0, j - a[j]);
      int r = min(n - 1, j + a[j]);
      imos[l]++;
      if (r + 1 <= n - 1) {
        imos[r + 1]--;
      }
    }
    for (int k = 0; k < n; k++) {
      if (k > 0) {
        imos[k] += imos[k - 1];
      }
    }
    /*for(int l = 0; l < n; l++){
      a[l] = imos[l];
    }*/
    a = imos;
  }

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

Error: WA

Fixed submission:

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

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 < min(k, 50); i++) {
    vector<int> imos(n); // I'll begin the imos method
    for (int j = 0; j < n; j++) {
      int l = max(0, j - a[j]);
      int r = min(n - 1, j + a[j]);
      imos[l]++;
      // if(r+1 < n){
      imos[r + 1]--;
      //}
    }
    for (int k = 0; k < n; k++) {
      if (k > 0) {
        imos[k] += imos[k - 1];
      }
    }
    /*for(int l = 0; l < n; l++){
      a[l] = imos[l];
    }*/
    a = imos;
  }

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

#include <iostream>
#include <vector>

#define int long long

signed main() {
  int n, k;
  std::cin >> n >> k;

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

  for (int i = 0; i < k; i++) {
    std::vector<int> b(n + 1, 0);
    for (int j = 1; j <= n; j++) {
      int l = std::max(1ll, j - a[j]);
      int r = std::min(n, j + a[j]);

      // std::cout << l << " " << r << std::endl;

      b[l]++;
      if (r < n)
        b[r + 1]--;
    }

    for (int j = 1; j <= n; j++)
      b[j] += b[j - 1];

    a = b;
    bool done = true;
    for (int j = 1; j <= n; j++)
      if (a[j] < n)
        done = false;

    if (done) {
      break;
    }
  }

  for (int i = 1; i <= n; i++)
    std::cout << a[i] << " ";
  std::cout << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define int long long

signed main() {
  int n, k;
  std::cin >> n >> k;

  std::vector<int> a(n + 1, 0);
  for (int i = 1; i <= n; i++)
    std::cin >> a[i];

  for (int i = 0; i < k; i++) {
    std::vector<int> b(n + 1, 0);
    for (int j = 1; j <= n; j++) {
      int l = std::max(1ll, j - a[j]);
      int r = std::min(n, j + a[j]);

      // std::cout << l << " " << r << std::endl;

      b[l]++;
      if (r < n)
        b[r + 1]--;
    }

    for (int j = 1; j <= n; j++)
      b[j] += b[j - 1];

    a = b;
    bool done = false;
    for (int j = 1; j <= n; j++)
      if (a[j] < n)
        done = false;

    if (done)
      break;
  }

  for (int i = 1; i <= n; i++)
    std::cout << a[i] << " ";
  std::cout << std::endl;

  return 0;
}