alexjercan/bugnet huggingface dataset:
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> 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 <iostream>
#include <string.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
int a[N], b[N];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  while (k--) {
    int cnt = 0;
    memset(b, 0, sizeof(b));
    for (int i = 0; i < n; i++) {
      b[max(i - a[i], 0)]++;
      if (i + a[i] + 1 <= n - 1)
        b[i + a[i] + 1]--;
    }
    a[0] = b[0];
    for (int i = 1; i < n; i++) {
      b[i] += b[i - 1];
      a[i] = b[i];
      if (a[i] == n)
        cnt++;
    }
    if (cnt == n)
      break;
  }
  for (int i = 0; i < n; i++)
    cout << a[i] << ' ';
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long int ll;
typedef pair<ll, ll> P;

ll a[212345], b[212345];

int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (pow(2, k - 50) > n) {
    for (int i = 0; i < n; i++) {
      cout << n << " ";
    }
    return 0;
  }
  for (int i = 0; i < k; i++) {
    for (int m = 0; m < n + 1; m++) {
      b[m] = 0;
    }
    for (int m = 0; m < n; m++) {
      b[max((ll)0, m - a[m])]++;
      b[min(n - 1, m + a[m]) + 1]--;
    }
    ll count = 0;
    for (int m = 0; m < n; m++) {
      count += b[m];
      a[m] = count;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }

  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long int ll;
typedef pair<ll, ll> P;

ll a[212345], b[212345];

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

  for (int i = 0; i < k; i++) {
    for (int m = 0; m < n + 1; m++) {
      b[m] = 0;
    }
    for (int m = 0; m < n; m++) {
      b[max((ll)0, m - a[m])]++;
      b[min(n - 1, m + a[m]) + 1]--;
    }
    ll count = 0;
    for (int m = 0; m < n; m++) {
      count += b[m];
      a[m] = count;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }

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

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  VL a(n + 1);
  rep(i, n) cin >> a[i];
  if (k > n) {
    rep(i, n) cout << n << " ";
    cout << "\\n";
    return 0;
  }
  VL sk(n + 1);
  rep(q, k) {
    fill(all(sk), 0);
    rep(i, n) {
      int st = max<ll>(0, i - a[i]);
      int ed = min<ll>(n, i + a[i] + 1);
      sk[st] += 1;
      sk[ed] -= 1;
    }
    rep(i, n) sk[i + 1] += sk[i];
    // rep(i, n) cout << sk[i] << " "; dbg("q=",q);
    a = sk;
    if (count(sk.begin(), sk.begin() + k, n) == n)
      break;
    if (q * n > 20000000)
      break;
  }
  rep(i, n) cout << sk[i] << " ";
  cout << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  VL a(n + 1);
  rep(i, n) cin >> a[i];
  if (k > n) {
    rep(i, n) cout << n << " ";
    cout << "\\n";
    return 0;
  }
  VL sk(n + 1);
  rep(q, k) {
    fill(all(sk), 0);
    rep(i, n) {
      int st = max<ll>(0, i - a[i]);
      int ed = min<ll>(n, i + a[i] + 1);
      sk[st] += 1;
      sk[ed] -= 1;
    }
    rep(i, n) sk[i + 1] += sk[i];
    // rep(i, n) cout << sk[i] << " "; dbg("q=",q);
    a = sk;
    if (count(sk.begin(), sk.begin() + k, n) == n)
      break;
  }
  rep(i, n) cout << sk[i] << " ";
  cout << "\\n";
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <ctype.h>
#include <iostream>
#include <limits.h>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int N, K;
vector<int> A;

int main() {
  cin >> N >> K;
  A.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < K; ++i) {
    if (i >= N)
      break;
    vector<int> B(N);
    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 j = 1; j < N; ++j) {
      B[j] += B[j - 1];
    }
    if (A == B)
      break;
    A = B;
  }
  for (int i = 0; i < N; ++i) {
    if (i != 0)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <ctype.h>
#include <iostream>
#include <limits.h>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

long N, K;
int A[200000];

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < K; ++i) {
    int B[200000] = {};

    for (int j = 0; j < N; ++j) {
      int counter = 0;
      for (int l = 0; l < N; ++l) {
        if (A[l] >= abs(j - l))
          counter++;
      }
      B[j] = counter;
    }
    memcpy(A, B, sizeof(A));
  }
  for (int i = 0; i < N; ++i) {
    if (i != 0)
      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;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  rep(i, k) {
    vector<ll> b(n + 1, 0);
    rep(j, n) {
      ll l = max(0LL, j - a[j]);
      ll r = min(n, j + a[j] + 1);
      b[l]++;
      b[r]--;
    }
    ll cnt = 0;
    rep(j, n) {
      b[j + 1] += b[j];
      a[j] = b[j];
      if (a[j] == n)
        cnt++;
    }
    if (cnt == n)
      break;
  }
  rep(i, n) printf("%lld%s", a[i], (i == (n - 1)) ? "\\n" : " ");
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  set<ll> s;
  rep(i, n) s.insert(i);
  rep(i, k) {
    vector<ll> b(n, 0);
    for (auto it = s.begin(); it != s.end();) {
      ll x = *it;
      ll left = max(0LL, x - a[x]);
      ll right = min(n - 1, x + a[x]);
      if ((left == 0) && (right == (n - 1))) {
        s.erase(it++);
        continue;
      }
      reps(o, left, right + 1) b[o]++;
      it++;
    }
    ll ap = n - sz(s);
    rep(j, n) a[j] = b[j] + ap;
    if (ap == n)
      break;
  }
  rep(i, n) printf("%lld%s", a[i], (i == (n - 1)) ? "\\n" : " ");
  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 + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < k; i++) {
    vector<int> imos(n + 2, 0);
    for (int j = 1; j <= n; j++) {
      int l = max(1, j - a.at(j));
      int r = min(n + 1, j + a.at(j) + 1);
      imos.at(l)++;
      imos.at(r)--;
    }
    for (int j = 1; j <= n; j++) {
      imos.at(j) += imos.at(j - 1);
    }
    int flag = 0;
    for (int j = 1; j <= n; j++) {
      if (imos.at(j) != n) {
        flag = 1;
      }
    }

    for (int j = 1; j <= n; j++) {
      a.at(j) = imos.at(j);
    }
    if (flag == 0) {
      break;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i != n) {
      cout << a.at(i) << " ";
    } else {
      cout << a.at(i);
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a.at(i);
  }
  vector<int> copy(n + 1);
  copy = a;
  map<int, int> mp;
  map<int, int> count;
  for (int i = 1; i <= n; i++) {
    mp[i] = 0;
  }
  count = mp;
  for (int i = 0; i < k; i++) {
    for (int j = 1; j <= n; j++) {
      int num = a.at(j);
      for (int l = j - num; l <= j + num; l++) {
        count[l]++;
      }
    }
    for (int j = 1; j <= n; j++) {
      a.at(j) = count[j];
    }
    if (a == copy) {
      break;
    }
    count = mp;
    copy = a;
  }
  for (int i = 1; i <= n; i++) {
    cout << a.at(i) << " ";
  }
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

const ll NMAX = 200002;

ll N, K;
ll A[NMAX] = {
    0,
};
ll B[NMAX] = {
    0,
};

void output(ll n[NMAX]) {
  for (ll i = 1; i <= N; i++) {
    if (i == 1)
      cout << n[i];
    else
      cout << " " << n[i];
  }
  cout << endl;
}

void outputN() {
  for (ll i = 1; i <= N; i++) {
    if (i == 1)
      cout << N;
    else
      cout << " " << N;
  }
  cout << endl;
}

void update(ll old[NMAX], ll new_one[NMAX]) {
  ll i, j;
  for (i = 0; i <= N; i++) {
    new_one[i] = 0;
  }

  for (i = 1; i <= N; i++) {
    ll jstart, jend;
    jstart = max((ll)1, i - old[i]);
    jend = min(N, i + old[i]) + 1;

    new_one[jstart]++;
    new_one[jend]--;
  }
  // output(new_one);
  for (i = 1; i <= N; i++) {
    new_one[i] += new_one[i - 1];
  }
}

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

  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
  }

  int stp;

  // if (K >= N) {
  //	outputN();
  //	exit(0);
  // }

  for (stp = 0; stp < K; stp++) {
    if (stp % 2 == 0) {
      update(A, B);
      if (B[1] == N and B[N] == N) {
        outputN();
        exit(0);
      }
      // output(B);
    } else {
      update(B, A);
      if (A[1] == N and A[N] == N) {
        outputN();
        exit(0);
      }
      // output(A);
    }
  }
  if (stp % 2 == 0) {
    output(A);
  } else {
    output(B);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

const ll NMAX = 200001;

ll N, K;
ll A[NMAX] = {
    0,
};
ll B[NMAX] = {
    0,
};

void update(ll old[NMAX], ll new_one[NMAX]) {
  ll i, j;
  for (i = 1; i <= N; i++) {
    new_one[i] = 0;
  }

  for (i = 1; i <= N; i++) {
    ll jstart, jend;
    jstart = max((ll)1, i - old[i]);
    jend = min(N, i + old[i]);

    for (j = jstart; j <= jend; j++) {
      // cout << "j=" << j << " " <<  new_one[j] << endl;

      new_one[j]++;
    }
  }
}

void output(ll n[NMAX]) {
  for (ll i = 1; i <= N; i++) {
    if (i == 1)
      cout << n[i];
    else
      cout << " " << n[i];
  }
  cout << endl;
}

void outputN() {
  for (ll i = 1; i <= N; i++) {
    if (i == 1)
      cout << N;
    else
      cout << " " << N;
  }
  cout << endl;
}

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

  for (ll i = 1; i <= N; i++) {
    cin >> A[i];
  }

  int stp;

  if (K >= N) {
    outputN();
    exit(0);
  }

  for (stp = 0; stp < K; stp++) {
    if (stp % 2 == 0) {
      update(A, B);
      if (B[1] == N and B[N] == N) {
        outputN();
        exit(0);
      }
    } else {
      update(B, A);
      if (A[1] == N and A[N] == N) {
        outputN();
        exit(0);
      }
    }
  }
  if (stp % 2 == 0) {
    output(A);
  } else {
    output(B);
  }
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k;
  cin >> n >> k;

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

  for (int i = 0; i < k; i++) {
    vector<int> imos(n);
    for (int j = 0; j < n; j++) {
      int l = max(0, j - a[j]);
      int r = j + a[j] + 1;
      imos[l]++;
      if (r < n)
        imos[r]--;
    }
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      if (j != 0)
        imos[j] += imos[j - 1];
      if (imos[j] == n)
        cnt++;
    }

    for (int j = 0; j < n; j++)
      a[j] = imos[j];

    if (cnt == n)
      break;
  }

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


Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;

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

  for (int i = 0; i < k; i++) {
    vector<int> imos(n);
    for (int j = 0; j < n; j++) {
      int l = max(0, j - a[j]);
      int r = j + a[j] + 1;
      imos[l]++;
      if (r < n)
        imos[r]--;
    }
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      if (j != 0)
        imos[j] += imos[j - 1];
      if (imos[j] == n - 1)
        cnt++;
    }

    for (int j = 0; j < n; j++)
      a[j] = imos[j];

    if (cnt == n - 1)
      break;
  }

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

