Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define DEBUG
#define PI 3.141592653589793238462643383279

#define _GLIBCXX_DEBUG
#ifdef DEBUG
#define s(...) show(__VA_ARGS__);
#define sl(...)                                                                \
  show(__VA_ARGS__);                                                           \
  cout << endl;
#else
#define s(...)
#define sl(...)
#endif
void show() {}
template <class Head, class... Args> void show(Head t, Args... args) {
  std::cout << t << " ";
  show(args...);
}
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)
#define REPR1(i, n) for (ll i = n; i >= 1; i--)
#define FOR1(i, m, n) for (ll i = m; i <= n; i++)
const ll INF = LLONG_MAX;
const ull MOD = 1000000007;
#define VEC(type, A, N) vector<type> A(N)
#define VECi(type, A, N, i) vector<type> A(N, i)
#define VEC2(type, A, N, M) vector<vector<type>> A(N, vector<type>(M))
#define VEC2i(type, A, N, M, i) vector<vector<type>> A(N, vector<type>(M, i))
#define ALL(v) v.begin(), v.end()
ll frac(ll k) {
  int s = 1;
  for (ll i = 1; i <= k; ++i)
    s *= i;
  return s;
}
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll antidiv(ll N, ll C, ll D) {
  return N - floor(N / C) - floor(N / D) + floor(N / lcm(C, D));
}
vector<ll> yakusuu(ll N) {
  vector<ll> ret;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      ret.push_back(i);
      if (i * i != N)
        ret.push_back(N / i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}
ll digitPoor(ll n) {
  assert(INT_MAX == 2147483647);
  assert(n >= 0);

  if (n < 10)
    return 1;
  if (n < 100)
    return 2;
  if (n < 1000)
    return 3;
  if (n < 10000)
    return 4;
  if (n < 100000)
    return 5;
  if (n < 1000000)
    return 6;
  if (n < 10000000)
    return 7;
  if (n < 100000000)
    return 8;
  if (n < 1000000000)
    return 9;
  return 10;
}
struct p {
  ll x;
  bool end;
};

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

  VEC(ll, A, N);
  REP(i, N) { cin >> A[i]; }

  REP(i, K) {
    bool allN = true;
    VEC(p, ps, 0);
    REP(j, N) {
      ll t = j - A[j] < 0 ? 0 : j - A[j];
      ll tt = j + A[j] > N - 1 ? N - 1 : j + A[j];
      ps.push_back({t, false});
      ps.push_back({tt, true});
    }
    sort(ALL(ps), [](p a, p b) {
      if (a.x > b.x) {
        return 0;
      } else if (a.x < b.x) {
        return 1;
      }
    });
    ll cu = 0, nowcu = 0, bef = 0;
    REP(j, N * 2) {
      p pp = ps[j];
      if (pp.x == bef + 1) {
        // cout<<nowcu<<" ";
        // sl(pp.x-1,"=",nowcu)
        A[pp.x - 1] = nowcu;
        if (A[pp.x - 1] != N)
          allN = false;
        nowcu = cu;
      } else if (pp.x > bef + 1) {
        ll tttt = pp.x - bef;
        // sl(pp.x-tttt,"=",nowcu)
        A[pp.x - tttt] = nowcu;
        // cout<<nowcu<<" ";
        while (--tttt > 0) {
          // cout<<cu<<" ";
          // sl(pp.x-tttt,"==",cu)
          A[pp.x - tttt] = cu;
          if (A[pp.x - tttt] != N)
            allN = false;
        }
        nowcu = cu;
      }
      if (pp.end == false) {
        cu++;
        nowcu++;
      } else {
        cu--;
      }
      bef = pp.x;
      // sl("nowcu=",nowcu,"cu=",cu)
    }
    ll tttt = N - bef;
    while (--tttt >= 0) {
      // cout<<nowcu<<" ";
      // sl(N-tttt-1,nowcu)
      A[N - tttt - 1] = nowcu;
      if (A[N - tttt - 1] != N)
        allN = false;
    }
    if (allN)
      break;
  }
  REP(i, N) { cout << A[i] << " "; }

  // cout<<fixed<<setprecision(32)<<m;
  // sort(ALL(C),[](auto& a, auto& b) {return a[2] < b[2];});
  // if(equal(D.begin(), D.end()-1, K.begin()))cout<<"Yes";else cout<<"No";
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define DEBUG
#define PI 3.141592653589793238462643383279

#define _GLIBCXX_DEBUG
#ifdef DEBUG
#define s(...) show(__VA_ARGS__);
#define sl(...)                                                                \
  show(__VA_ARGS__);                                                           \
  cout << endl;
#else
#define s(...)
#define sl(...)
#endif
void show() {}
template <class Head, class... Args> void show(Head t, Args... args) {
  std::cout << t << " ";
  show(args...);
}
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define REP1(i, n) for (ll i = 1; i <= n; i++)
#define REPR1(i, n) for (ll i = n; i >= 1; i--)
#define FOR1(i, m, n) for (ll i = m; i <= n; i++)
const ll INF = LLONG_MAX;
const ull MOD = 1000000007;
#define VEC(type, A, N) vector<type> A(N)
#define VECi(type, A, N, i) vector<type> A(N, i)
#define VEC2(type, A, N, M) vector<vector<type>> A(N, vector<type>(M))
#define VEC2i(type, A, N, M, i) vector<vector<type>> A(N, vector<type>(M, i))
#define ALL(v) v.begin(), v.end()
ll frac(ll k) {
  int s = 1;
  for (ll i = 1; i <= k; ++i)
    s *= i;
  return s;
}
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll antidiv(ll N, ll C, ll D) {
  return N - floor(N / C) - floor(N / D) + floor(N / lcm(C, D));
}
vector<ll> yakusuu(ll N) {
  vector<ll> ret;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      ret.push_back(i);
      if (i * i != N)
        ret.push_back(N / i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}
ll digitPoor(ll n) {
  assert(INT_MAX == 2147483647);
  assert(n >= 0);

  if (n < 10)
    return 1;
  if (n < 100)
    return 2;
  if (n < 1000)
    return 3;
  if (n < 10000)
    return 4;
  if (n < 100000)
    return 5;
  if (n < 1000000)
    return 6;
  if (n < 10000000)
    return 7;
  if (n < 100000000)
    return 8;
  if (n < 1000000000)
    return 9;
  return 10;
}
struct p {
  ll x;
  bool end;
};

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

  VEC(ll, A, N);
  REP(i, N) { cin >> A[i]; }

  REP(i, K) {
    bool allN = true;
    VEC(p, ps, 0);
    REP(j, N) {
      ll t = j - A[j] < 0 ? 0 : j - A[j];
      ll tt = j + A[j] > N - 1 ? N - 1 : j + A[j];
      ps.push_back({t, false});
      ps.push_back({tt, true});
    }
    sort(ALL(ps), [](p a, p b) {
      if (a.x > b.x) {
        return 0;
      } else if (a.x < b.x) {
        return 1;
      }
      return 0;
    });
    ll cu = 0, nowcu = 0, bef = 0;
    REP(j, N * 2) {
      p pp = ps[j];
      if (pp.x == bef + 1) {
        // cout<<nowcu<<" ";
        // sl(pp.x-1,"=",nowcu)
        A[pp.x - 1] = nowcu;
        if (A[pp.x - 1] != N)
          allN = false;
        nowcu = cu;
      } else if (pp.x > bef + 1) {
        ll tttt = pp.x - bef;
        // sl(pp.x-tttt,"=",nowcu)
        A[pp.x - tttt] = nowcu;
        // cout<<nowcu<<" ";
        while (--tttt > 0) {
          // cout<<cu<<" ";
          // sl(pp.x-tttt,"==",cu)
          A[pp.x - tttt] = cu;
          if (A[pp.x - tttt] != N)
            allN = false;
        }
        nowcu = cu;
      }
      if (pp.end == false) {
        cu++;
        nowcu++;
      } else {
        cu--;
      }
      bef = pp.x;
      // sl("nowcu=",nowcu,"cu=",cu)
    }
    ll tttt = N - bef;
    while (--tttt >= 0) {
      // cout<<nowcu<<" ";
      // sl(N-tttt-1,nowcu)
      A[N - tttt - 1] = nowcu;
      if (A[N - tttt - 1] != N)
        allN = false;
    }
    if (allN)
      break;
  }
  REP(i, N) { cout << A[i] << " "; }

  // cout<<fixed<<setprecision(32)<<m;
  // sort(ALL(C),[](auto& a, auto& b) {return a[2] < b[2];});
  // if(equal(D.begin(), D.end()-1, K.begin()))cout<<"Yes";else cout<<"No";
  cout << endl;
  return 0;
}