Problem p04052 (C++)

Buggy submission (status: Runtime Error):
// Written by newbiechd
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

// Delete the debugging information!
#define debug(x) std::cerr << #x << " = " << x << std::endl;

const int BUFFER_SIZE = 1 << 25 | 1;
struct InputOutputStream {
  char ibuf[BUFFER_SIZE], obuf[BUFFER_SIZE], *s, *oh;
  InputOutputStream() : s(ibuf), oh(obuf) {
#ifndef ONLINE_JUDGE
    freopen("a.in", "r", stdin);
    // freopen("a.out", "w", stdout);
#endif
    ibuf[fread(ibuf, 1, BUFFER_SIZE, stdin)] = '\0';
  }
  ~InputOutputStream() { fwrite(obuf, 1, oh - obuf, stdout); }
  template <typename T> inline InputOutputStream &operator>>(T &x) {
    while (!isdigit(*s))
      ++s;
    for (x = 0; isdigit(*s); ++s)
      x = x * 10 + (*s ^ '0');
    return *this;
  }
  template <typename T> inline InputOutputStream &operator<<(T x) {
    static char buf[23];
    register char *top = buf;
    if (x) {
      for (T t; x;)
        t = x / 10, *top++ = x - t * 10 + 48, x = t;
      while (top != buf)
        *oh++ = *--top;
    } else
      *oh++ = '0';
    *oh++ = '\n';
    return *this;
  }
} IO;

template <typename lhs, typename rhs>
inline lhs min(const lhs &x, const rhs &y) {
  return x < y ? x : y;
}
template <typename lhs, typename rhs>
inline lhs max(const lhs &x, const rhs &y) {
  return x > y ? x : y;
}

const int MAXN = 500003;
int n, k, p[MAXN], q[MAXN];

const int MAXT = 2000003;
#define mid ((l + r) >> 1)
#define lSon (k << 1)
#define rSon (k << 1 | 1)
struct SegmentTree {
  int mx[MAXT];
  void modify(int k, int l, int r, int x, int v) {
    mx[k] = max(mx[k], v);
    if (l == r)
      return;
    x <= mid ? modify(lSon, l, mid, x, v) : modify(rSon, mid + 1, r, x, v);
  }
  int query(int k, int l, int r, int x, int y) {
    if (x <= l && r <= y)
      return mx[k];
    int o = 0;
    if (x <= mid)
      o = max(o, query(lSon, l, mid, x, y));
    if (mid < y)
      o = max(o, query(rSon, mid + 1, r, x, y));
    return o;
  }
} tree;
#undef mid
#undef lSon
#undef rSon

std::vector<int> g[MAXN];
int deg[MAXN];

std::priority_queue<int> que;

int main() {
  IO >> n >> k, --k; // k = k - 1
  for (int i = 1; i <= n; ++i)
    IO >> p[i], q[p[i]] = i;
  for (int i = 1; i <= n; ++i) {
    int temp = tree.query(1, 1, n, q[i] - k, q[i]);
    if (temp)
      g[q[i]].push_back(q[temp]), ++deg[q[temp]];
    temp = tree.query(1, 1, n, q[i], q[i] + k);
    if (temp)
      g[q[i]].push_back(q[temp]), ++deg[q[temp]];
    tree.modify(1, 1, n, q[i], i);
  }

  static int ans[MAXN], cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (!deg[i])
      que.push(i);
  while (!que.empty()) {
    int x = que.top();
    que.pop(), ans[++cnt] = x;
    for (int y : g[x])
      if (--deg[y] == 0)
        que.push(y);
  }
  std::reverse(ans + 1, ans + n + 1);
  for (int i = 1; i <= n; ++i)
    p[ans[i]] = i;
  for (int i = 1; i <= n; ++i)
    IO << p[i];
  return 0;
}


Fixed submission:
// Written by newbiechd
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

// Delete the debugging information!
#define debug(x) std::cerr << #x << " = " << x << std::endl;

const int BUFFER_SIZE = 1 << 25 | 1;
struct InputOutputStream {
  char ibuf[BUFFER_SIZE], obuf[BUFFER_SIZE], *s, *oh;
  InputOutputStream() : s(ibuf), oh(obuf) {
    ibuf[fread(ibuf, 1, BUFFER_SIZE, stdin)] = '\0';
  }
  ~InputOutputStream() { fwrite(obuf, 1, oh - obuf, stdout); }
  template <typename T> inline InputOutputStream &operator>>(T &x) {
    while (!isdigit(*s))
      ++s;
    for (x = 0; isdigit(*s); ++s)
      x = x * 10 + (*s ^ '0');
    return *this;
  }
  template <typename T> inline InputOutputStream &operator<<(T x) {
    static char buf[23];
    register char *top = buf;
    if (x) {
      for (T t; x;)
        t = x / 10, *top++ = x - t * 10 + 48, x = t;
      while (top != buf)
        *oh++ = *--top;
    } else
      *oh++ = '0';
    *oh++ = '\n';
    return *this;
  }
} IO;

template <typename lhs, typename rhs>
inline lhs min(const lhs &x, const rhs &y) {
  return x < y ? x : y;
}
template <typename lhs, typename rhs>
inline lhs max(const lhs &x, const rhs &y) {
  return x > y ? x : y;
}

const int MAXN = 500003;
int n, k, p[MAXN], q[MAXN];

const int MAXT = 2000003;
#define mid ((l + r) >> 1)
#define lSon (k << 1)
#define rSon (k << 1 | 1)
struct SegmentTree {
  int mx[MAXT];
  void modify(int k, int l, int r, int x, int v) {
    mx[k] = max(mx[k], v);
    if (l == r)
      return;
    x <= mid ? modify(lSon, l, mid, x, v) : modify(rSon, mid + 1, r, x, v);
  }
  int query(int k, int l, int r, int x, int y) {
    if (x <= l && r <= y)
      return mx[k];
    int o = 0;
    if (x <= mid)
      o = max(o, query(lSon, l, mid, x, y));
    if (mid < y)
      o = max(o, query(rSon, mid + 1, r, x, y));
    return o;
  }
} tree;
#undef mid
#undef lSon
#undef rSon

std::vector<int> g[MAXN];
int deg[MAXN];

std::priority_queue<int> que;

int main() {
  IO >> n >> k, --k; // k = k - 1
  for (int i = 1; i <= n; ++i)
    IO >> p[i], q[p[i]] = i;
  for (int i = 1; i <= n; ++i) {
    int temp = tree.query(1, 1, n, q[i] - k, q[i]);
    if (temp)
      g[q[i]].push_back(q[temp]), ++deg[q[temp]];
    temp = tree.query(1, 1, n, q[i], q[i] + k);
    if (temp)
      g[q[i]].push_back(q[temp]), ++deg[q[temp]];
    tree.modify(1, 1, n, q[i], i);
  }

  static int ans[MAXN], cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (!deg[i])
      que.push(i);
  while (!que.empty()) {
    int x = que.top();
    que.pop(), ans[++cnt] = x;
    for (int y : g[x])
      if (--deg[y] == 0)
        que.push(y);
  }
  std::reverse(ans + 1, ans + n + 1);
  for (int i = 1; i <= n; ++i)
    p[ans[i]] = i;
  for (int i = 1; i <= n; ++i)
    IO << p[i];
  return 0;
}