Problem p04052 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define F(i, l, r) for (int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define f(i, r, l) for (int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define file(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
using namespace std;

template <typename T> bool chkmax(T &a, T b) { return (a < b) ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return (a > b) ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}
vector<int> G[1000005];
int tree[4000005];
int p[1000005];
int q[1000005];
int in[1000005];
priority_queue<int, vector<int>, greater<int>> Q;
#define mid ((l + r) >> 1)
void update(int rt, int l, int r, int pos, int val) {
  if (l == r) {
    tree[rt] = val;
    return;
  }
  if (pos <= mid)
    update(rt << 1, l, mid, pos, val);
  else
    update(rt << 1 | 1, mid + 1, r, pos, val);
  tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]);
}
int now;
void query(int rt, int l, int r, int L, int R) {
  if (L > R)
    return;
  if (L <= l && r <= R) {
    chkmin(now, tree[rt]);
    return;
  }
  if (L <= mid)
    query(rt << 1, l, mid, L, R);
  if (R > mid)
    query(rt << 1 | 1, mid + 1, r, L, R);
  return;
}
int n, k;
int ans[1000005], top;
int main() {
#ifndef ONLINE_JUDGE
  file("agc001f");
#endif
  Set(tree, 0x3f3f3f3f);
  n = read();
  k = read();
  F(i, 1, n) {
    p[i] = read();
    q[p[i]] = i;
  }
  f(i, n, 1) {
    now = 0x3f3f3f3f;
    query(1, 1, n, q[i] + 1, min(q[i] + k - 1, n));
    if (now != 0x3f3f3f3f) {
      G[q[i]].push_back(q[now]);
      in[q[now]]++;
    }
    now = 0x3f3f3f3f;
    query(1, 1, n, max(1, q[i] - k + 1), q[i] - 1);
    if (now != 0x3f3f3f3f) {
      G[q[i]].push_back(q[now]);
      in[q[now]]++;
    }
    update(1, 1, n, q[i], i);
  }
  F(i, 1, n) if (!in[i]) Q.push(i);
  while (!Q.empty()) {
    int x = Q.top();
    Q.pop();
    ans[x] = ++top;
    for (int v : G[x]) {
      if (--in[v] == 0)
        Q.push(v);
    }
  }
  F(i, 1, top) printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define F(i, l, r) for (int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define f(i, r, l) for (int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define file(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
using namespace std;

template <typename T> bool chkmax(T &a, T b) { return (a < b) ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return (a > b) ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}
vector<int> G[1000005];
int tree[4000005];
int p[1000005];
int q[1000005];
int in[1000005];
priority_queue<int, vector<int>, greater<int>> Q;
#define mid ((l + r) >> 1)
void update(int rt, int l, int r, int pos, int val) {
  if (l == r) {
    tree[rt] = val;
    return;
  }
  if (pos <= mid)
    update(rt << 1, l, mid, pos, val);
  else
    update(rt << 1 | 1, mid + 1, r, pos, val);
  tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]);
}
int now;
void query(int rt, int l, int r, int L, int R) {
  if (L > R)
    return;
  if (L <= l && r <= R) {
    chkmin(now, tree[rt]);
    return;
  }
  if (L <= mid)
    query(rt << 1, l, mid, L, R);
  if (R > mid)
    query(rt << 1 | 1, mid + 1, r, L, R);
  return;
}
int n, k;
int ans[1000005], top;
int main() {
#ifndef ONLINE_JUDGE
// file("agc001f");
#endif
  Set(tree, 0x3f3f3f3f);
  n = read();
  k = read();
  F(i, 1, n) {
    p[i] = read();
    q[p[i]] = i;
  }
  f(i, n, 1) {
    now = 0x3f3f3f3f;
    query(1, 1, n, q[i] + 1, min(q[i] + k - 1, n));
    if (now != 0x3f3f3f3f) {
      G[q[i]].push_back(q[now]);
      in[q[now]]++;
    }
    now = 0x3f3f3f3f;
    query(1, 1, n, max(1, q[i] - k + 1), q[i] - 1);
    if (now != 0x3f3f3f3f) {
      G[q[i]].push_back(q[now]);
      in[q[now]]++;
    }
    update(1, 1, n, q[i], i);
  }
  F(i, 1, n) if (!in[i]) Q.push(i);
  while (!Q.empty()) {
    int x = Q.top();
    Q.pop();
    ans[x] = ++top;
    for (int v : G[x]) {
      if (--in[v] == 0)
        Q.push(v);
    }
  }
  F(i, 1, top) printf("%d\n", ans[i]);
  return 0;
}