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 pll = pair<ll, ll>;
using pii = pair<int, int>;
#define ALL(x) (x).begin(), (x).end()

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N + 1, 0);
  for (int i = 1; i <= N; i++)
    cin >> A[i];

  vector<ll> out(N + 1);

  if (K <= 100) {
    for (int k = 0; k < K; k++) {
      for (int i = 0; i <= N; i++)
        out[i] = 0;

      for (int i = 1; i <= N; i++) {
        if (i - A[i] >= 0)
          out[i - A[i]]++;
        else
          out[0]++;

        if (i + A[i] + 1 <= N)
          out[i + A[i] + 1]--;
      }

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

    for (int i = 1; i <= N; i++)
      cout << out[i] << ((i == N) ? '\\n' : ' ');
  } else {
    for (int i = 1; i <= N; i++)
      cout << N << ((i == N) ? '\\n' : ' ');
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
#define ALL(x) (x).begin(), (x).end()

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N + 1, 0);
  for (int i = 1; i <= N; i++)
    cin >> A[i];

  vector<ll> out(N + 1);

  for (int k = 0; k < K; k++) {
    for (int i = 0; i <= N; i++)
      out[i] = 0;

    for (int i = 1; i <= N; i++) {
      if (i - A[i] >= 0)
        out[i - A[i]]++;
      else
        out[0]++;

      if (i + A[i] + 1 <= N)
        out[i + A[i] + 1]--;
    }

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

  for (int i = 1; i <= N; i++)
    cout << out[i] << ((i == N) ? '\\n' : ' ');
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}

int main() {
  run();
  int n, k;
  cin >> n >> k;
  vector<int> v(n), up(n + 1);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  while (k--) {
    for (int i = 0; i < n; i++) {
      up[max(i - v[i], 0)]++;
      up[min(i + v[i] + 1, n)]--;
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      v[i] = (sum += up[i]);
      up[i] = 0;
    }
    if (*min_element(all(v)) == n)
      break;
  }
  for (int i = 0; i < n; i++)
    cout << v[i] << ' ';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(), v.end()
#define watch(x) cout << (#x) << " = " << x << endl
const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1};
const int dc[]{0, 1, 1, 1, 0, -1, -1, -1};
#if __cplusplus >= 201402L
template <typename T> vector<T> create(size_t n) { return vector<T>(n); }
template <typename T, typename... Args> auto create(size_t n, Args... args) {
  return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef EZZAT
  freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
#else
#endif
}

int main() {
  run();
  int n, k;
  cin >> n >> k;
  vector<int> v(n + 1), up(n + 1);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  while (k--) {
    for (int i = 0; i < n; i++) {
      up[max(i - v[i], 0)]++;
      up[min(i + v[i] + 1, n)]--;
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      v[i] = (sum += up[i]);
      up[i] = 0;
    }
    if (*min_element(all(v)) == n)
      break;
  }
  for (int i = 0; i < n; i++)
    cout << v[i] << ' ';
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int N, K;
int main() {
  cin >> N >> K;
  int A[2][N + 1];
  for (int i = 0; i < N; i++)
    cin >> A[0][i];
  for (int i = 0; i <= N; i++)
    A[1][i] = 0LL;
  for (int k = 0; k < min(K, 100); k++) {
    for (int i = 0; i < N; i++) {
      A[(k + 1) % 2][max(0, i - A[k % 2][i])]++;
      A[(k + 1) % 2][min(N, i + A[k % 2][i] + 1)]--;
    }
    for (int i = 1; i <= N; i++)
      A[(k + 1) % 2][i] += A[(k + 1) % 2][i - 1], A[k % 2][i - 1] = 0;
  }
  for (int i = 0; i < N; i++)
    cout << A[(K < 100) ? K % 2 : 0][i] << " \\n"[i + 1 == N];
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long N, K;
int main() {
  cin >> N >> K;
  long long A[2][N];
  for (int i = 0; i < N; i++)
    cin >> A[0][i];
  for (int i = 0; i < N; i++)
    A[1][i] = 0LL;
  for (int k = 0; k < min(K, 1000LL); k++) {
    for (int i = 0; i < N; i++) {
      for (int d = -A[k % 2][i]; d <= A[k % 2][i]; d++) {
        if (i + d < 0 || N <= i + d)
          continue;
        A[(k + 1) % 2][i + d]++;
      }
    }
    for (int j = 0; j < N; j++)
      A[k % 2][j] = 0;
  }
  for (int i = 0; i < N; i++)
    cout << A[K % 2][i] << " \\n"[i + 1 == N];
  return 0;
}
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++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  vector<int> imos(n + 1, 0);
  rep(i, k) {
    rep(j, n + 1) { imos.at(j) = 0; }
    rep(j, n) {
      int l = max(0, j - a.at(j));
      int r = min(n, j + a.at(j) + 1);
      imos.at(l)++;
      imos.at(r)--;
    }
    int now = 0;
    bool f = false;
    rep(i, n) {
      now += imos.at(i);
      a.at(i) = now;
      if (a.at(i) != n)
        f = true;
    }
    if (f == false)
      break;
  }
  rep(i, n) {
    if (i != 0)
      cout << " ";
    cout << a.at(i);
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  vector<int> nx(n, 0);
  rep(i, k) {
    rep(j, n) nx.at(j) = 0;
    rep(j, n) {
      int p = a.at(j);
      nx.at(j)++;
      rep2(k, 1, p + 1) {
        if (j + k < n)
          nx.at(j + k)++;
        if (j - k >= 0)
          nx.at(j - k)++;
      }
    }
    bool f = false;
    rep(j, n) {
      a.at(j) = min(n, nx.at(j));
      if (a.at(j) != n)
        f = true;
    }
    if (f == false)
      break;
  }
  rep(i, n) {
    if (i != 0)
      cout << " ";
    cout << a.at(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++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
using P = pair<int, int>;
using pq = priority_queue<int>;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  int k;
  cin >> 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(i - a[i], 0);
      int r = min(i + a[i], n - 1);
      b[l]++;
      if (r < n + 1)
        b[r + 1]--;
    }
    for (int i = 1; i < n; i++)
      b[i] += b[i - 1];
    if (a == b)
      break;
    a = b;
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
  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 rep1(i, n) for (int i = 1; i <= (int)(n); i++)
using P = pair<int, int>;
using pq = priority_queue<int>;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  int k;
  cin >> 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(i - a[i], 0);
      int r = min(i + a[i], n - 1);
      b[l]++;
      if (r < n + 1)
        b[r + 1]--;
    }
    for (int i = 1; i < n; i++)
      b[i] += b[i - 1];
    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 REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
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> imos(N + 1, 0);
    REP(j, N) {
      imos.at(max(0, j - A[j]))++;
      imos.at(min(N, j + A[j] + 1))--;
    }
    int sum = 0;
    REP(j, N) {
      sum += imos.at(j);
      A[j] = sum;
    }
    int flag = 0;
    REP(j, N) {
      if (A[j] != N) {
        flag = 1;
      }
    }
    if (flag == 0) {
      break;
    }
  }

  REP(i, N) {
    if (i != N - 1) {
      cout << A[i] << " ";
    } else {
      cout << A[i];
    }
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
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> imos(N + 1, 0);
    REP(j, N) {
      imos.at(max(0, j - A[j]))++;
      imos.at(min(N, j + A[j] + 1))--;
    }
    int sum = 0;
    REP(j, N) {
      sum += imos.at(j);
      A[j] = sum;
    }
  }

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

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

// 総数を1000000007（素数）で割った余り
const long long mod = 1e9 + 7;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

int main() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, n) cin >> a[i];
  rep(i, k) {
    vi b(n);
    rep(j, n) {
      int l = max(0, j - a[j]), r = min(n - 1, j + a[j]);
      b[l]++;
      if (r < n - 1)
        b[r + 1]--;
    }
    rep1(j, n - 1) b[j] += b[j - 1];
    a = b;
    bool ok = true;
    rep(j, n) if (a[j] != n) ok = false;
    if (ok)
      break;
  }
  rep(i, n) printf("%d%c", a[i], (i < n - 1 ? ' ' : '\\n'));
  return 0;
}


Error: TLE

Fixed submission:

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

// 総数を1000000007（素数）で割った余り
const long long mod = 1e9 + 7;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

int main() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, n) cin >> a[i];
  rep(i, k) {
    vi b(n);
    rep(j, n) {
      int l = max(0, j - a[j]), r = min(n - 1, j + a[j]);
      for (int j2 = l; j2 <= r; ++j2) {
        b[j2]++;
      }
    }
    a = b;
    bool ok = true;
    rep(j, n) if (a[j] != n) ok = false;
    if (ok)
      break;
  }
  rep(i, n) printf("%d%c", a[i], (i < n - 1 ? ' ' : '\\n'));
  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 ll = long long int;
