Problem:
Score : 2000 points

Problem Statement

You are given a permutation P_1 ... P_N of the set {1, 2, ..., N}.

You can apply the following operation to this permutation, any number of times (possibly zero):

Choose two indices i,j (1 ≦ i < j ≦ N), such that j - i ≧ K and |P_i - P_j| = 1.
Then, swap the values of P_i and P_j.

Among all permutations that can be obtained by applying this operation to the given permutation, find the lexicographically smallest one.

Constraints

2≦N≦500,000

1≦K≦N-1

P is a permutation of the set {1, 2, ..., N}.

Input

The input is given from Standard Input in the following format:

N K
P_1 P_2 ... P_N

Output

Print the lexicographically smallest permutation that can be obtained.

Sample Input 1

4 2
4 2 3 1

Sample Output 1

2
1
4
3

One possible way to obtain the lexicographically smallest permutation is shown below:

4 2 3 1

4 1 3 2

3 1 4 2

2 1 4 3

Sample Input 2

5 1
5 4 3 2 1

Sample Output 2

1
2
3
4
5

Sample Input 3

8 3
4 5 7 8 3 1 2 6

Sample Output 3

1
2
6
7
5
3
4
8

Solution in C++14 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int TEN(int n) {return (n==0)?1:10*TEN(n-1);}

struct Node {
    using NP = Node*;

    int ma = -1;
    int get(int a, int b) {
        if (b <= 0 or sz <= a) return -1;
        if (a <= 0 and sz <= b) {
            return ma;
        }
        return max(l->get(a, b), r->get(a-sz/2, b-sz/2));
    }
    void set(int k, int x) {
        if (sz == 1) {
            ma = x;
            return;
        }
        if (k < sz/2) {
            l->set(k, x);
        } else {
            r->set(k-sz/2, x);
        }
        ma = max(l->ma, r->ma);
    }
    NP l, r;
    int sz;
    Node(int sz) : sz(sz) {
        if (sz == 1) {
            return;
        }
        l = new Node(sz/2);
        r = new Node(sz - sz/2);
    }
};


int main() {
    int n, k;
    cin >> n >> k;
    int p[n], q[n];
    for (int i = 0; i < n; i++) {
        cin >> p[i]; p[i]--;
    }
    for (int i = 0; i < n; i++) {
        q[p[i]] = i;
    }
    Node* rmq = new Node(n);
    int mi[n];
    vector<vector<int>> g(n+1);
    for (int i = 0; i < n; i++) {
        int v = rmq->get(max(0, q[i]-k+1), min(n, q[i]+k));
        if (v != -1) {
            int qv = q[v];
            g[qv].push_back(q[i]);
        }
        rmq->set(q[i], i);
    }
    Node* rrmq = new Node(n);
    for (int i = n-1; i >= 0; i--) {
        int v = n-1-rrmq->get(max(0, q[i]-k+1), min(n, q[i]+k));
        if (v != n) {
            int qv = q[v];
            g[q[i]].push_back(qv);
        }
        rrmq->set(q[i], n-1-i);
    }

    int deg[n];
    fill_n(deg, n, 0);
    for (int i = 0; i < n; i++) {
        for (int j: g[i]) {
            deg[j]++;
        }
    }
    priority_queue<int, vector<int>, greater<int>> que;
    for (int i = 0; i < n; i++) {
        if (deg[i] == 0) {
            que.push(i);
        }
    }
    vector<int> v;
    while (que.size()) {
        int p = que.top(); que.pop();
        v.push_back(p);
        for (int d: g[p]) {
            deg[d]--;
            if (deg[d] == 0) {
                que.push(d);
            }
        }
    }
    for (int d: v) {
        printf("%d\n", d+1);
    }
    return 0;
}