alexjercan/bugnet huggingface dataset:
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

template <typename T> struct BinaryIndexedTree {
private:
  int n;
  T *data;

public:
  BinaryIndexedTree(int n = 0) : n(n) {
    data = new T[n + 1];
    for (int i = 0; i <= n; i++)
      data[i] = 0;
  }

  bool add(int index, T x = 1) {
    if (index < 0 || n <= index)
      return false;
    for (index++; index <= n; index += index & -index) {
      data[index] += x;
    }
    return true;
  }

  T sum(int index) {
    if (index < 0)
      index = 0;
    else if (index >= n)
      index = n - 1;

    T x = 0;
    for (index++; index; index -= index & -index) {
      x += data[index];
    }
    return x;
  }

  ~BinaryIndexedTree() { delete[] data; }
};

using namespace std;

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

  if (K >= 500) {
    for (int i = 0; i < N - 1; i++)
      cout << N << ' ';
    cout << N << endl;
  } else {
    vector<int> A(N);
    for (int i = 0; i < N; i++)
      cin >> A[i];
    for (int k = 0; k < K; k++) {
      vector<int> B(N + 1, 0);
      for (int i = 0; i < N; i++) {
        B[max(i - A[i], 0)]++;
        B[min(i + A[i] + 1, N)]--;
      }
      A[0] = B[0];
      for (int i = 1; i < N; i++) {
        B[i] += B[i - 1];
        A[i] = B[i];
      }
    }

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

  return 0;
}

Error: 1

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  if (K >= 500) {
    for (int i = 0; i < N - 1; i++)
      cout << N << ' ';
    cout << N << endl;
  } else {
    exit(1);
  }

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

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) { cin >> A.at(i); }
  rep(i, min(K, (ll)100)) {
    vector<ll> hoge(N, 0);
    vector<ll> sirusi(N, 0);
    rep(i, N) {
      sirusi.at(max((ll)0, i - A.at(i)))++;
      if (i + A.at(i) < N - 1) {
        sirusi.at(i + A.at(i) + 1)--;
      }
    }
    rep(i, N) {
      if (i == 0) {
        hoge.at(0) = sirusi.at(0);
        continue;
      }
      hoge.at(i) = hoge.at(i - 1) + sirusi.at(i);
    }
    A = hoge;
  }
  rep(i, N) {
    if (i != 0) {
      cout << " ";
    }
    cout << A.at(i);
  }
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  rep(i, N) { cin >> A.at(i); }
  rep(i, min(K, (ll)1000)) {
    vector<ll> hoge(N, 0);
    vector<ll> sirusi(N, 0);
    rep(i, N) {
      sirusi.at(max((ll)0, i - A.at(i)))++;
      if (i + A.at(i) < N - 1) {
        sirusi.at(i + A.at(i) + 1)--;
      }
    }
    rep(i, N) {
      if (i == 0) {
        hoge.at(0) = sirusi.at(0);
        continue;
      }
      hoge.at(i) = hoge.at(i - 1) + sirusi.at(i);
    }
    A = hoge;
  }
  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 <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using i64 = int64_t;
using P = pair<i64, i64>;
// i64 mod = 1000000007;
i64 mod = 998244353;
int int_max = 2147483647;
i64 INF = 1e16;
int max_n = 1e5;

vector<i64> fibo(50);

void simulation_once(const vector<i64> &in, vector<i64> &out) {
  i64 sz = in.size();
  for (i64 i = 0; i < sz; ++i) {
    i64 num = in[i];
    i64 left = max((i64)0, i - num);
    i64 right = min(sz - 1, i + num);
    ++out[left];
    if (right + 1 < sz)
      --out[right + 1];
  }
  for (i64 i = 1; i < sz; ++i) {
    out[i] = out[i - 1] + out[i];
  }
}

void print_v(const vector<i64> &v) {
  for (auto i : v) {
    cout << i << " ";
  }
  cout << endl;
}
void print_diff(const vector<i64> &v, const vector<i64> &v2) {
  for (size_t i = 0; i < v.size(); ++i) {
    cout << (v[i] - v2[i]) << " ";
  }
  cout << endl;
}