#include <algorithm>
#include <cstdio>
#include <memory.h>
using namespace std;

typedef long long ll;
ll bulb[300010];
ll bkt[300010];

ll operate(int n) {
  memset(bkt, 0, sizeof bkt);
  for (int i = 0; i < n; ++i) {
    ll op1 = i - bulb[i];
    ll op2 = i + bulb[i];
    if (op1 < 0)
      op1 = 0;
    ++bkt[op1];
    if (op2 >= n)
      op2 = n - 1;
    --bkt[op2 + 1];
  }
  bulb[0] = bkt[0];
  for (int i = 1; i < n; ++i)
    bulb[i] = bulb[i - 1] + bkt[i];
  return *min_element(bulb, bulb + n);
}

int main() {
  int n, k, i;
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; ++i)
    scanf("%lld", bulb + i);
  for (i = 0; i < k; ++i)
    if (operate(n) >= n)
      break;
  for (int i = 0; i < n; ++i)
    printf("%lld%c", bulb[i], " \\n"[i == n - 1]);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <memory.h>
using namespace std;

typedef long long ll;
ll bulb[300010];
ll bkt[300010];

ll operate(int n) {
  memset(bkt, 0, sizeof bkt);
  for (int i = 0; i < n; ++i) {
    ll op1 = i - bulb[i];
    ll op2 = i + bulb[i];
    if (op1 < 0)
      op1 = 0;
    ++bkt[op1];
    if (op2 >= n)
      op2 = n - 1;
    --bkt[op2 + 1];
  }
  bulb[0] = bkt[0];
  for (int i = 1; i < n; ++i)
    bulb[i] = bulb[i - 1] + bkt[i];
  return *min_element(bulb, bulb + n);
}

