Problem p04052 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define RI register int
int read() {
  int q = 0;
  char ch = ' ';
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9')
    q = q * 10 + ch - '0', ch = getchar();
  return q;
}
const int N = 500005;
int n, K, tot, js;
int tr[N << 1], a[N], h[N], ne[N], to[N], du[N], ans[N];
priority_queue<int, vector<int>, greater<int>> q;
int query(int l, int r, int s, int t, int i) {
  if (l <= s && t <= r)
    return tr[i];
  int mid = (s + t) >> 1, re = 0;
  if (l <= mid)
    re = query(l, r, s, mid, i << 1);
  if (mid + 1 <= r)
    re = max(re, query(l, r, mid + 1, t, (i << 1) | 1));
  return re;
}
void chan(int x, int s, int t, int i, int num) {
  if (s == t) {
    tr[i] = num;
    return;
  }
  int mid = (s + t) >> 1;
  if (x <= mid)
    chan(x, s, mid, i << 1, num);
  else
    chan(x, mid + 1, t, (i << 1) | 1, num);
  tr[i] = max(tr[i << 1], tr[(i << 1) | 1]);
}
void add(int x, int y) { to[++tot] = y, ne[tot] = h[x], h[x] = tot, ++du[y]; }
void topsort() {
  for (RI i = 1; i <= n; ++i)
    if (!du[i])
      q.push(i);
  while (!q.empty()) {
    int x = q.top();
    q.pop(), ans[x] = ++js;
    for (RI i = h[x]; i; i = ne[i]) {
      --du[to[i]];
      if (!du[to[i]])
        q.push(to[i]);
    }
  }
}
int main() {
  n = read(), K = read();
  for (RI i = 1; i <= n; ++i)
    a[read()] = i;
  for (RI i = 1; i <= n; ++i) {
    int k = query(max(1, a[i] - K + 1), a[i], 1, n, 1);
    if (k)
      add(a[k], a[i]);
    k = query(a[i], min(n, a[i] + K - 1), 1, n, 1);
    if (k)
      add(a[k], a[i]);
    chan(a[i], 1, n, 1, i);
  }
  topsort();
  for (RI i = 1; i <= n; ++i)
    printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define RI register int
int read() {
  int q = 0;
  char ch = ' ';
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9')
    q = q * 10 + ch - '0', ch = getchar();
  return q;
}
const int N = 500005;
int n, K, tot, js;
int tr[N << 2], a[N], h[N], ne[N << 1], to[N << 1], du[N], ans[N];
priority_queue<int, vector<int>, greater<int>> q;
int query(int l, int r, int s, int t, int i) {
  if (l <= s && t <= r)
    return tr[i];
  int mid = (s + t) >> 1, re = 0;
  if (l <= mid)
    re = query(l, r, s, mid, i << 1);
  if (mid + 1 <= r)
    re = max(re, query(l, r, mid + 1, t, (i << 1) | 1));
  return re;
}
void chan(int x, int s, int t, int i, int num) {
  if (s == t) {
    tr[i] = num;
    return;
  }
  int mid = (s + t) >> 1;
  if (x <= mid)
    chan(x, s, mid, i << 1, num);
  else
    chan(x, mid + 1, t, (i << 1) | 1, num);
  tr[i] = max(tr[i << 1], tr[(i << 1) | 1]);
}
void add(int x, int y) { to[++tot] = y, ne[tot] = h[x], h[x] = tot, ++du[y]; }
void topsort() {
  for (RI i = 1; i <= n; ++i)
    if (!du[i])
      q.push(i);
  while (!q.empty()) {
    int x = q.top();
    q.pop(), ans[x] = ++js;
    for (RI i = h[x]; i; i = ne[i]) {
      --du[to[i]];
      if (!du[to[i]])
        q.push(to[i]);
    }
  }
}
int main() {
  n = read(), K = read();
  for (RI i = 1; i <= n; ++i)
    a[read()] = i;
  for (RI i = 1; i <= n; ++i) {
    int k = query(max(1, a[i] - K + 1), a[i], 1, n, 1);
    if (k)
      add(a[k], a[i]);
    k = query(a[i], min(n, a[i] + K - 1), 1, n, 1);
    if (k)
      add(a[k], a[i]);
    chan(a[i], 1, n, 1, i);
  }
  topsort();
  for (RI i = 1; i <= n; ++i)
    printf("%d\n", ans[i]);
  return 0;
}