int main(int argc, char **argv) {
  i64 n, k;
  cin >> n >> k;

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

  vector<i64> b = a, c(n, 0);

  for (int k1 = 0; k1 < k && k1 * k1 < n; ++k1) {
    vector<i64> tmp(n, 0), tmp2(n, 0);
    simulation_once(b, tmp);
    b = std::move(tmp);
  }

  print_v(b);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using i64 = int64_t;
using P = pair<i64, i64>;
// i64 mod = 1000000007;
i64 mod = 998244353;
int int_max = 2147483647;
i64 INF = 1e16;
int max_n = 1e5;

void simulation_once(const vector<i64> &in, vector<i64> &out) {
  for (int i = 0; i < in.size(); ++i) {
    i64 num = in[i];
    for (i64 j = i - num; j <= i + num; ++j) {
      if (0 <= j && j < out.size())
        ++out[j];
    }
  }
}

void print_v(const vector<i64> &v) {
  for (auto i : v) {
    cout << i << " ";
  }
  cout << endl;
}
void print_diff(const vector<i64> &v, const vector<i64> &v2) {
  for (size_t i = 0; i < v.size(); ++i) {
    cout << (v[i] - v2[i]) << " ";
  }
  cout << endl;
}

int main(int argc, char **argv) {
  i64 n, k;
  cin >> n >> k;

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

  vector<i64> b = a, c(n, 0);

  for (int k1 = 0; k1 < k; ++k1) {
    vector<i64> tmp(n, 0), tmp2(n, 0);
    simulation_once(b, tmp);
    b = std::move(tmp);
    if (b[0] == b[n - 1] && b[0] == n)
      break;
  }

  print_v(b);

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

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define inf 1000000000000000
#define mod 1000000007
#define ld long double
#define pll pair<ll, ll>
using namespace std;
ll n;

vector<ll> bit2;

void add2(ll i, ll val) {
  i++;

  for (i; i <= n; i += i & (-i))
    bit2[i] += val;
}

void range_update2(ll l, ll r, ll val) {
  add2(l, val);
  add2(r + 1, -val);
}

ll query2(ll i) {
  ll ans = 0;
  i++;

  for (i; i > 0; i -= (i & (-i)))
    ans += bit2[i];

  return ans;
}

int main() {

  fast;

  ll k;
  cin >> n >> k;

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

  for (ll tes = 1; tes <= k; tes++) {
    bit2.assign(n + 10, 0);

    for (ll j = 0; j < n; j++)
      range_update2(max((ll)0, j - a[j]), min(n - 1, a[j] + j), 1);

    bool f = 1;

    for (ll j = 0; j < n; j++)
      if (a[j] != query2(j)) {
        f = 0;
        break;
      }

    if (f)
      break;
    else {
      for (ll j = 0; j < n; j++)
        a[j] = query2(j);
    }
  }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define inf 1000000000000000
#define mod 1000000007
#define ld long double
#define pll pair<ll, ll>
using namespace std;

int main() {

  fast;

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

  ll a[n + 1];
  ll left[n + 1], right[n + 1];
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    left[i] = max((ll)1, i - a[i]);
    right[i] = min(n, i + a[i]);
  }

  for (ll i = 1; i <= k; i++) {
    vector<ll> t(n + 1, 0);
    for (ll j = 1; j <= n; j++)
      for (ll q = left[j]; q <= right[j]; q++)
        t[q]++;

    bool f = 1;

    for (ll j = 1; j <= n; j++)
      if (a[j] != t[j]) {
        f = 0;
        break;
      }

    if (f)
      break;
    else {
      for (ll i = 1; i <= n; i++) {
        a[i] = t[i];
        left[i] = max((ll)1, i - a[i]);
        right[i] = min(n, i + a[i]);
      }
    }
  }

  for (ll i = 1; i <= n; i++)
    cout << a[i] << " ";
}
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 200010;
int n, m, minn = 1 << 30;
int a[N], sum[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int k = 1; k <= n; k++)
    scanf("%d", &a[k]), minn = min(minn, a[k]);
  for (int k = 1; k <= m && minn < n; k++) {
    memset(sum, 0, sizeof sum);
    for (int i = 1; i <= n; i++) {
      sum[max(1, i - a[i])]++;
      sum[min(i + a[i] + 1, n + 1)]--;
    }
    for (int i = 1; i <= n; i++)
      sum[i] += sum[i - 1];
    minn = 1 << 30;
    for (int i = 1; i <= n; i++)
      a[i] = sum[i], minn = min(minn, a[i]);
  }
  for (int k = 1; k <= n; k++)
    printf("%d ", a[k]);
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 200010;
int n, m, minn = 1 << 30;
int a[N], sum[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int k = 1; k <= n; k++)
    scanf("%d", &a[k]), minn = min(minn, a[k]);
  for (int k = 1; k <= m && minn < n; k++) {
    memset(sum, 0, sizeof sum);
    for (int i = 1; i <= n; i++) {
      sum[i - a[i]]++;
      sum[i + a[i] + 1]--;
    }
    for (int i = 1; i <= n; i++)
      sum[i] += sum[i - 1];
    minn = 1 << 30;
    for (int i = 1; i <= n; i++)
      a[i] = sum[i], minn = min(minn, a[i]);
  }
  for (int k = 1; k <= n; k++)
    printf("%d ", a[k]);
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) for (int i = 1; i < (int)(n); ++i)
using namespace std;
using lint = int64_t;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  rep(i, K) {
    bool judge = true;
    rep(i, N) {
      if (A[i] != N)
        judge = false;
    }
    if (judge)
      break;
    vector<int> B(N + 1, 0);
    rep(i, N) {
      ++B[max(0, i - A[i])];
      --B[min(N, i + A[i] + 1)];
    }
    A[0] = B[0];
    REP(i, N) A[i] = A[i - 1] + B[i];
  }
  rep(i, N) cout << A[i] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) for (int i = 1; i < (int)(n); ++i)
