Problem p04052 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define segc int M = (L + R - 1) >> 1, lc = id << 1, rc = lc | 1

const int N = 100054, M = N * 2;

int n, K, E = 0;
int a[N], b[N];
int to[M], first[N], next[M], deg[N];
int x[N * 4];
std::priority_queue<int> pq;

inline void up(int &x, const int y) { x < y ? x = y : 0; }
inline int min(const int x, const int y) { return x < y ? x : y; }
inline int max(const int x, const int y) { return x < y ? y : x; }
inline void addedge(int u, int v) {
  to[++E] = v, next[E] = first[u], first[u] = E, ++deg[v];
}

void adj(int id, int L, int R, int h, int v) {
  if (L == R) {
    x[id] = v;
    return;
  }
  segc;
  h <= M ? adj(lc, L, M, h, v) : adj(rc, M + 1, R, h, v);
  x[id] = max(x[lc], x[rc]);
}

int range(int id, int L, int R, int ql, int qr) {
  if (ql > qr)
    return 0;
  if (ql <= L && R <= qr)
    return x[id];
  segc, s = 0;
  if (ql <= M)
    up(s, range(lc, L, M, ql, qr));
  if (qr > M)
    up(s, range(rc, M + 1, R, ql, qr));
  return s;
}

int main() {
  int i, j, x, y;
  scanf("%d%d", &n, &K), --K;
  for (i = 1; i <= n; ++i)
    scanf("%d", a + i), b[a[i]] = i;
  for (i = 1; i <= n; ++i) {
    if (x = range(1, 1, n, max(b[i] - K, 1), b[i] - 1))
      addedge(b[i], b[x]);
    if (x = range(1, 1, n, b[i] + 1, min(b[i] + K, n)))
      addedge(b[i], b[x]);
    adj(1, 1, n, b[i], i);
  }
  for (i = 1; i <= n; ++i)
    if (!deg[i])
      pq.emplace(i);
  for (i = n; i; --i) {
    b[i] = x = pq.top(), pq.pop();
    for (j = first[x]; j; j = next[j])
      if (!--deg[y = to[j]])
        pq.emplace(y);
  }
  for (i = 1; i <= n; ++i)
    a[b[i]] = i;
  for (i = 1; i <= n; ++i)
    printf("%d\n", a[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define segc int M = (L + R - 1) >> 1, lc = id << 1, rc = lc | 1

const int N = 500054, M = N * 2;

int n, K, E = 0;
int a[N], b[N];
int to[M], first[N], next[M], deg[N];
int x[N * 4];
std::priority_queue<int> pq;

inline void up(int &x, const int y) { x < y ? x = y : 0; }
inline int min(const int x, const int y) { return x < y ? x : y; }
inline int max(const int x, const int y) { return x < y ? y : x; }
inline void addedge(int u, int v) {
  to[++E] = v, next[E] = first[u], first[u] = E, ++deg[v];
}

void adj(int id, int L, int R, int h, int v) {
  if (L == R) {
    x[id] = v;
    return;
  }
  segc;
  h <= M ? adj(lc, L, M, h, v) : adj(rc, M + 1, R, h, v);
  x[id] = max(x[lc], x[rc]);
}

int range(int id, int L, int R, int ql, int qr) {
  if (ql > qr)
    return 0;
  if (ql <= L && R <= qr)
    return x[id];
  segc, s = 0;
  if (ql <= M)
    up(s, range(lc, L, M, ql, qr));
  if (qr > M)
    up(s, range(rc, M + 1, R, ql, qr));
  return s;
}

int main() {
  int i, j, x, y;
  scanf("%d%d", &n, &K), --K;
  for (i = 1; i <= n; ++i)
    scanf("%d", a + i), b[a[i]] = i;
  for (i = 1; i <= n; ++i) {
    if (x = range(1, 1, n, max(b[i] - K, 1), b[i] - 1))
      addedge(b[i], b[x]);
    if (x = range(1, 1, n, b[i] + 1, min(b[i] + K, n)))
      addedge(b[i], b[x]);
    adj(1, 1, n, b[i], i);
  }
  for (i = 1; i <= n; ++i)
    if (!deg[i])
      pq.emplace(i);
  for (i = n; i; --i) {
    b[i] = x = pq.top(), pq.pop();
    for (j = first[x]; j; j = next[j])
      if (!--deg[y = to[j]])
        pq.emplace(y);
  }
  for (i = 1; i <= n; ++i)
    a[b[i]] = i;
  for (i = 1; i <= n; ++i)
    printf("%d\n", a[i]);
  return 0;
}