Two Teams
Contest: Codeforces Round 552 (Div. 3)
Difficulty rating: 1800
Tags: data structures, implementation, sortings

There are $$$n$$$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.

The $$$i$$$-th student has integer programming skill $$$a_i$$$. All programming skills are distinct and between $$$1$$$ and $$$n$$$, inclusive.

Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $$$k$$$ closest students to the left of him and $$$k$$$ closest students to the right of him (if there are less than $$$k$$$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).

Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.

Input format:
The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of students and the value determining the range of chosen students during each move, respectively.

The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the programming skill of the $$$i$$$-th student. It is guaranteed that all programming skills are distinct.

Output format:
Print a string of $$$n$$$ characters; $$$i$$$-th character should be 1 if $$$i$$$-th student joins the first team, or 2 otherwise.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example the first coach chooses the student on a position $$$3$$$, and the row becomes empty (all students join the first team).

In the second example the first coach chooses the student on position $$$4$$$, and the row becomes $$$[2, 1]$$$ (students with programming skills $$$[3, 4, 5]$$$ join the first team). Then the second coach chooses the student on position $$$1$$$, and the row becomes empty (and students with programming skills $$$[1, 2]$$$ join the second team).

In the third example the first coach chooses the student on position $$$1$$$, and the row becomes $$$[1, 3, 5, 4, 6]$$$ (students with programming skills $$$[2, 7]$$$ join the first team). Then the second coach chooses the student on position $$$5$$$, and the row becomes $$$[1, 3, 5]$$$ (students with programming skills $$$[4, 6]$$$ join the second team). Then the first coach chooses the student on position $$$3$$$, and the row becomes $$$[1]$$$ (students with programming skills $$$[3, 5]$$$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $$$1$$$ joins the second team).

In the fourth example the first coach chooses the student on position $$$3$$$, and the row becomes $$$[2, 1]$$$ (students with programming skills $$$[3, 4, 5]$$$ join the first team). Then the second coach chooses the student on position $$$1$$$, and the row becomes empty (and students with programming skills $$$[1, 2]$$$ join the second team).

Example input:
5 2
2 4 5 3 1

Example output:
11111

Example input:
5 1
2 1 3 5 4

Example output:
22111

Editorial:
Let's maintain two data structures: a queue with positions of students in order of decreasing their programming skill and a set (std::set, note that we need exactly ordered set) with positions of students not taken in any team.
To construct the first data structure we need to sort pairs $$$(a_i, i)$$$ in decreasing order of the first element and after that push second elements in order from left to right.
The second data structure can be constructed even easier — we just need to insert all values from $$$[1, n]$$$ into it.
Also let's maintain an array $$$ans$$$, where $$$ans_i = 1$$$ if the $$$i$$$-th student belongs to the first team and $$$ans_i = 2$$$ otherwise, and the variable $$$who$$$ to determine whose turn is now (initially it is $$$1$$$).
While our set is not empty, let's repeat the following algorithm: firstly, while the head (the first element) of the queue is not in the set, pop it out. This is how we determine which student will be taken now. Let his position be $$$pos$$$. And don't forget to pop him out too. Create the additional dynamic array $$$add$$$ which will contain all students we will add to the team during this turn.
Let's find the iterator to the student with the position $$$pos$$$. Then make the following sequence of moves $$$k+1$$$ times: add the element the current iterator is pointing at to the array $$$add$$$, then if the current iterator is pointing at the first element, break the cycle, otherwise go to the iterator pointing at the previous element.
Then let's find the iterator to the student next to the student with position $$$pos$$$. And then let's make almost the same sequence of moves $$$k$$$ times: if the current iterator is pointing to the end of the set, break the cycle, otherwise add the element the current iterator is pointing at to the array $$$add$$$ and advance to the iterator pointing at the next element.
Then let's remove all values from the array $$$add$$$ from the set, and for each student $$$st$$$ we delete let's set $$$ans_{st} = who$$$. And change the variable $$$who$$$ to $$$2$$$ if it is $$$1$$$ now and to $$$1$$$ otherwise.
Time complexity: $$$O(n \log n)$$$.