alexjercan/bugnet huggingface dataset:
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>;

void solve(long long N, long long K, std::vector<long long> A) {
  for (int i = 0; i < K; i++) {
    ll minV = ll(1e18) + 5;
    vector<ll> sum(N + 1, 0);
    for (int j = 0; j < N; j++) {
      ll l = max(0ll, j - A[j]);
      ll r = min(N - 1, j + A[j]);
      sum[l]++;
      sum[r + 1]--;
    }
    for (int j = 0; j < N; j++) {
      A[j] = sum[j];
      minV = min(minV, A[j]);
      sum[j + 1] += sum[j];
    }
    if (minV == N) {
      break;
    }
  }
  for (int i = 0; i < N; i++) {
    if (i != N - 1) {
      cout << A[i] << " ";
      continue;
    }
    cout << A[i];
  }
  cout << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, K, std::move(A));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

void solve(long long N, long long K, std::vector<long long> A) {
  ll minV = 0;
  for (int i = 0; i < K; i++) {
    vector<ll> sum(N + 1, 0);
    for (int j = 0; j < N; j++) {
      ll l = max(0ll, j - A[j]);
      ll r = min(N - 1, j + A[j]);
      sum[l]++;
      sum[r + 1]--;
    }
    for (int j = 0; j < N; j++) {
      A[j] = sum[j];
      minV = min(minV, A[j]);
      sum[j + 1] += sum[j];
    }
    if (minV == N) {
      break;
    }
  }
  for (int i = 0; i < N; i++) {
    if (i != N - 1) {
      cout << A[i] << " ";
      continue;
    }
    cout << A[i];
  }
  cout << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, K, std::move(A));
  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, n) for (int i = 0; i < n; i++)

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N, 0);
  rep(i, N) cin >> A[i];
  if (K > 2000)
    K = 2000;
  rep(k, K) {
    vector<int> B(N + 1, 0);
    rep(i, N) {
      int d = A.at(i);
      int s = i - d, l = i + d;
      if (s < 0)
        s = 0;
      B.at(s)++;
      if (l < N - 1)
        B.at(l + 1)--;
    }
    for (int i = 1; i < N; i++)
      B.at(i) += B.at(i - 1);
    rep(i, N) A.at(i) = B.at(i);
  }

  rep(i, N) cout << A.at(i) << ' ';
  cout << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N, 0);
  rep(i, N) cin >> A[i];
  rep(k, K) {
    vector<int> B(N + 1, 0);
    rep(i, N) {
      int d = A.at(i);
      int s = i - d, l = i + d;
      if (s < 0)
        s = 0;
      if (l > N - 1)
        l = N - 1;
      B.at(s)++;
      B.at(l + 1)--;
    }
    for (int i = 1; i < N; i++)
      B.at(i) += B.at(i - 1);
    rep(i, N) A.at(i) = B.at(i);
  }

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

