Problem p04052 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IL inline
#define rep(i, j, k) for (int i = j; i <= k; ++i)
#define repd(i, j, k) for (int i = j; i >= k; --i)
#define pb push_back
#define db double
#define mp make_pair
#define mp3(a, b, c) mp(mp(a, b), c)
#define pii pair<int, int>
#define piii pair<pii, int>
#define fr first
#define se second
#define ll long long
#define ull unsigned long long
#define pbc(x) __builtin_popcount(x)
#define clr(x) memset(x, 0, sizeof x)
#define SIZE(x) (int)(x.size())
const int mod = 1e9 + 7;
IL int pls(int x, int y) {
  x += y;
  return x >= mod ? x - mod : x;
}
IL int dec(int x, int y) {
  x -= y;
  return x < 0 ? x + mod : x;
}
IL int mul(int x, int y) { return 1ll * x * y % mod; }
IL int mul(int x, int y, int z) { return mul(mul(x, y), z); }
IL int mul(int x, int y, int z, int p) { return mul(mul(x, y), mul(z, p)); }
IL void add(int &x, int y) {
  x += y;
  x = (x >= mod) ? x - mod : x;
}
IL int fpw(int x, int y, int r = 1) {
  for (; y; y >>= 1, x = mul(x, x))
    if (y & 1)
      r = mul(r, x);
  return r;
}
IL int inv(int x) { return fpw(x, mod - 2); }
IL int gi() {
  int x;
  int _w = scanf("%d", &x);
  return x;
}
IL void gi(int &x) { int _w = scanf("%d", &x); }
IL void write(int x) { printf("%d\n", x); }
void chkmax(int &x, int y) { x = (x > y) ? x : y; }
void chkmin(int &x, int y) { x = (x < y) ? x : y; }
const int INF = 0x3f3f3f3f;
template <typename T> IL void debug(T x) {
  cerr << x;
  return;
}
/* ---------------------------------------------------------------------------------------------------------
 */
