Problem p04052 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#define N 1048576
inline int min(int x, int y) { return x < y ? x : y; }
inline int max(int x, int y) { return x < y ? y : x; }
char G[3500000], *H = G;
inline int read() {
  int x = 0;
  while (*H < 48 || 57 < *H)
    H++;
  while (47 < *H && *H < 58)
    x = (x << 1) + (x << 3) + (*H++ ^ 48);
  return x;
}
int S[6], T;
inline void print(int x) {
  int y;
  do
    y = x / 10, S[T++] = x - y * 10, x = y;
  while (x);
  while (T)
    *H++ = S[--T] ^ 48;
}
int a[N], b[N], g[N], h[N], i, j, k, m, n, o;
inline void up(int u) { g[u] = min(g[u << 1], g[u << 1 | 1]); }
inline void add(int u, int v) { g[u] += v, h[u] += v; }
inline void down(int u) {
  if (h[u])
    add(u << 1, h[u]), add(u << 1 | 1, h[u]), h[u] = 0;
}
inline void push(int u) {
  for (j = o; j; j--)
    down(u >> j);
}
inline void add(int u, int v, int w) {
  for (push(u += m - 1), push(v += m + 1); u ^ v ^ 1;
       up(u >>= 1), up(v >>= 1)) {
    if (~u & 1)
      add(u ^ 1, w);
    if (v & 1)
      add(v ^ 1, w);
  }
  while (u >>= 1)
    up(u);
}
inline int query() {
  int u = 1;
  for (; u < m; u = u << 1 | (g[u << 1] > 0))
    down(u);
  return u ^ m;
}
inline void add(int u) {
  while (u <= n)
    h[u]++, u += u & -u;
}
inline void del(int u) {
  while (u <= n)
    h[u]--, u += u & -u;
}
inline int sum(int u) {
  int x = 0;
  while (u)
    x += h[u], u ^= u & -u;
  return x;
}
int main() {
  freopen("test.in", "r", stdin);
  fread(G, 1, 3500000, stdin), n = read(), k = read();
  for (i = 1; i <= n; i++)
    b[a[i] = read()] = i;
  for (m = 1; m < n + 2; m <<= 1)
    o++;
  for (i = 1; i <= n; i++)
    g[m | b[i]] = sum(min(n, b[i] + k - 1)) - sum(max(0, b[i] - k)), add(b[i]);
  for (i = g[m] = n + 1, memset(h + 1, 0, n << 2); i < m; i++)
    g[m | i] = n + 1;
  for (i = m, H = G; --i;)
    up(i);
  for (i = 1; i <= n; i++)
    a[b[i] = query()] = i, add(b[i], b[i], n + 1),
             add(max(1, b[i] - k + 1), min(n, b[i] + k - 1), -1);
  for (i = 1; i <= n; *H++ = '\n', i++)
    print(a[i]);
  return 0 & fwrite(G, 1, H - G, stdout);
}

Fixed submission:
#include <cstdio>
#include <cstring>
#define N 1048576
inline int min(int x, int y) { return x < y ? x : y; }
inline int max(int x, int y) { return x < y ? y : x; }
char G[3500000], *H = G;
inline int read() {
  int x = 0;
  while (*H < 48 || 57 < *H)
    H++;
  while (47 < *H && *H < 58)
    x = (x << 1) + (x << 3) + (*H++ ^ 48);
  return x;
}
int S[6], T;
inline void print(int x) {
  int y;
  do
    y = x / 10, S[T++] = x - y * 10, x = y;
  while (x);
  while (T)
    *H++ = S[--T] ^ 48;
}
int a[N], b[N], g[N], h[N], i, j, k, m, n, o;
inline void up(int u) { g[u] = min(g[u << 1], g[u << 1 | 1]); }
inline void add(int u, int v) { g[u] += v, h[u] += v; }
inline void down(int u) {
  if (h[u])
    add(u << 1, h[u]), add(u << 1 | 1, h[u]), h[u] = 0;
}
inline void push(int u) {
  for (j = o; j; j--)
    down(u >> j);
}
inline void add(int u, int v, int w) {
  for (push(u += m - 1), push(v += m + 1); u ^ v ^ 1;
       up(u >>= 1), up(v >>= 1)) {
    if (~u & 1)
      add(u ^ 1, w);
    if (v & 1)
      add(v ^ 1, w);
  }
  while (u >>= 1)
    up(u);
}
inline int query() {
  int u = 1;
  for (; u < m; u = u << 1 | (g[u << 1] > 0))
    down(u);
  return u ^ m;
}
inline void add(int u) {
  while (u <= n)
    h[u]++, u += u & -u;
}
inline void del(int u) {
  while (u <= n)
    h[u]--, u += u & -u;
}
inline int sum(int u) {
  int x = 0;
  while (u)
    x += h[u], u ^= u & -u;
  return x;
}
int main() {
  fread(G, 1, 3500000, stdin), n = read(), k = read();
  for (i = 1; i <= n; i++)
    b[a[i] = read()] = i;
  for (m = 1; m < n + 2; m <<= 1)
    o++;
  for (i = 1; i <= n; i++)
    g[m | b[i]] = sum(min(n, b[i] + k - 1)) - sum(max(0, b[i] - k)), add(b[i]);
  for (i = g[m] = n + 1, memset(h + 1, 0, n << 2); i < m; i++)
    g[m | i] = n + 1;
  for (i = m, H = G; --i;)
    up(i);
  for (i = 1; i <= n; i++)
    a[b[i] = query()] = i, add(b[i], b[i], n + 1),
             add(max(1, b[i] - k + 1), min(n, b[i] + k - 1), -1);
  for (i = 1; i <= n; *H++ = '\n', i++)
    print(a[i]);
  return 0 & fwrite(G, 1, H - G, stdout);
}