#include <bits/stdc++.h>
#include <climits>
#include <sstream>
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];

  // bool flag;
  for (int i = 0; i < K; i++) {
    vector<int> B(N, 0);
    for (int j = 0; j < N; j++) {
      int l = max(0, j - A[j]);
      int r = min(N - 1, j + A[j]);
      B[l]++;
      if (r + 1 < N)
        B[r + 1]--;
    }
    int sum = B[0];
    for (int i = 1; i < N; i++) {
      B[i] += B[i - 1];
      sum += B[i];
    }
    for (int i = 0; i < N; i++) {
      A[i] = B[i];
    }

    if (sum == N * N)
      break;
  }

  for (int i = 0; i < N; i++) {
    if (i != N - 1)
      cout << A[i] << " ";
    else
      cout << A[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <climits>
#include <sstream>
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];

  bool flag;
  for (int i = 0; i < K; i++) {
    vector<int> B(N, 0);
    for (int j = 0; j < N; j++) {
      int l = max(0, j - A[j]);
      int r = min(N - 1, j + A[j]);
      B[l]++;
      if (r + 1 < N)
        B[r + 1]--;
    }
    for (int i = 1; i < N; i++)
      B[i] += B[i - 1];

    for (int i = 0; i < N; i++) {
      A[i] = B[i];
      if (A[i] != N)
        flag = false;
    }

    if (flag)
      break;
  }

  for (int i = 0; i < N; i++) {
    if (i != N - 1)
      cout << A[i] << " ";
    else
      cout << A[i] << endl;
  }
}
Problem p02647 (C++) — buggy submission, verdict: 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;
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  rep(i, n) { cin >> a[i]; }
  rep(i, k) { // 100000
    vector<int> add(n + 1);
    rep(j, n) { // 100000
      //			rep(l, a[j]*2+1){//100000
      //				cout << j-a[j]+l << endl;
      int st = max(j - a[j], 0);
      int en = min(j + a[j] + 1, n);
      add[st]++;
      add[en]--;
      //			}
    }
    int f = 0;
    int sum = 0;
    rep(j, n) {
      sum += add[j];
      if (sum != n)
        f = 1;
      a[j] = sum;
    }
    if (f == 0)
      break;
  }
  rep(i, n) cout << a[i] << ' ';
  cout << endl;
  return 0;
}