using namespace std;
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;
}
ll mod = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  k = min(k, 50);
  int l[n], r[n];
  int g[n];
  rep(i, n) cin >> g[i];
  int a[n];

  rep(times, k) {
    rep(i, n) a[i] = 0;
    for (int i = 1; i <= n; i++) {
      l[i -
        1] = max(i - g[i - 1] - 1, 0),
        r[i - 1] = min(
            i + g[i - 1],
            n); // 始点を+1,終点を-1加える(もし始点と終点が同じ場合があるなら、--lだけにする)
      a[l[i - 1]]++;
      a[r[i - 1]]--;
    }

    rep(i, n) {
      if (0 < i)
        a[i] += a[i - 1]; // シュミレーションする
    }
    rep(i, n) g[i] = a[i];
  }
  rep(i, n) cout << min(a[i], n) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long int;
using namespace std;
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;
}
ll mod = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  int l[n], r[n];
  int g[n];
  rep(i, n) cin >> g[i];
  int a[n];

  rep(times, k) {
    rep(i, n) a[i] = 0;
    for (int i = 1; i <= n; i++) {
      l[i -
        1] = max(i - g[i - 1] - 1, 0),
        r[i - 1] = min(
            i + g[i - 1],
            n); // 始点を+1,終点を-1加える(もし始点と終点が同じ場合があるなら、--lだけにする)
      a[l[i - 1]]++;
      a[r[i - 1]]--;
    }

    rep(i, n) {
      if (0 < i)
        a[i] += a[i - 1]; // シュミレーションする
    }
    rep(i, n) g[i] = a[i];
  }
  rep(i, n) cout << a[i] << endl;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define mod 1e9 + 7

