Problem p04052 (C++)

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

const int INF = 0x3f3f3f3f;
const int MAXN = 100 + 5;

template <typename _T> void read(_T &x) {
  x = 0;
  char s = getchar();
  int f = 1;
  while (s > '9' || s < '0') {
    if (s == '-')
      f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + (s - '0'), s = getchar();
  }
  x *= f;
}

template <typename _T> void write(_T x) {
  if (x < 0) {
    putchar('-');
    x = (~x) + 1;
  }
  if (9 < x) {
    write(x / 10);
  }
  putchar(x % 10 + '0');
}

template <typename _T> _T MIN(const _T a, const _T b) { return a < b ? a : b; }

template <typename _T> _T MAX(const _T a, const _T b) { return a > b ? a : b; }

struct edge {
  int to, nxt;
} Graph[MAXN << 1];

priority_queue<int, vector<int>, greater<int>> q;

int mn[MAXN << 2];

int seq[MAXN], ans[MAXN];
int A[MAXN], P[MAXN], head[MAXN], deg[MAXN];
int N, K, cnt;

void addEdge(const int from, const int to) {
  Graph[++cnt].to = to, Graph[cnt].nxt = head[from];
  head[from] = cnt, deg[to]++;
}

void upt(const int x) { mn[x] = MIN(mn[x << 1], mn[x << 1 | 1]); }

void build(const int x, const int l, const int r) {
  if (l > r)
    return;
  mn[x] = INF;
  if (l == r)
    return;
  int mid = l + r >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
}

void update(const int x, const int l, const int r, const int p, const int v) {
  if (l == r) {
    mn[x] = v;
    return;
  }
  int mid = l + r >> 1;
  if (p <= mid)
    update(x << 1, l, mid, p, v);
  else
    update(x << 1 | 1, mid + 1, r, p, v);
  upt(x);
}

int query(const int x, const int l, const int r, const int segL,
          const int segR) {
  if (segL <= l && r <= segR)
    return mn[x];
  int mid = l + r >> 1, ret = INF;
  if (segL <= mid)
    ret = MIN(ret, query(x << 1, l, mid, segL, segR));
  if (mid < segR)
    ret = MIN(ret, query(x << 1 | 1, mid + 1, r, segL, segR));
  return ret;
}

int query(const int l, const int r) {
  return query(1, 1, N, MAX(1, l), MIN(N, r));
}

int main() {
  read(N), read(K);
  for (int i = 1; i <= N; i++)
    read(P[i]), A[P[i]] = i;
  build(1, 1, N);
  for (int i = N; i; i--) {
    int pre = query(A[i] - K + 1, A[i]), nxt = query(A[i], A[i] + K - 1);
    if (pre < INF)
      addEdge(A[i], A[pre]);
    if (nxt < INF)
      addEdge(A[i], A[nxt]);
    update(1, 1, N, A[i], i);
  }
  for (int i = 1; i <= N; i++)
    if (!deg[i])
      q.push(i);
  int u, v, tot = 0;
  while (!q.empty()) {
    u = seq[++tot] = q.top(), q.pop();
    for (int i = head[u]; i; i = Graph[i].nxt)
      if (!(--deg[v = Graph[i].to]))
        q.push(v);
  }
  for (int i = 1; i <= N; i++)
    ans[seq[i]] = i;
  for (int i = 1; i <= N; i++)
    write(ans[i]), putchar('\n');
  return 0;
}

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

const int INF = 0x3f3f3f3f;
const int MAXN = 5e5 + 5;

template <typename _T> void read(_T &x) {
  x = 0;
  char s = getchar();
  int f = 1;
  while (s > '9' || s < '0') {
    if (s == '-')
      f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + (s - '0'), s = getchar();
  }
  x *= f;
}

template <typename _T> void write(_T x) {
  if (x < 0) {
    putchar('-');
    x = (~x) + 1;
  }
  if (9 < x) {
    write(x / 10);
  }
  putchar(x % 10 + '0');
}

template <typename _T> _T MIN(const _T a, const _T b) { return a < b ? a : b; }

template <typename _T> _T MAX(const _T a, const _T b) { return a > b ? a : b; }

struct edge {
  int to, nxt;
} Graph[MAXN << 1];

priority_queue<int, vector<int>, greater<int>> q;

int mn[MAXN << 2];

int seq[MAXN], ans[MAXN];
int A[MAXN], P[MAXN], head[MAXN], deg[MAXN];
int N, K, cnt;

void addEdge(const int from, const int to) {
  Graph[++cnt].to = to, Graph[cnt].nxt = head[from];
  head[from] = cnt, deg[to]++;
}

void upt(const int x) { mn[x] = MIN(mn[x << 1], mn[x << 1 | 1]); }

void build(const int x, const int l, const int r) {
  if (l > r)
    return;
  mn[x] = INF;
  if (l == r)
    return;
  int mid = l + r >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
}

void update(const int x, const int l, const int r, const int p, const int v) {
  if (l == r) {
    mn[x] = v;
    return;
  }
  int mid = l + r >> 1;
  if (p <= mid)
    update(x << 1, l, mid, p, v);
  else
    update(x << 1 | 1, mid + 1, r, p, v);
  upt(x);
}

int query(const int x, const int l, const int r, const int segL,
          const int segR) {
  if (segL <= l && r <= segR)
    return mn[x];
  int mid = l + r >> 1, ret = INF;
  if (segL <= mid)
    ret = MIN(ret, query(x << 1, l, mid, segL, segR));
  if (mid < segR)
    ret = MIN(ret, query(x << 1 | 1, mid + 1, r, segL, segR));
  return ret;
}

int query(const int l, const int r) {
  return query(1, 1, N, MAX(1, l), MIN(N, r));
}

int main() {
  read(N), read(K);
  for (int i = 1; i <= N; i++)
    read(P[i]), A[P[i]] = i;
  build(1, 1, N);
  for (int i = N; i; i--) {
    int pre = query(A[i] - K + 1, A[i]), nxt = query(A[i], A[i] + K - 1);
    if (pre < INF)
      addEdge(A[i], A[pre]);
    if (nxt < INF)
      addEdge(A[i], A[nxt]);
    update(1, 1, N, A[i], i);
  }
  for (int i = 1; i <= N; i++)
    if (!deg[i])
      q.push(i);
  int u, v, tot = 0;
  while (!q.empty()) {
    u = seq[++tot] = q.top(), q.pop();
    for (int i = head[u]; i; i = Graph[i].nxt)
      if (!(--deg[v = Graph[i].to]))
        q.push(v);
  }
  for (int i = 1; i <= N; i++)
    ans[seq[i]] = i;
  for (int i = 1; i <= N; i++)
    write(ans[i]), putchar('\n');
  return 0;
}