Error: TLE

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  rep(i, k) {
    vector<int> add(n);
    rep(j, n) {
      rep(l, a[j] * 2 + 1) {
        //				cout << j-a[j]+l << endl;
        if (j - a[j] + l >= 0 && j - a[j] + l < n)
          add[j - a[j] + l]++;
      }
    }
    int f = 0;
    rep(j, n) {
      if (add[j] != n)
        f = 1;
      a[j] = add[j];
    }
    if (f == 0)
      break;
  }
  rep(i, n) cout << a[i] << ' ';
  cout << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (auto &a : A)
    cin >> a;
  for (int i = 0; i < K; i++) {
    vector<int> B(N + 1);
    for (int j = 0; j < N; j++) {
      B[max(j - A[j], 0)]++;
      B[min(j + A[j] + 1, N)]--;
    }
    for (int j = 1; j <= N; j++) {
      B[j] += B[j - 1];
    }
    B.pop_back();
    swap(A, B);
    if (*min_element(A.begin(), A.end()) == N)
      break;
  }
  for (int i = 0; i < N; i++) {
    cout << A[i] << " \\n"[i == N - 1];
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (auto &a : A)
    cin >> a;
  for (int i = 0; i < K; i++) {
    vector<int> B(N + 1);
    for (int j = 0; j < N; j++) {
      B[max(j - A[j], 0)]++;
      B[min(j + A[j] + 1, N)]--;
    }
    for (int j = 1; j <= N; j++) {
      B[j] += B[j - 1];
    }
    swap(A, B);
    if (*min_element(A.begin(), A.end()) == N)
      break;
  }
  for (int i = 0; i < N; i++) {
    cout << A[i] << " \\n"[i == N - 1];
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

#define ALL(A) A.begin(), A.end()

void ALLIN1_NUMBER(vector<ll> &V) {
  for (auto &x : V) {
    cin >> x;
  }
}

void ALLOUT_NUMBER(vector<ll> V) {
  ll N = V.size();
  for (ll i = 0; i < N; i++) {
    cout << V[i];
    if (i < N - 1)
      cout << ' ';
  }
  cout << endl;
}

template <class T> void OUT0(T N) { cout << N << endl; }

static const double pi = acos(-1.0);

double Cos(double D) { return cos(pi / 180 * D); }

ll log2_N(ll N) {
  for (ll i = 0;; i++) {
    if ((1 << i) >= N) {
      return i;
    }
  }
}

vector<ll> Basically_move(ll N, ll K, vector<ll> A) {
  for (ll i = 0; i < min(log2_N(N) + 100, K); i++) {
    vector<ll> UpDown(N + 1, 0);
    for (ll j = 0; j < N; j++) {
      UpDown[max(0LL, j - A[j])]++;
      UpDown[min(N, j + A[j] + 1)]--;
    }
    vector<ll> next(N);
    next[0] = UpDown[0];
    for (ll j = 1; j < N; j++) {
      next[j] = next[j - 1] + UpDown[j];
    }
    A = next;
  }
  return A;
}

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

  vector<ll> A(N);
  ALLIN1_NUMBER(A);

  ALLOUT_NUMBER(Basically_move(N, K, A));

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

#define ALL(A) A.begin(), A.end()

void ALLIN1_NUMBER(vector<ll> &V) {
  for (auto &x : V) {
    cin >> x;
  }
}

void ALLOUT_NUMBER(vector<ll> V) {
  ll N = V.size();
  for (ll i = 0; i < N; i++) {
    cout << V[i];
    if (i < N - 1)
      cout << ' ';
  }
  cout << endl;
}

template <class T> void OUT0(T N) { cout << N << endl; }

static const double pi = acos(-1.0);

double Cos(double D) { return cos(pi / 180 * D); }

ll log2_N(ll N) {
  for (ll i = 0;; i++) {
    if ((1 << i) >= N) {
      return i;
    }
  }
}

vector<ll> Basically_move(ll N, ll K, vector<ll> A) {
  for (ll i = 0; i < min(log2_N(N), K); i++) {
    vector<ll> next(N, 0);
    for (ll j = 0; j < N; j++) {
      for (ll k = max(0LL, j - A[j]); k <= min(N - 1, j + A[j]); k++) {
        next[k]++;
      }
    }
    A = next;
  }
  return A;
}

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

  vector<ll> A(N);
  ALLIN1_NUMBER(A);

  ALLOUT_NUMBER(Basically_move(N, K, A));

  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)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using G = vector<vector<int>>;

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

  vector<int> dp(n, 0);
  rep(i, k) {
    rep(j, n) dp[j] = 0;
    rep(j, n) {
      int left, right;
      left = j - a[j];
      right = j + a[j];
      int begin;
      if (left < 0)
        begin = 0;
      else
        begin = left;
      dp[begin]++;
      if (right < n - 1)
        dp[right + 1]--;
    }
    rep(j, n) {
      if (j > 0)
        dp[j] += dp[j - 1];
    }
    rep(j, n) {
      int tmp = dp[j];
      a[j] = tmp;
    }

    int flag = 0;
    rep(j, n) {
      if (a[j] < n) {
        flag = 1;
        break;
      }
    }
    if (!flag) {
      rep(j, n) { cout << n << " "; }
      cout << endl;
      return 0;
    }
  }

  rep(i, n) cout << a[i] << " ";
  cout << endl;
  return 0;
}

Error: TLE

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>;
using G = vector<vector<int>>;

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

  vector<int> dp(n, 0);

  if (k >= n)
    k = n;
  rep(i, k) {
    rep(j, n) dp[j] = 0;
    rep(j, n) {
      int left, right;
      left = j - a[j];
      right = j + a[j];
      int begin;
      if (left < 0)
        begin = 0;
      else
        begin = left;
      dp[begin]++;
      if (right < n - 1)
        dp[right + 1]--;
    }
    rep(j, n) {
      if (j > 0)
        dp[j] += dp[j - 1];
    }
    rep(j, n) {
      int tmp = dp[j];
      a[j] = tmp;
    }
  }

  rep(i, n) cout << a[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 < (int)(n); i++)