int main()

{

  int n, k;

  cin >> n >> k;

  int a[n + 1], b[n + 1];

  for (int i = 1; i <= n; i++)

  {

    cin >> a[i];
  }

  while (k--)

  {

    memset(b, 0, sizeof(b));

    for (int i = 1; i <= n; i++)

    {

      if (a[i] + i < n)

      {

        b[i]++;

        b[i + a[i] + 1]--;

      }

      else

      {

        b[i]++;
      }

      if (i - a[i] >= 1)

      {

        b[i - a[i]]++;

        b[i]--;

      }

      else

      {

        b[1]++;

        b[i]--;
      }
    }

    a[1] = b[1];

    for (int i = 2; i <= n; i++)

    {

      a[i] = a[i - 1] + b[i];
    }

    int flag = 0;

    for (int i = 1; i <= n; i++)

    {

      if (a[i] != n)

      {

        flag++;

        break;
      }
    }

    if (!flag)

    {

      break;
    }
  }

  for (int i = 1; i <= n; i++)

  {

    cout << a[i] << " ";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define mod 1e9 + 7

int main()

{

  int n, k;

  cin >> n >> k;

  int a[n + 1], b[n + 1];

  for (int i = 1; i <= n; i++)

  {

    cin >> a[i];
  }

  while (k--)

  {

    memset(b, 0, sizeof(b));

    for (int i = 1; i <= n; i++)

    {

      if (a[i] + i < n)

      {

        b[i]++;

        b[i + a[i] + 1]--;

      }

      else

      {

        b[i]++;
      }

      if (i - a[i] >= 1)

      {

        b[i - a[i]]++;

        b[i]--;

      }

      else

      {

        b[1]++;

        b[i]--;
      }
    }

    a[1] = b[1];

    for (int i = 2; i <= n; i++)

    {

      a[i] = a[i - 1] + b[i];
    }
  }

  for (int i = 1; i <= n; i++)

  {

    cout << a[i] << " ";
  }

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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

bool iterate(std::vector<int> &a) {
  const int n = a.size();
  bool converged = true;
  std::vector<int> b(n, 0);
  std::vector<int> up(n, 0);
  std::vector<int> down(n, 0);

  for (int i = 0; i < n; i++) {
    const int i_up = (i - a[i] < 0) ? (0) : (i - a[i]);
    const int i_down = (i + a[i] >= n) ? (n - 1) : (i + a[i]);

    up[i_up]++;
    down[i_down]++;
  }

  int count = 0;
  for (int i = 0; i < n; i++) {
    count += up[i];
    b[i] = count;
    count -= down[i];
  }

  a.swap(b);

  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      converged = false;
    }
  }

  return converged;
}

int main(int argc, char *argv[]) {
  int n, k;
  std::vector<int> a;

  std::cin >> n >> k;

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

  for (int i = 0; i < k; i++) {
    bool converged = iterate(a);
    if (converged) {
      break;
    }
  }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

void iterate(std::vector<int> &a) {
  const int n = a.size();
  std::vector<int> b(n, 0);
  std::vector<int> up(n, 0);
  std::vector<int> down(n, 0);

  for (int i = 0; i < n; i++) {
    const int i_up = (i - a[i] < 0) ? (0) : (i - a[i]);
    const int i_down = (i + a[i] >= n) ? (n - 1) : (i + a[i]);

    up[i_up]++;
    down[i_down]++;
  }

  int count = 0;
  for (int i = 0; i < n; i++) {
    count += up[i];
    b[i] = count;
    count -= down[i];
  }

  a.swap(b);
}

int main(int argc, char *argv[]) {
  int n, k;
  std::vector<int> a;

  std::cin >> n >> k;

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

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

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
long double eps = 1e-9;
long double pi = acos(-1);

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  while (k--) {
    vector<int> sum(n);
    for (int i = 0; i < n; i++) {
      sum[max(0, i - a[i])]++;
      if (i + a[i] + 1 < n)
        sum[i + 1 + a[i]]--;
    }
    bool ok = 1;
    if (sum[0] != n)
      ok = false;
    for (int i = 1; i < n; i++) {
      sum[i] += sum[i - 1];
      if (sum[i] != n)
        ok = false;
    }
    swap(a, sum);
    if (ok)
      break;
  }
  for (int i = 0; i < n; i++)
    cout << a[i] << " ";
  cout << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
long double eps = 1e-9;
long double pi = acos(-1);

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  while (k--) {
    vector<int> sum(n);
    for (int i = 0; i < n; i++) {
      sum[max(0, i - a[i])]++;
      if (i + a[i] + 1 < n)
        sum[i + 1 + a[i]]--;
    }
    bool ok = 1;
    if (sum[0] != 0)
      ok = false;
    for (int i = 1; i < n; i++) {
      sum[i] += sum[i - 1];
      if (sum[i] != n)
        ok = false;
    }
    swap(a, sum);
    if (ok)
      break;
  }
  for (int i = 0; i < n; i++)
    cout << a[i] << " ";
  cout << endl;
}