using namespace std;
using lint = int64_t;

struct LazySegmentTree {
  int length;
  vector<lint> node, lazy;
  LazySegmentTree(int N) {
    length = 1;
    while (length < N)
      length *= 2;
    node.resize(2 * length - 1, 0);
    lazy.resize(2 * length - 1, 0);
  }
  void eval(int k) {
    node[k] += lazy[k];
    if (k < length - 1) {
      lazy[2 * k + 1] += lazy[k] / 2;
      lazy[2 * k + 2] += lazy[k] / 2;
    }
    lazy[k] = 0;
  }
  void update(int a, int b, lint x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = length;
    eval(k);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] += x * (r - l);
      eval(k);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }
  lint getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = length;
    if (b <= l || r <= a)
      return 0;
    eval(k);
    if (a <= l && r <= b)
      return node[k];
    lint vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    lint vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  rep(i, K) {
    bool judge = true;
    rep(i, N) {
      if (A[i] != N)
        judge = false;
    }
    if (judge)
      break;
    LazySegmentTree LST(N);
    rep(i, N) { LST.update(max(0, i - A[i]), min(N, i + A[i] + 1), 1); }
    rep(i, N) { A[i] = LST.getsum(i, i + 1); }
  }
  rep(i, N) cout << A[i] << "\\n";
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using pint = pair<int, int>;
using vec = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

  rep(ki, k) {
    vec b(n);
    rep(i, n) {
      int l = max(0, i - a[i]);
      int r = min(i + a[i] + 1, n);
      b[l]++;
      b[r]--;
    }

    rep(i, n) b[i + 1] += b[i];
    if (a == b)
      break;
    a = b;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <vector>
#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 pint = pair<int, int>;
using vec = vector<int>;

int main() {
  int n, k;
  cin >> n >> k;
  vec a(n + 1);
  rep(i, n) { cin >> a[i]; }

  for (int l = 0; l < k; l++) {
    vec b(n + 1);
    for (int i = 0; i < n; i++) {
      b[i] += 1;
      for (int j = 1; j <= a[i]; j++) {
        if (i + j < n + 1)
          b[i + j] += 1;
        if (i - j >= 0)
          b[i - j] += 1;
      }
    }

    rep(i, n) { a[i] = b[i]; }
  }

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

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

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

  rep(i, k) {

    vector<int> memo(n + 1);

    rep(j, n) {
      int min, max;
      min = j - a[i][j];
      max = j + 1 + a[i][j];
      if (min < 0)
        min = 0;
      if (max > n)
        max = n;
      memo[min]++;
      memo[max]--;
    }

    int cnt = 0;

    rep(j, n) {
      cnt += memo[j];
      a[i + 1][j] = cnt;
    }

    bool fin = true;

    rep(j, n) {
      if (a[i][j] != a[i + 1][j])
        fin = false;
    }

    if (fin) {
      k = i + 1;
      break;
    }
  }

  rep(j, n - 1) { cout << a[k][j] << " "; }
  cout << a[k][n - 1] << endl;

  return 0;
}

Error: 0

Fixed submission:

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

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

  rep(i, k) {

    vector<int> memo(n + 1);

    rep(j, n) {
      int min, max;
      min = j - a[i][j];
      max = j + 1 + a[i][j];
      if (min < 0)
        min = 0;
      if (max > n)
        max = n;
      memo[min]++;
      memo[max]--;
    }

    int cnt = 0;

    rep(j, n) {
      cnt += memo[j];
      a[i + 1][j] = cnt;
    }

    bool fin = true;

    rep(j, n) {
      if (a[i][j] != a[i + 1][j])
        fin = false;
    }

    if (fin) {
      k = i + 1;
      break;
    }
  }

  rep(j, n - 1) { cout << a[k][j] << " "; }
  cout << a[k][n - 1] << endl;

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

/*input
3 2
0 0 0
*/

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 2;

int a[N], s[N + 4];

unordered_set<int> ss;

bool checkSingle(int n) {
  return (ss.size() == 1) && (ss.find(n) == ss.begin());
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k;
  cin >> n >> k;

  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    ss.insert(a[i]);
  }

  int single = checkSingle(n);
  while (!single && k) {
    ss.clear();

    for (int i = 0; i <= n + 1; ++i) {
      s[i] = 0;
    }
    for (int i = 1; i <= n; ++i) {
      s[max(0, i - a[i])]++;
      s[min(N, i + a[i]) + 1]--;
    }
    for (int i = 1; i <= n; ++i) {
      s[i] += s[i - 1];
      a[i] = s[i];
      ss.insert(a[i]);
    }

    single = checkSingle(n);
    k--;
  }

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

  return 0;
}

