There are \(N\) chairs arranged in a row. \(K\) people come in a line and start occupying the chairs. Each person wants to be as far as possible from every other person. So, every person arriving looks for the largest empty continuous sequence of unoccupied chairs and occupies the middle position. They have a preference indicating whether they would choose the left or the right chair if there are two chairs at the middle to chose from (else the preference does not matter, since there is only 1 chair at the center). If there are multiple largest empty sequences, then the person chooses the sequence which appears first from left side. You are asked to answer \(Q\) queries. Determine which person has occupied the queried position.
Input Format
The first line of every test file are \(2\) integers \(N\) and \(K\).
The next line contains a string \(S\) of length \(K\). The \(i^{th}\) character would be '\(L\)' or '\(R\)' indicating the preference of the \(i^{th}\) person - the left or the right seat respectively.
Next line contains an integer \(Q\) - the number of queries.
Next \(Q\) lines contain an integer \(Q_{i}\) - the queried position.
Output Format
For each query, output the persons' entry number if it is occupied, else print '\(-1\)' without quotes in a new line.
Constraints
\(1 \leq N \leq 10^{18}\)
\(1 \leq K \leq min(N, \space 10^{5})\)
\(1 \leq Q \leq 10^{5}\)
\(1 \leq Q_{i} \leq N\)
\(length(S) = K\)
Sample Inputs
Input | Output |
3 3 RRL 3 1 3 2 |
2 3 1 |
The first person would sit at the center since there are odd number of seats. The second person would occupy the first position since {1, 2} is the largest uncoccupied sequence which appears first and his preference is the left seat. The third would occupy the last seat because his prefernce is the right seat.
Hence \(1^{st}\) position is occupied by \(2^{nd}\) person, \(3^{rd}\) position by \(1^{st}\) person and \(5^{th}\) position by \(3^{rd}\) person.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor