Problem p04052 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

const int N = 5e5 + 10;
const int inf = 0x3f3f3f3f;
struct E {
  int v, nt;
} e[N << 1];
int n, K, tot, h[N], q[N], p[N], deg[N], seg[N];
priority_queue<int, vector<int>, greater<int>> que;

inline int max(int a, int b) { return a > b ? a : b; }
inline int min(int a, int b) { return a < b ? a : b; }
inline void add(int u, int v) { e[++tot] = (E){v, h[u]}, h[u] = tot, ++deg[v]; }
template <class T> inline void in(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9';) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  for (; ch >= '0' && ch <= '9';)
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}

inline void build(int o, int l, int r) {
  seg[o] = inf;
  if (l == r)
    return;
  int mid = l + r >> 1, lc = o << 1, rc = lc | 1;
  build(lc, l, mid), build(rc, mid + 1, r);
}

inline void update(int o, int l, int r, int q, int v) {
  if (l == r) {
    seg[o] = v;
    return;
  }
  int mid = l + r >> 1, lc = o << 1, rc = lc | 1;
  if (q <= mid)
    update(lc, l, mid, q, v);
  else
    update(rc, mid + 1, r, q, v);
  seg[o] = min(seg[lc], seg[rc]);
}

inline int query(int o, int l, int r, int L, int R) {
  if (L <= l && r <= R)
    return seg[o];
  int mid = l + r >> 1, lc = o << 1, rc = lc | 1, ret = inf;
  if (L <= mid)
    ret = min(ret, query(lc, l, mid, L, R));
  if (R > mid)
    ret = min(ret, query(rc, mid + 1, r, L, R));
  return ret;
}

int main() {
  int x, v;
  in(n), in(K), build(1, 1, n);
  for (int i = 1; i <= n; ++i)
    in(p[i]), q[p[i]] = i;
  for (int i = n; i; --i) {
    x = query(1, 1, n, max(1, q[i] - K + 1), q[i]);
    if (x <= n)
      add(q[i], q[x]);
    x = query(1, 1, n, q[i], min(q[i] + K - 1, n));
    if (x <= n)
      add(q[i], q[x]);
    update(1, 1, n, q[i], i);
  }
  for (int i = 1; i <= n; ++i)
    if (!deg[i])
      que.push(i);
  for (int l = 1; l <= n; ++l) {
    x = q[l] = que.top(), que.pop();
    for (int i = h[x]; i; i = e[i].nt)
      if (!(--deg[v = e[i].v]))
        que.push(v);
  }
  for (int i = 1; i <= n; ++i)
    p[q[i]] = i;
  for (int i = 1; i <= n; ++i)
    printf("%d\n", p[i]);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

const int N = 5e5 + 10;
const int inf = 0x3f3f3f3f;
struct E {
  int v, nt;
} e[N << 1];
int n, K, tot, h[N], q[N], p[N], deg[N], seg[N << 2];
priority_queue<int, vector<int>, greater<int>> que;

inline int max(int a, int b) { return a > b ? a : b; }
inline int min(int a, int b) { return a < b ? a : b; }
inline void add(int u, int v) { e[++tot] = (E){v, h[u]}, h[u] = tot, ++deg[v]; }
template <class T> inline void in(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9';) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  for (; ch >= '0' && ch <= '9';)
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}

inline void build(int o, int l, int r) {
  seg[o] = inf;
  if (l == r)
    return;
  int mid = l + r >> 1, lc = o << 1, rc = lc | 1;
  build(lc, l, mid), build(rc, mid + 1, r);
}

inline void update(int o, int l, int r, int q, int v) {
  if (l == r) {
    seg[o] = v;
    return;
  }
  int mid = l + r >> 1, lc = o << 1, rc = lc | 1;
  if (q <= mid)
    update(lc, l, mid, q, v);
  else
    update(rc, mid + 1, r, q, v);
  seg[o] = min(seg[lc], seg[rc]);
}

inline int query(int o, int l, int r, int L, int R) {
  if (L <= l && r <= R)
    return seg[o];
  int mid = l + r >> 1, lc = o << 1, rc = lc | 1, ret = inf;
  if (L <= mid)
    ret = min(ret, query(lc, l, mid, L, R));
  if (R > mid)
    ret = min(ret, query(rc, mid + 1, r, L, R));
  return ret;
}

int main() {
  int x, v;
  in(n), in(K), build(1, 1, n);
  for (int i = 1; i <= n; ++i)
    in(p[i]), q[p[i]] = i;
  for (int i = n; i; --i) {
    x = query(1, 1, n, max(1, q[i] - K + 1), q[i]);
    if (x <= n)
      add(q[i], q[x]);
    x = query(1, 1, n, q[i], min(q[i] + K - 1, n));
    if (x <= n)
      add(q[i], q[x]);
    update(1, 1, n, q[i], i);
  }
  for (int i = 1; i <= n; ++i)
    if (!deg[i])
      que.push(i);
  for (int l = 1; l <= n; ++l) {
    x = q[l] = que.top(), que.pop();
    for (int i = h[x]; i; i = e[i].nt)
      if (!(--deg[v = e[i].v]))
        que.push(v);
  }
  for (int i = 1; i <= n; ++i)
    p[q[i]] = i;
  for (int i = 1; i <= n; ++i)
    printf("%d\n", p[i]);
  return 0;
}