Error: -6
5cd83e13-c3ce-430e-9d6b-1424c79ec3c6.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02647/C++/s480142783.cpp:41: int main(int, const char**): Assertion `a[i] != 0' failed.


Fixed submission:

/*input
5 6
0 0 0 0 0
*/

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 2;

int a[N], s[N + 4];

unordered_set<int> ss;

bool checkSingle(int n) {
  return (ss.size() == 1) &&
         (ss.find(0) == ss.begin() || ss.find(n) == ss.begin());
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k;
  cin >> n >> k;

  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    ss.insert(a[i]);
  }

  int single = checkSingle(n);
  while (!single && k) {
    ss.clear();

    for (int i = 0; i <= n + 1; ++i) {
      s[i] = 0;
    }
    for (int i = 1; i <= n; ++i) {
      assert(a[i] != 0);
      s[max(0, i - a[i])]++;
      s[min(N, i + a[i]) + 1]--;
    }
    for (int i = 1; i <= n; ++i) {
      s[i] += s[i - 1];
      a[i] = s[i];
      ss.insert(a[i]);
    }

    single = checkSingle(n);
    k--;
  }

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

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

// This code was made by Chinese_zjc_.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define int long long
#define INF 0x3fffffffffffffff
using namespace std;
int n, k, a[200002], b[200002];
signed main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int j = 1; j <= k; ++j) {
    for (int i = 1; i <= n; ++i) {
      ++b[max(1ll, i - a[i])];
      --b[min(n + 1, i + a[i] + 1)];
    }
    for (int i = 1; i <= n; ++i) {
      a[i] = b[i] + a[i - 1];
      b[i] = 0;
    }
    bool flag = true;
    for (int i = 1; i <= n; ++i) {
      if (a[i] != n) {
        flag = false;
        break;
      }
    }
    if (flag) {
      break;
    }
  }
  for (int i = 1; i <= n; ++i) {
    cout << a[i] << ' ';
  }
  return 0;
}


Error: WA

Fixed submission:

// This code was made by Chinese_zjc_.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define int long long
#define INF 0x3fffffffffffffff
using namespace std;
int n, k, a[100002], b[100002];
signed main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int j = 1; j <= k; ++j) {
    for (int i = 1; i <= n; ++i) {
      ++b[max(1ll, i - a[i])];
      --b[min(n + 1, i + a[i] + 1)];
    }
    for (int i = 1; i <= n; ++i) {
      a[i] = b[i] + a[i - 1];
      b[i] = 0;
    }
    //		for(int i=1;i<=n;++i)
    //		{
    //			cout<<a[i]<<' ';
    //		}
    //		cout<<endl;
  }
  for (int i = 1; i <= n; ++i) {
    cout << a[i] << ' ';
  }
  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 (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  int a;
  int min;
  int max;
  int count_max;
  for (int k = 0; k < K; k++) {
    vector<int> B(N);
    count_max = 0;
    for (int i = 0; i < N; i++) {
      a = A.at(i);
      if (a == N) {
        count_max += 1;
      }
      min = i - a;
      max = i + a;
      if (min < 0) {
        min = 0;
      }
      if (N - 1 < max) {
        max = N - 1;
      }

      B.at(min) += 1;
      if (max + 1 < N) {
        B.at(max + 1) -= 1;
      }
    }

    for (int i = 1; i < N; i++) {
      B.at(i) += B.at(i - 1);
    }
    A = B;
    if (count_max == N) {
      break;
    }
  }

  for (int i = 0; i < N; i++) {
    cout << A.at(i) << " ";
  }
  cout << "" << endl;
  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);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }

  int a;
  int min;
  int max;
  for (int k = 0; k < K; k++) {
    vector<int> B(N);
    int count_max;
    for (int i = 0; i < N; i++) {
      a = A.at(i);
      if (a == N) {
        count_max += 1;
      }
      min = i - a;
      max = i + a;
      if (min < 0) {
        min = 0;
      }
      if (N - 1 < max) {
        max = N - 1;
      }

      for (int j = min; j <= max; j++) {
        B.at(j) += 1;
      }
    }
    A = B;
    if (count_max == N) {
      break;
    }
  }

  for (int i = 0; i < N; i++) {
    cout << A.at(i) << " ";
  }
  cout << "" << endl;
  return 0;
}
Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);
  // naive
  for (int i = 1; i <= k; i++) {
    vector<int> imos(n + 1, 0);
    for (int j = 0; j < n; j++) {
      int pp = max(0, j - a.at(j));
      int mp = min(n, j + a.at(j) + 1);
      imos.at(pp)++;
      imos.at(mp)--;
    }
    for (int j = 1; j < n; j++)
      imos.at(j) += imos.at(j - 1);
    bool x = false;
    for (int j = 0; j < n; j++) {
      if (a.at(j) != imos.at(j)) {
        x = true;
        break;
      }
    }
    if (!x)
      break;
    for (int j = 0; j < n; j++)
      a.at(j) = imos.at(j);
  }
  for (int i = 0; i < n; i++) {
    if (i == 0)
      cout << a.at(i);
    else
      cout << " " << a.at(i);
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i);
  // naive
  for (int i = 1; i <= k; i++) {
    vector<int> imos(n + 1, 0);
    for (int j = 0; j < n; j++) {
      int pp = max(0, j - a.at(j));
      int mp = min(n, j + a.at(j) + 1);
      imos.at(pp)++;
      imos.at(mp)--;
    }
    for (int j = 1; j < n; j++)
      imos.at(j) += imos.at(j - 1);
    for (int j = 0; j < n; j++)
      a.at(j) = imos.at(j);
  }
  for (int i = 0; i < n; i++) {
    if (i == 0)
      cout << a.at(i);
    else
      cout << " " << a.at(i);
  }
}