int n, k;
const int maxn = 1e5 + 10;
int a[maxn], p[maxn], deg[maxn], b[maxn];
vector<int> e[maxn];
int mn[maxn << 2];
void add(int p, int x, int y, int pos, int d) {
  if (x == y) {
    mn[p] = d;
    return;
  }
  int mid = (x + y) >> 1;
  if (pos <= mid)
    add(p << 1, x, mid, pos, d);
  else
    add(p << 1 | 1, mid + 1, y, pos, d);
  mn[p] = min(mn[p << 1], mn[p << 1 | 1]);
}
int qry(int p, int x, int y, int l, int r) {
  if (x == l && y == r)
    return mn[p];
  int mid = (x + y) >> 1;
  if (r <= mid)
    return qry(p << 1, x, mid, l, r);
  else if (l > mid)
    return qry(p << 1 | 1, mid + 1, y, l, r);
  else
    return min(qry(p << 1, x, mid, l, mid),
               qry(p << 1 | 1, mid + 1, y, mid + 1, r));
}
int main() {
#ifdef LOCAL
  freopen("/home/noilinux/Desktop/input.txt", "r", stdin);
#endif
  memset(mn, 0x3f, sizeof mn);
  n = gi();
  k = gi();
  rep(i, 1, n) p[i] = gi(), a[p[i]] = i;
  repd(i, n, 1) {
    int pos;
    pos = qry(1, 1, n, a[i], min(a[i] + k - 1, n));
    if (pos != INF)
      e[i].pb(pos), deg[pos]++;
    pos = qry(1, 1, n, max(1, a[i] - k + 1), a[i]);
    if (pos != INF)
      e[i].pb(pos), deg[pos]++;
    add(1, 1, n, a[i], i);
  }
  priority_queue<pii, vector<pii>, greater<pii>> q;
  for (int i = 1; i <= n; ++i)
    if (!deg[i])
      q.push({a[i], i});
  int cnt = 0;
  while (q.size()) {
    int t = q.top().se;
    q.pop();
    b[++cnt] = a[t];
    for (auto v : e[t]) {
      deg[v]--;
      if (!deg[v])
        q.push({a[v], v});
    }
  }
  rep(i, 1, n) p[b[i]] = i;
  rep(i, 1, n) printf("%d\n", p[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IL inline
#define rep(i, j, k) for (int i = j; i <= k; ++i)
#define repd(i, j, k) for (int i = j; i >= k; --i)
#define pb push_back
#define db double
#define mp make_pair
#define mp3(a, b, c) mp(mp(a, b), c)
#define pii pair<int, int>
#define piii pair<pii, int>
#define fr first
#define se second
#define ll long long
#define ull unsigned long long
#define pbc(x) __builtin_popcount(x)
#define clr(x) memset(x, 0, sizeof x)
#define SIZE(x) (int)(x.size())
const int mod = 1e9 + 7;
IL int pls(int x, int y) {
  x += y;
  return x >= mod ? x - mod : x;
}
IL int dec(int x, int y) {
  x -= y;
  return x < 0 ? x + mod : x;
}
IL int mul(int x, int y) { return 1ll * x * y % mod; }
IL int mul(int x, int y, int z) { return mul(mul(x, y), z); }
IL int mul(int x, int y, int z, int p) { return mul(mul(x, y), mul(z, p)); }
IL void add(int &x, int y) {
  x += y;
  x = (x >= mod) ? x - mod : x;
}
IL int fpw(int x, int y, int r = 1) {
  for (; y; y >>= 1, x = mul(x, x))
    if (y & 1)
      r = mul(r, x);
  return r;
}
IL int inv(int x) { return fpw(x, mod - 2); }
IL int gi() {
  int x;
  int _w = scanf("%d", &x);
  return x;
}
IL void gi(int &x) { int _w = scanf("%d", &x); }
IL void write(int x) { printf("%d\n", x); }
void chkmax(int &x, int y) { x = (x > y) ? x : y; }
void chkmin(int &x, int y) { x = (x < y) ? x : y; }
const int INF = 0x3f3f3f3f;
template <typename T> IL void debug(T x) {
  cerr << x;
  return;
}
/* ---------------------------------------------------------------------------------------------------------
 */
int n, k;
const int maxn = 5e5 + 10;
int a[maxn], p[maxn], deg[maxn], b[maxn];
vector<int> e[maxn];
int mn[maxn << 2];
void add(int p, int x, int y, int pos, int d) {
  if (x == y) {
    mn[p] = d;
    return;
  }
  int mid = (x + y) >> 1;
  if (pos <= mid)
    add(p << 1, x, mid, pos, d);
  else
    add(p << 1 | 1, mid + 1, y, pos, d);
  mn[p] = min(mn[p << 1], mn[p << 1 | 1]);
}
int qry(int p, int x, int y, int l, int r) {
  if (x == l && y == r)
    return mn[p];
  int mid = (x + y) >> 1;
  if (r <= mid)
    return qry(p << 1, x, mid, l, r);
  else if (l > mid)
    return qry(p << 1 | 1, mid + 1, y, l, r);
  else
    return min(qry(p << 1, x, mid, l, mid),
               qry(p << 1 | 1, mid + 1, y, mid + 1, r));
}
int main() {
#ifdef LOCAL
  freopen("/home/noilinux/Desktop/input.txt", "r", stdin);
#endif
  memset(mn, 0x3f, sizeof mn);
  n = gi();
  k = gi();
  rep(i, 1, n) p[i] = gi(), a[p[i]] = i;
  repd(i, n, 1) {
    int pos;
    pos = qry(1, 1, n, a[i], min(a[i] + k - 1, n));
    if (pos != INF)
      e[i].pb(pos), deg[pos]++;
    pos = qry(1, 1, n, max(1, a[i] - k + 1), a[i]);
    if (pos != INF)
      e[i].pb(pos), deg[pos]++;
    add(1, 1, n, a[i], i);
  }
  priority_queue<pii, vector<pii>, greater<pii>> q;
  for (int i = 1; i <= n; ++i)
    if (!deg[i])
      q.push({a[i], i});
  int cnt = 0;
  while (q.size()) {
    int t = q.top().se;
    q.pop();
    b[++cnt] = a[t];
    for (auto v : e[t]) {
      deg[v]--;
      if (!deg[v])
        q.push({a[v], v});
    }
  }
  rep(i, 1, n) p[b[i]] = i;
  rep(i, 1, n) printf("%d\n", p[i]);
  return 0;
}