int main() {
  int n, k, i;
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; ++i)
    scanf("%lld", bulb + i);
  for (i = 0; i < k; ++i)
    if (operate(n) > n)
      break;
  for (int i = 0; i < n; ++i)
    printf("%lld%c", bulb[i], " \\n"[i == n - 1]);
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
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];
  if (K >= N) {
    for (int i = 0; i < N; i++)
      cout << N << (i < N - 1 ? " " : "\\n");
    return 0;
  }
  for (int i = 0; i < K; i++) {
    vector<int> B(N);
    for (int i = 0; i < N; i++) {
      B[max(0, i - A[i])]++;
      if (i + A[i] + 1 <= N - 1)
        B[i + A[i] + 1]--;
    }
    int now = 0;
    bool all = true;
    for (int i = 0; i < N; i++) {
      now += B[i];
      A[i] = now;
      if (A[i] < N)
        all = false;
    }
    if (all)
      break;
  }
  for (int i = 0; i < N; i++)
    cout << A[i] << (i < N - 1 ? " " : "\\n");
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
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];
  if (K >= N) {
    for (int i = 0; i < N; i++)
      cout << N << (i < N - 1 ? " " : "\\n");
    return 0;
  }
  for (int i = 0; i < K; i++) {
    vector<int> B(N);
    for (int i = 0; i < N; i++) {
      B[max(0, i - A[i])]++;
      if (i + A[i] + 1 <= N - 1)
        B[i + A[i] + 1]--;
    }
    int now = 0;
    for (int i = 0; i < N; i++) {
      now += B[i];
      A[i] = now;
    }
  }
  for (int i = 0; i < N; i++)
    cout << A[i] << (i < N - 1 ? " " : "\\n");
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
#define maxn 210000
int a[maxn], v[maxn];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  while (k--) {
    for (int i = 1; i <= n; i++) {
      a[max(1, i - v[i])]++;
      a[min(n + 1, i + v[i] + 1)]--;
    }
    for (int i = 1; i < n; i++)
      a[i + 1] += a[i];
    for (int i = 1; i <= n; i++)
      v[i] = a[i];
    for (int i = 1; i <= n; i++)
      a[i] = 0;
    int flag = 0;
    for (int i = 1; i <= n; i++) {
      if (v[i] != n) {
        flag = 1;
        break;
      }
    }
    if (!flag)
      break;
  }
  for (int i = 1; i <= n; i++)
    cout << v[i] << ' ';
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#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;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k;
  cin >> n >> k;
  k = min(k, 50);
  int a[n], dif[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int j = 0; j < k; j++) {
    for (int i = 0; i < n; i++)
      dif[i] = 0;
    for (int i = 0; i < n; i++) {
      dif[max(0, i - a[i])]++;
      if (min(n - 1, i + a[i]) + 1 < n)
        dif[min(n - 1, i + a[i]) + 1]--;
    }
    for (int i = 0; i < n; i++)
      a[i] = 0;
    for (int i = 0; i < n; i++)
      a[i] = (i > 0 ? a[i - 1] : 0) + dif[i];
  }
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  int a[n], na[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < k; i++) {
    bool flg = false;
    for (int j = 0; j < n; j++)
      na[j] = 0;
    for (int j = 0; j < n; j++) {
      for (int k = j - a[j]; k <= j + a[j]; k++) {
        if (k >= 0 && k < n)
          na[k]++;
      }
    }
    for (int j = 0; j < n; j++)
      a[j] = na[j];
    for (int j = 0; j < n; j++) {
      if (a[j] != n)
        flg = true;
    }
    if (!flg) {
      for (int j = 0; j < n; j++) {
        cout << n << endl;
      }
      return 0;
    }
  }
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
}
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++)

signed main() {
  int N, K;

  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) { cin >> A[i]; }

  rep(k, K) {
    vector<int> B(N + 1);
    rep(i, N) {
      int s = max(0, i - A[i]);
      int l = min(i + A[i] + 1, N);
      B[s]++;
      B[l]--;
    }
    rep(i, N) B[i + 1] += B[i];
    B.pop_back();
    if (A == B)
      break;
    A = B;
  }
  rep(i, N) {
    cout << A[i];
    if (i != N)
      cout << ' ';
  }
  cout << endl;
}


Error: TLE

Fixed submission:

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

signed main() {
  int N, K;
  ll A[400010];
  ll B[400010];

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

  rep(k, K) {
    memset(B, 0, sizeof(B));
    rep(i, N) {
      ll min = i - A[i];
      if (min < 0)
        min = 0;
      B[min]++;
      //            ll max = i + A[i] + 1;
      //            if (max > N) max = N;
      //            B[max]--;
      B[i + A[i] + 1]--;
    }
    ll sum = 0;
    rep(i, N) {
      sum += B[i];
      A[i] = sum;
    }
  }
  rep(i, N) {
    cout << A[i];
    if (i != N)
      cout << ' ';
  }
  cout << endl;
}