Problem p04052 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline T &read(T &x) {
  static char c;
  bool flag = 0;
  while (!isdigit(c = getchar()))
    if (c == '-')
      flag = 1;
  for (x = c - '0'; isdigit(c = getchar()); (x *= 10) += c - '0')
    ;
  if (flag)
    x = -x;
  return x;
}
const int maxn = 500010;
int n, k, t[maxn], p[maxn], ind[maxn];
int head[maxn], nxt[maxn], to[maxn], e;
void ae(int x, int y) {
  to[++e] = y;
  nxt[e] = head[x];
  head[x] = e;
}
namespace SGT {
#define ls h << 1
#define rs h << 1 | 1
#define mid ((l + r) >> 1)
#define lc l, mid
#define rc mid + 1, r
int T[maxn * 3];
void pushup(int h) { T[h] = max(T[ls], T[rs]); }
void update(int h, int l, int r, int p, int x) {
  if (l == r) {
    T[h] = x;
    return;
  }
  if (p <= mid)
    update(ls, lc, p, x);
  else
    update(rs, rc, p, x);
  pushup(h);
}
int query(int h, int l, int r, int L, int R) {
  if (L <= l && r <= R)
    return T[h];
  int ret = 0;
  if (L <= mid)
    chkmax(ret, query(ls, lc, L, R));
  if (R > mid)
    chkmax(ret, query(rs, rc, L, R));
  return ret;
}
} // namespace SGT
using namespace SGT;
priority_queue<int, vector<int>, greater<int>> q;
int main() {
#ifndef ONLINE_JUDGE
  freopen("exec.in", "r", stdin);
  freopen("exec.out", "w", stdout);
#endif
  read(n);
  read(k);
  for (int i = 1; i <= n; i++)
    t[read(p[i])] = i;
  for (int i = 1; i <= n; i++) {
    int pre = query(1, 1, n, t[i] + 1, t[i] + k - 1);
    update(1, 1, n, t[i], i);
    if (pre)
      ae(t[pre], t[i]), ++ind[t[i]];
  }
  for (int i = 1; i <= n; i++)
    if (!ind[i])
      q.push(i);
  for (int i = 1; i <= n; i++) {
    int u = q.top();
    q.pop();
    t[i] = u;
    for (int j = head[u]; j; j = nxt[j]) {
      int v = to[j];
      if (!--ind[v])
        q.push(v);
    }
  }
  for (int i = 1; i <= n; i++)
    p[t[i]] = i;
  for (int i = 1; i <= n; i++)
    printf("%d\n", p[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <class T> inline bool chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline T &read(T &x) {
  static char c;
  bool flag = 0;
  while (!isdigit(c = getchar()))
    if (c == '-')
      flag = 1;
  for (x = c - '0'; isdigit(c = getchar()); (x *= 10) += c - '0')
    ;
  if (flag)
    x = -x;
  return x;
}
const int maxn = 500010;
int n, k, t[maxn], p[maxn], ind[maxn];
int head[maxn], nxt[maxn], to[maxn], e;
void ae(int x, int y) {
  to[++e] = y;
  nxt[e] = head[x];
  head[x] = e;
}
namespace SGT {
#define ls h << 1
#define rs h << 1 | 1
#define mid ((l + r) >> 1)
#define lc l, mid
#define rc mid + 1, r
int T[maxn * 3];
void pushup(int h) { T[h] = max(T[ls], T[rs]); }
void update(int h, int l, int r, int p, int x) {
  if (l == r) {
    T[h] = x;
    return;
  }
  if (p <= mid)
    update(ls, lc, p, x);
  else
    update(rs, rc, p, x);
  pushup(h);
}
int query(int h, int l, int r, int L, int R) {
  if (L <= l && r <= R)
    return T[h];
  int ret = 0;
  if (L <= mid)
    chkmax(ret, query(ls, lc, L, R));
  if (R > mid)
    chkmax(ret, query(rs, rc, L, R));
  return ret;
}
} // namespace SGT
using namespace SGT;
priority_queue<int, vector<int>, greater<int>> q;
int main() {
  read(n);
  read(k);
  for (int i = 1; i <= n; i++)
    t[read(p[i])] = i;
  for (int i = 1; i <= n; i++) {
    int pre = query(1, 1, n, t[i] + 1, t[i] + k - 1);
    update(1, 1, n, t[i], i);
    if (pre)
      ae(t[pre], t[i]), ++ind[t[i]];
  }
  for (int i = 1; i <= n; i++)
    if (!ind[i])
      q.push(i);
  for (int i = 1; i <= n; i++) {
    int u = q.top();
    q.pop();
    t[i] = u;
    for (int j = head[u]; j; j = nxt[j]) {
      int v = to[j];
      if (!--ind[v])
        q.push(v);
    }
  }
  for (int i = 1; i <= n; i++)
    p[t[i]] = i;
  for (int i = 1; i <= n; i++)
    printf("%d\n", p[i]);
  return 0;
}