int main() {
  int N, K;

  bool end = true;

  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  rep(i, K) {
    end = true;
    vector<int> B(N, 0);
    rep(j, N) {
      int l = max(j - A[j], 0);
      int r = min(j + A[j], N - 1);
      B[l]++;
      if (r + 1 < N)
        B[r + 1]--;
    }
    rep(j, N) if (j > 0) B[j] += B[j - 1];
    rep(j, N) {
      A[j] = B[j];
      if (A[j] < N)
        end = false;
    }
    if (end) {
      rep(j, N) { cout << N << endl; }
      return 0;
    }
  }
  rep(j, N) cout << A[j] << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, K;

  bool end = true;

  cin >> N >> K;
  vector<int> A(N + 1);
  vector<int> temp(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A[i];

  if (N <= K) {
    rep(e, N) cout << N << endl;
    return 0;
  }

  rep(i, K) {
    vector<int> temp(N + 1);
    long double r, l;
    end = true;
    for (int j = 1; j <= N; j++) {
      l = j - A[j] - 0.5;
      r = j + A[j] + 0.5;
      if (l < 1)
        l = 1;
      if (r > N)
        r = N + 1;
      for (int t = ceil(l); t < r; t++) {
        temp[t]++;
      }
    }
    rep(s, N + 1) {
      A[s] = temp[s];
      if (A[s] < N)
        end = false;
    }
    if (end) {
      for (int i = 1; i <= N; i++) {
        cout << A[i] << endl;
        return 0;
      }
    }
  }
  for (int i = 1; i <= N; i++) {
    cout << A[i] << endl;
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int N, K, l, r;
int main() {
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  while (K--) {
    vector<int> B(N + 1, 0);
    for (int i = 0; i < N; i++) {
      l = max(0, i - A[i]);
      r = min(N - 1, i + A[i]);
      B[l]++;
      B[r + 1]--;
    }
    bool changed = false;
    for (int i = 0; i < N; i++) {
      changed |= (A[i] != B[i]);
      A[i] = B[i];
      B[i + 1] += B[i];
    }
    if (!changed)
      break;
  }
  for (int i = 0; i < N; i++)
    cout << A[i] << " \\n"[i == N - 1];
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N, K, l, r;
int main() {
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  while (K--) {
    vector<int> B(N, 0);
    for (int i = 0; i < N; i++) {
      l = max(0, i - A[i]);
      r = min(N - 1, i + A[i]);
      B[l]++;
      if (r + 1 < N)
        B[r + 1]--;
    }
    for (int i = 0; i < N - 1; i++)
      B[i + 1] += B[i];
    A = B;
  }
  for (int i = 0; i < N; i++)
    cout << A[i] << " \\n"[i == N - 1];
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, k;
int a[N];

int b[N];

int q[N];

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

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

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


Error: TLE

Fixed submission:

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

int n, k;
int a[N];

int b[N];

int q[N];

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

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

// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
// lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

void app() {
  int n, k;
  cin >> n >> k;

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

  rep(t, k) {
    vector<int> dbdi(n + 1, 0);
    rep(i, n) {
      dbdi[max(0, i - a[i])]++;
      dbdi[min(n, i + a[i] + 1)]--;
    }
    int db = 0;
    bool ch = false;
    rep(i, n) {
      db += dbdi[i];
      if (a[i] != db)
        ch = true;
      a[i] = db;
    }
    if (!ch)
      break;
  }

  rep(i, n) cout << a[i] << ' ';
  cout << endl;
}

int main() {
  app();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
// lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

void app() {
  int n, k;
  cin >> n >> k;

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

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

  rep(i, n) cout << a[i] << ' ';
  cout << endl;
}

int main() {
  app();
  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)
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];

  vector<int> next_a(n);
  rep(cnt, k) {
    bool ended = true;
    vector<int> min_map(n);
    vector<int> max_map(n);
    rep(i, n) {
      int val = a[i];
      if (val < n) {
        ended = false;
      }
      int mn = max(i - val, 0);
      int mx = min(i + val, n - 1);
      min_map[mn]++;
      max_map[mx]++;
    }
    int cur = 0;
    rep(i, n) {
      if (min_map[i]) {
        cur += min_map[i];
      }
      a[i] = cur;
      if (max_map[i]) {
        cur -= max_map[i];
      }
    }

    if (ended) {
      break;
    }
  }

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

Error: TLE

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

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

  int cntall = min(n, k);
  vector<int> next_a(n);
  rep(cnt, cntall) {
    vector<int> min_map(n);
    vector<int> max_map(n);
    rep(i, n) {
      int val = a[i];
      int mn = max(i - val, 0);
      int mx = min(i + val, n - 1);
      min_map[mn]++;
      max_map[mx]++;
    }
    int cur = 0;
    rep(i, n) {
      if (min_map[i]) {
        cur += min_map[i];
      }
      a[i] = cur;
      if (max_map[i]) {
        cur -= max_map[i];
      }
    }
  }

  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 <deque>
#include <iostream>
#include <queue>

using namespace std;
struct Item {
  int start;
  int end;
};

bool operator<(const Item &a, const Item &b) { return a.start > b.start; };

int main(void) {

  int N;
  cin >> N;
  int K;
  cin >> K;

  vector<int> data(N);
  for (auto i = 0; i < N; i++) {
    cin >> data[i];
  }
  for (auto x = 0; x < K; x++) {
    int ended = 0;
    priority_queue<Item> raise_queue;
    priority_queue<int> down_queue;
    for (int i = 0; i < N; i++) {
      raise_queue.push({i - data[i], i + data[i]});
      //			cout << i << "," << data[i] << endl;
    }
    int height = 0;
    vector<int> new_data(N);
    for (int i = 0; i < N; i++) {
      while (!raise_queue.empty() && raise_queue.top().start <= i) {
        //				cout << i << "-" << raise_queue.top().start <<
        //endl;
        height += 1;
        down_queue.push(-raise_queue.top().end);
        raise_queue.pop();
      }
      while (!down_queue.empty() && -down_queue.top() < i) {
        height -= 1;
        down_queue.pop();
      }
      new_data[i] = height;
      if (height == N) {
        ended += 1;
      }
    }
    data = move(new_data);
    //		data = new_data;
    if (ended == N) {
      //			cout << x << endl;
      break;
    } else {
      //			cout << ended << endl;
    }
  }

  for (int i = 0; i < N; i++) {
    if (i > 0) {
      cout << ' ';
    }
    cout << data[i];
  }
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <deque>
#include <iostream>
#include <queue>

using namespace std;
struct Item {
  int start;
  int end;
};

bool operator<(const Item &a, const Item &b) { return a.start > b.start; };

int main(void) {

  int N;
  cin >> N;
  int K;
  cin >> K;

  vector<int> data(N);
  for (auto i = 0; i < N; i++) {
    cin >> data[i];
  }
  int ended = 0;
  for (auto x = 0; x < K; x++) {
    priority_queue<Item, deque<Item>> raise_queue;
    priority_queue<int, deque<int>> down_queue;
    for (int i = 0; i < N; i++) {
      raise_queue.push({i - data[i], i + data[i]});
      //			cout << i << "," << data[i] << endl;
    }
    int height = 0;
    vector<int> new_data(N);
    for (int i = 0; i < N; i++) {
      while (!raise_queue.empty() && raise_queue.top().start <= i) {
        //				cout << i << "-" << raise_queue.top().start <<
        //endl;
        height += 1;
        down_queue.push(-raise_queue.top().end);
        raise_queue.pop();
      }
      while (!down_queue.empty() && -down_queue.top() < i) {
        height -= 1;
        down_queue.pop();
      }
      new_data[i] = height;
    }
    if (height == N) {
      ended += 1;
    }
    //		data = move(new_data);
    data = new_data;
    if (ended == N) {
      break;
    }
  }

  for (int i = 0; i < N; i++) {
    if (i > 0) {
      cout << ' ';
    }
    cout << data[i];
  }
  cout << endl;

  return 0;
}