Problem p04052 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 500005, maxe = maxn << 1;
int n, K, p[maxn], q[maxn], ans[maxn];
int e, lnk[maxn], nxt[maxe], son[maxe], num[maxn];
inline char nc() {
  static char buf[100000], *p1, *p2;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int ret = 0;
  bool f = 0;
  char ch = nc();
  while (ch > '9' || ch < '0')
    f ^= ch == '-', ch = nc();
  while (ch <= '9' && ch >= '0')
    ret = ret * 10 + ch - '0', ch = nc();
  return f ? -ret : ret;
}
inline void add_e(int x, int y) {
  son[++e] = y;
  nxt[e] = lnk[x];
  lnk[x] = e;
  num[y]++;
}
struct ST {
#define mid (L + R >> 1)
#define ls (rt << 1)
#define rs (ls | 1)
  int sum[maxn];
  inline void add(int L, int R, int rt, int x, int y) {
    sum[rt] += y;
    if (L == R)
      return;
    if (x <= mid)
      add(L, mid, ls, x, y);
    else
      add(mid + 1, R, rs, x, y);
  }
  inline int getl(int L, int R, int rt, int x) {
    if (L > x || !sum[rt])
      return 0;
    if (L == R)
      return L;
    int now = getl(mid + 1, R, rs, x);
    return now ? now : getl(L, mid, ls, x);
  }
  inline int getr(int L, int R, int rt, int x) {
    if (R < x || !sum[rt])
      return 0;
    if (L == R)
      return L;
    int now = getr(L, mid, ls, x);
    return now ? now : getr(mid + 1, R, rs, x);
  }
} T;
inline void swap(int &x, int &y) {
  int t = x;
  x = y;
  y = t;
}
struct HP {
  int h[maxn], len;
  inline void add(int x) {
    h[++len] = x;
    for (int i = len; i > 1; i >>= 1)
      if (h[i] > h[i >> 1])
        swap(h[i], h[i >> 1]);
      else
        break;
  }
  inline int get() {
    int ret = h[1];
    h[1] = h[len--];
    for (int i = 1, j; (i << 1) <= len; i = j) {
      j = (i << 1) == len || h[(i << 1)] > h[(i << 1 | 1)] ? (i << 1)
                                                           : (i << 1 | 1);
      if (h[j] > h[i])
        swap(h[i], h[j]);
      else
        break;
    }
    return ret;
  }
} S;
int main() {
  n = read(), K = read();
  for (int i = 1; i <= n; i++)
    p[i] = read(), q[p[i]] = i;
  for (int i = 1; i <= n; i++) {
    if (i - K > 0)
      T.add(1, n, 1, p[i - K], -1);
    int l = T.getl(1, n, 1, p[i]), r = T.getr(1, n, 1, p[i]);
    if (l)
      add_e(i, q[l]);
    if (r)
      add_e(q[r], i);
    T.add(1, n, 1, p[i], 1);
  }
  for (int i = 1; i <= n; i++)
    if (!num[i])
      S.add(i);
  for (int i = n; i; i--) {
    int x = S.get();
    ans[x] = i;
    for (int j = lnk[x]; j; j = nxt[j])
      if (!--num[son[j]])
        S.add(son[j]);
  }
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 500005, maxe = maxn << 1;
int n, K, p[maxn], q[maxn], ans[maxn];
int e, lnk[maxn], nxt[maxe], son[maxe], num[maxn];
inline char nc() {
  static char buf[100000], *p1, *p2;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int ret = 0;
  bool f = 0;
  char ch = nc();
  while (ch > '9' || ch < '0')
    f ^= ch == '-', ch = nc();
  while (ch <= '9' && ch >= '0')
    ret = ret * 10 + ch - '0', ch = nc();
  return f ? -ret : ret;
}
inline void add_e(int x, int y) {
  son[++e] = y;
  nxt[e] = lnk[x];
  lnk[x] = e;
  num[y]++;
}
struct ST {
#define mid (L + R >> 1)
#define ls (rt << 1)
#define rs (ls | 1)
  int sum[maxn << 2];
  inline void add(int L, int R, int rt, int x, int y) {
    sum[rt] += y;
    if (L == R)
      return;
    if (x <= mid)
      add(L, mid, ls, x, y);
    else
      add(mid + 1, R, rs, x, y);
  }
  inline int getl(int L, int R, int rt, int x) {
    if (L > x || !sum[rt])
      return 0;
    if (L == R)
      return L;
    int now = getl(mid + 1, R, rs, x);
    return now ? now : getl(L, mid, ls, x);
  }
  inline int getr(int L, int R, int rt, int x) {
    if (R < x || !sum[rt])
      return 0;
    if (L == R)
      return L;
    int now = getr(L, mid, ls, x);
    return now ? now : getr(mid + 1, R, rs, x);
  }
} T;
inline void swap(int &x, int &y) {
  int t = x;
  x = y;
  y = t;
}
struct HP {
  int h[maxn], len;
  inline void add(int x) {
    h[++len] = x;
    for (int i = len; i > 1; i >>= 1)
      if (h[i] > h[i >> 1])
        swap(h[i], h[i >> 1]);
      else
        break;
  }
  inline int get() {
    int ret = h[1];
    h[1] = h[len--];
    for (int i = 1, j; (i << 1) <= len; i = j) {
      j = (i << 1) == len || h[(i << 1)] > h[(i << 1 | 1)] ? (i << 1)
                                                           : (i << 1 | 1);
      if (h[j] > h[i])
        swap(h[i], h[j]);
      else
        break;
    }
    return ret;
  }
} S;
int main() {
  n = read(), K = read();
  for (int i = 1; i <= n; i++)
    p[i] = read(), q[p[i]] = i;
  for (int i = 1; i <= n; i++) {
    if (i - K > 0)
      T.add(1, n, 1, p[i - K], -1);
    int l = T.getl(1, n, 1, p[i]), r = T.getr(1, n, 1, p[i]);
    if (l)
      add_e(i, q[l]);
    if (r)
      add_e(q[r], i);
    T.add(1, n, 1, p[i], 1);
  }
  for (int i = 1; i <= n; i++)
    if (!num[i])
      S.add(i);
  for (int i = n; i; i--) {
    int x = S.get();
    ans[x] = i;
    for (int j = lnk[x]; j; j = nxt[j])
      if (!--num[son[j]])
        S.add(son[j]);
  }
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
  return 0;
}