Problem p04052 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fo(i, a, b) for (i = a; i <= b; i++)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
typedef long long ll;
const int maxn = 5e5 + 5;
using namespace std;
int q[maxn], p[maxn], k, i, j, n, inf, y, a[maxn], d[maxn];
int l[maxn], r[maxn], tree[maxn << 2], now;
struct ar {
  int x, y;
};
bool operator<(ar a, ar b) { return a.y == b.y ? a.x < b.x : a.y < b.y; }
set<ar> w;
inline int read() {
  int n = 0;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    n = n * 10 + c - 48;
  return n;
}
void find(int k, int l, int r, int a, int b) {
  if (now <= tree[k])
    return;
  if (l == a && r == b) {
    now = tree[k];
    return;
  }
  int m = (l + r) >> 1;
  if (b <= m)
    find(k << 1, l, m, a, b);
  else if (a > m)
    find(k << 1 | 1, m + 1, r, a, b);
  else
    find(k << 1, l, m, a, m), find(k << 1 | 1, m + 1, r, m + 1, b);
}
void ins(int k, int l, int r, int x) {
  tree[k] = min(tree[k], y);
  if (l == r)
    return;
  int m = (l + r) >> 1;
  if (x <= m)
    ins(k << 1, l, m, x);
  else
    ins(k << 1 | 1, m + 1, r, x);
}
int main() {
  freopen("permutation.in", "r", stdin);
  freopen("permutation.out", "w", stdout);
  n = read(), k = read();
  fo(i, 1, n) p[i] = read(), q[p[i]] = i;
  memset(tree, 127 / 3, sizeof(tree));
  inf = tree[0];
  fd(i, n, 1) {
    int x = q[i];
    now = inf, find(1, 1, n, max(1, x - k + 1), x);
    if (now != inf)
      l[x] = q[now], d[l[x]]++;
    now = inf, find(1, 1, n, x, min(x + k - 1, n));
    if (now != inf)
      r[x] = q[now], d[r[x]]++;
    y = i, ins(1, 1, n, x);
  }
  fo(i, 1, n) w.insert((ar){i, d[i]});
  fo(i, 1, n) {
    ar now = *w.begin();
    w.erase(w.begin());
    a[i] = now.x;
    if (i == n)
      continue;
    if (l[now.x]) {
      int x = l[now.x];
      w.erase(w.find((ar){x, d[x]}));
      d[x]--;
      w.insert((ar){x, d[x]});
    }
    if (r[now.x]) {
      int x = r[now.x];
      w.erase(w.find((ar){x, d[x]}));
      d[x]--;
      w.insert((ar){x, d[x]});
    }
  }
  fo(i, 1, n) p[a[i]] = i;
  fo(i, 1, n) printf("%d\n", p[i]);
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (i = a; i <= b; i++)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
typedef long long ll;
const int maxn = 5e5 + 5;
using namespace std;
int q[maxn], p[maxn], k, i, j, n, inf, y, a[maxn], d[maxn];
int l[maxn], r[maxn], tree[maxn << 2], now;
struct ar {
  int x, y;
};
bool operator<(ar a, ar b) { return a.y == b.y ? a.x < b.x : a.y < b.y; }
set<ar> w;
inline int read() {
  int n = 0;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    n = n * 10 + c - 48;
  return n;
}
void find(int k, int l, int r, int a, int b) {
  if (now <= tree[k])
    return;
  if (l == a && r == b) {
    now = tree[k];
    return;
  }
  int m = (l + r) >> 1;
  if (b <= m)
    find(k << 1, l, m, a, b);
  else if (a > m)
    find(k << 1 | 1, m + 1, r, a, b);
  else
    find(k << 1, l, m, a, m), find(k << 1 | 1, m + 1, r, m + 1, b);
}
void ins(int k, int l, int r, int x) {
  tree[k] = min(tree[k], y);
  if (l == r)
    return;
  int m = (l + r) >> 1;
  if (x <= m)
    ins(k << 1, l, m, x);
  else
    ins(k << 1 | 1, m + 1, r, x);
}
int main() {
  n = read(), k = read();
  fo(i, 1, n) p[i] = read(), q[p[i]] = i;
  memset(tree, 127 / 3, sizeof(tree));
  inf = tree[0];
  fd(i, n, 1) {
    int x = q[i];
    now = inf, find(1, 1, n, max(1, x - k + 1), x);
    if (now != inf)
      l[x] = q[now], d[l[x]]++;
    now = inf, find(1, 1, n, x, min(x + k - 1, n));
    if (now != inf)
      r[x] = q[now], d[r[x]]++;
    y = i, ins(1, 1, n, x);
  }
  fo(i, 1, n) w.insert((ar){i, d[i]});
  fo(i, 1, n) {
    ar now = *w.begin();
    w.erase(w.begin());
    a[i] = now.x;
    if (i == n)
      continue;
    if (l[now.x]) {
      int x = l[now.x];
      w.erase(w.find((ar){x, d[x]}));
      d[x]--;
      w.insert((ar){x, d[x]});
    }
    if (r[now.x]) {
      int x = r[now.x];
      w.erase(w.find((ar){x, d[x]}));
      d[x]--;
      w.insert((ar){x, d[x]});
    }
  }
  fo(i, 1, n) p[a[i]] = i;
  fo(i, 1, n) printf("%d\n", p[i]);
}