Problem p04052 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define Inf 0x3f3f3f3f
#define rg register
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
using namespace std;
typedef long long ll;
inline ll min(ll x, ll y) { return x < y ? x : y; }
inline ll max(ll x, ll y) { return x > y ? x : y; }
const int Maxn = 5e5 + 5;
inline int read() {
  rg int res = 0, sign = 1;
  rg char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      sign = -1;
    ch = getchar();
  }
  do {
    res = (res << 3) + (res << 1) + (ch ^ 48);
    ch = getchar();
  } while (isdigit(ch));
  return res * sign;
}
int n, k, cnt;
int p[Maxn], pos[Maxn], fir[Maxn], ind[Maxn], ans[Maxn], tree[Maxn << 2];
struct {
  int to, last;
} e[2 * Maxn];
void con(int u, int v) {
  e[++cnt].to = v;
  e[cnt].last = fir[u];
  fir[u] = cnt;
  ind[v]++;
}
void Insert(rg int x, rg int l, rg int r, rg int pos, rg int s) {
  if (l == r) {
    tree[x] = s;
    return;
  }
  rg int mid = l + r >> 1;
  if (pos <= mid)
    Insert(lson, pos, s);
  else
    Insert(rson, pos, s);
  tree[x] = min(tree[x << 1], tree[x << 1 | 1]);
}
int Query(rg int x, rg int l, rg int r, rg int nl, rg int nr) {
  if (nl > nr)
    return Inf;
  if (nl <= l && r <= nr)
    return tree[x];
  rg int mid = l + r >> 1, res = Inf;
  if (nl <= mid)
    res = Query(lson, nl, nr);
  if (nr > mid)
    res = min(res, Query(rson, nl, nr));
  return res;
}
priority_queue<int, vector<int>, greater<int>> q;
void topo_sort() {
  rg int x;
  for (rg int i = 1; i <= n; i++) {
    x = q.top();
    q.pop();
    pos[i] = x;
    for (rg int next = fir[x], nx; next; next = e[next].last) {
      nx = e[next].to;
      ind[nx]--;
      if (!ind[nx])
        q.push(nx);
    }
  }
}
int main() {
  memset(tree, 0x3f, sizeof(tree));
  rg int a;
  freopen("permutation.in", "r", stdin);
  freopen("permutation.out", "w", stdout);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    a = read();
    p[a] = i;
  }
  Insert(1, 1, n, p[n], n);
  for (rg int i = n - 1; i >= 1; i--) {
    a = Query(1, 1, n, max(1, p[i] - k + 1), p[i] - 1);
    if (a != Inf)
      con(p[i], p[a]);
    a = Query(1, 1, n, p[i] + 1, min(p[i] + k - 1, n));
    if (a != Inf)
      con(p[i], p[a]);
    Insert(1, 1, n, p[i], i);
  }
  for (int i = 1; i <= n; i++)
    if (!ind[p[i]])
      q.push(p[i]);
  topo_sort();
  for (int i = 1; i <= n; i++)
    ans[pos[i]] = i;
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define Inf 0x3f3f3f3f
#define rg register
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
using namespace std;
typedef long long ll;
inline ll min(ll x, ll y) { return x < y ? x : y; }
inline ll max(ll x, ll y) { return x > y ? x : y; }
const int Maxn = 5e5 + 5;
inline int read() {
  rg int res = 0, sign = 1;
  rg char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      sign = -1;
    ch = getchar();
  }
  do {
    res = (res << 3) + (res << 1) + (ch ^ 48);
    ch = getchar();
  } while (isdigit(ch));
  return res * sign;
}
int n, k, cnt;
int p[Maxn], pos[Maxn], fir[Maxn], ind[Maxn], ans[Maxn], tree[Maxn << 2];
struct {
  int to, last;
} e[2 * Maxn];
void con(int u, int v) {
  e[++cnt].to = v;
  e[cnt].last = fir[u];
  fir[u] = cnt;
  ind[v]++;
}
void Insert(rg int x, rg int l, rg int r, rg int pos, rg int s) {
  if (l == r) {
    tree[x] = s;
    return;
  }
  rg int mid = l + r >> 1;
  if (pos <= mid)
    Insert(lson, pos, s);
  else
    Insert(rson, pos, s);
  tree[x] = min(tree[x << 1], tree[x << 1 | 1]);
}
int Query(rg int x, rg int l, rg int r, rg int nl, rg int nr) {
  if (nl > nr)
    return Inf;
  if (nl <= l && r <= nr)
    return tree[x];
  rg int mid = l + r >> 1, res = Inf;
  if (nl <= mid)
    res = Query(lson, nl, nr);
  if (nr > mid)
    res = min(res, Query(rson, nl, nr));
  return res;
}
priority_queue<int, vector<int>, greater<int>> q;
void topo_sort() {
  rg int x;
  for (rg int i = 1; i <= n; i++) {
    x = q.top();
    q.pop();
    pos[i] = x;
    for (rg int next = fir[x], nx; next; next = e[next].last) {
      nx = e[next].to;
      ind[nx]--;
      if (!ind[nx])
        q.push(nx);
    }
  }
}
int main() {
  memset(tree, 0x3f, sizeof(tree));
  rg int a;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    a = read();
    p[a] = i;
  }
  Insert(1, 1, n, p[n], n);
  for (rg int i = n - 1; i >= 1; i--) {
    a = Query(1, 1, n, max(1, p[i] - k + 1), p[i] - 1);
    if (a != Inf)
      con(p[i], p[a]);
    a = Query(1, 1, n, p[i] + 1, min(p[i] + k - 1, n));
    if (a != Inf)
      con(p[i], p[a]);
    Insert(1, 1, n, p[i], i);
  }
  for (int i = 1; i <= n; i++)
    if (!ind[p[i]])
      q.push(p[i]);
  topo_sort();
  for (int i = 1; i <= n; i++)
    ans[pos[i]] = i;
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
  return 0;
}