Problem p04052 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double db;
#define go(u) for (int i = head[u], v = e[i].to; i; i = e[i].lst, v = e[i].to)
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define pb push_back
#define re(x) memset(x, 0, sizeof x)
inline int gi() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - 48;
    ch = getchar();
  }
  return x * f;
}
template <typename T> inline bool Max(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline bool Min(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
const int N = 1e5 + 7;
int n, K;
int pos[N], du[N], ans[N], rans[N];
vector<int> G[N];
void Add(int a, int b) {
  ++du[b];
  G[a].pb(b);
}
typedef pair<int, int> pii;
#define mp make_pair
#define Ls o << 1
#define Rs o << 1 | 1
int mx[N << 2];
void modify(int p, int l, int r, int o, int v) {
  if (l == r) {
    mx[o] = v;
    return;
  }
  int mid = l + r >> 1;
  if (p <= mid)
    modify(p, l, mid, Ls, v);
  else
    modify(p, mid + 1, r, Rs, v);
  mx[o] = max(mx[Ls], mx[Rs]);
}
int query(int L, int R, int l, int r, int o) {
  if (L > R)
    return 0;
  if (L <= l && r <= R)
    return mx[o];
  int mid = l + r >> 1;
  if (R <= mid)
    return query(L, R, l, mid, Ls);
  if (L > mid)
    return query(L, R, mid + 1, r, Rs);
  return max(query(L, R, l, mid, Ls), query(L, R, mid + 1, r, Rs));
}
priority_queue<int> Q;
int main() {
#ifndef ONLINE_JUDGE
//	freopen("data.in", "r", stdin);
#endif
  n = gi(), K = gi() - 1;
  rep(i, 1, n) { pos[gi()] = i; }
  rep(i, 1, n) {
    int x = query(max(1, pos[i] - K), pos[i], 1, n, 1);
    if (x)
      Add(pos[i], pos[x]);
    x = query(pos[i], min(n, pos[i] + K), 1, n, 1);
    if (x)
      Add(pos[i], pos[x]);
    modify(pos[i], 1, n, 1, i);
  }
  rep(i, 1, n) if (!du[i]) Q.push(i);
  int now = n;
  while (!Q.empty()) {
    int u = Q.top();
    Q.pop();
    for (auto v : G[u])
      if (--du[v] == 0)
        Q.push(v);
    ans[now--] = u;
  }
  rep(i, 1, n) rans[ans[i]] = i;
  rep(i, 1, n) printf("%d\n", rans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double db;
#define go(u) for (int i = head[u], v = e[i].to; i; i = e[i].lst, v = e[i].to)
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define pb push_back
#define re(x) memset(x, 0, sizeof x)
inline int gi() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - 48;
    ch = getchar();
  }
  return x * f;
}
template <typename T> inline bool Max(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline bool Min(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
const int N = 5e5 + 7;
int n, K;
int pos[N], du[N], ans[N], rans[N];
vector<int> G[N];
void Add(int a, int b) {
  ++du[b];
  G[a].pb(b);
}
typedef pair<int, int> pii;
#define mp make_pair
#define Ls o << 1
#define Rs o << 1 | 1
int mx[N << 2];
void modify(int p, int l, int r, int o, int v) {
  if (l == r) {
    mx[o] = v;
    return;
  }
  int mid = l + r >> 1;
  if (p <= mid)
    modify(p, l, mid, Ls, v);
  else
    modify(p, mid + 1, r, Rs, v);
  mx[o] = max(mx[Ls], mx[Rs]);
}
int query(int L, int R, int l, int r, int o) {
  if (L > R)
    return 0;
  if (L <= l && r <= R)
    return mx[o];
  int mid = l + r >> 1;
  if (R <= mid)
    return query(L, R, l, mid, Ls);
  if (L > mid)
    return query(L, R, mid + 1, r, Rs);
  return max(query(L, R, l, mid, Ls), query(L, R, mid + 1, r, Rs));
}
priority_queue<int> Q;
int main() {
#ifndef ONLINE_JUDGE
//	freopen("data.in", "r", stdin);
#endif
  n = gi(), K = gi() - 1;
  rep(i, 1, n) { pos[gi()] = i; }
  rep(i, 1, n) {
    int x = query(max(1, pos[i] - K), pos[i], 1, n, 1);
    if (x)
      Add(pos[i], pos[x]);
    x = query(pos[i], min(n, pos[i] + K), 1, n, 1);
    if (x)
      Add(pos[i], pos[x]);
    modify(pos[i], 1, n, 1, i);
  }
  rep(i, 1, n) if (!du[i]) Q.push(i);
  int now = n;
  while (!Q.empty()) {
    int u = Q.top();
    Q.pop();
    for (auto v : G[u])
      if (--du[v] == 0)
        Q.push(v);
    ans[now--] = u;
  }
  rep(i, 1, n) rans[ans[i]] = i;
  rep(i, 1, n) printf("%d\n", rans[i]);
  return 0;
}