Language: C++

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
#include<iterator>
#include<set>
#include<stack>
#include<queue>
#include<fstream>
#include<iomanip>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include<cmath>
#include<list>
#include <sstream>
#include<unordered_map>
using namespace std;

#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); i--)
#define ITER(it,a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a,value) memset(a, value, sizeof(a))

#define ALL(a) a.begin(),a.end()
#define SZ(a) (int)a.size()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;

const int MAX = 5 * 1000 * 100 + 47;

int P[MAX];
int ANS[MAX];
int M[MAX];
priority_queue<int> q;

struct RMQ
{
	int T[MAX * 4];
	int n;

	int Max(int a, int b)
	{
		return P[a] > P[b] ? a : b;
	}

	void build(int v, int tl, int tr)
	{
		if (tl == tr)
		{
			T[v] = tl;
			return;
		}

		int tm = (tl + tr) / 2;
		build(2 * v, tl, tm);
		build(2 * v + 1, tm + 1, tr);
		T[v] = Max(T[2 * v], T[2 * v + 1]);
	}

	void init(int n)
	{
		this->n = n;
		build(1, 0, n - 1);
	}

	int get1(int v, int tl, int tr, int l, int r)
	{
		if (l > r) return n - 1;

		if (tl == l && tr == r) return T[v];

		int tm = (tl + tr) / 2;
		int v1 = get1(2 * v, tl, tm, l, min(r, tm));
		int v2 = get1(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
		return Max(v1, v2);
	}

	int get(int l, int r)
	{
		return get1(1, 0, n - 1, l, r);
	}

	void update(int v, int tl, int tr, int ind, int val)
	{
		if (tl == tr)
		{
			T[v] = tl;
			P[tl] = val;
			return;
		}

		int tm = (tl + tr) / 2;
		if (tm >= ind)
		{
			update(2 * v, tl, tm, ind, val);
		}
		else
		{
			update(2 * v + 1, tm + 1, tr, ind, val);
		}

		T[v] = Max(T[2 * v + 1], T[2 * v]);
	}

	void update(int ind, int val)
	{
		update(1, 0, n - 1, ind, val);
	}
} R;

int n, k;
void upd(int block)
{
	int l = block * k;
	int r = min((block + 1) * k - 1, n - 1);
	int ind = R.get(l, r);
	if (ind == M[block] || ind == n) return;
	int v = R.get(max(ind - k + 1, 0), min(ind + k - 1, n - 1));
	if (v == ind)
	{
		q.push(ind);
		M[block] = ind;
	}
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n >> k;
	FOR(i, 0, n)
	{
		cin >> P[i];
		P[i]--;
	}

	P[n] = -INF;
	R.init(n + 1);
	int sz = (n + k - 1) / k;
	FILL(M, -1);

	FOR(i, 0, sz)
	{
		upd(i);
	}

	int ind = n - 1;
	while (!q.empty())
	{
		int pos = q.top();
		q.pop();
		ANS[pos] = ind;
		R.update(pos, -INF);
		int block = pos / k;
		upd(block);
		if (block) upd(block - 1);
		if (block + 1 != sz) upd(block + 1);
		ind--;
	}

	FOR(i, 0, n) cout << ANS[i] + 1 << "\n";
}

Compiler output:
a.cc: In function 'int main()':
a.cc:26:23: error: 'memset' was not declared in this scope
   26 | #define FILL(a,value) memset(a, value, sizeof(a))
      |                       ^~~~~~
a.cc:153:9: note: in expansion of macro 'FILL'
  153 |         FILL(M, -1);
      |         ^~~~
a.cc:19:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
   18 | #include<list>
  +++ |+#include <cstring>
   19 | #include <sstream>