879. The Same List, Many Rank Questions
A list of n numbers is fixed and never changes. Then q questions arrive,
each a rank j, and each asks for the jth smallest value in the list,
counting from 1. Duplicates count separately.
Print the q answers on one line, in the order the questions were asked.
list 7 2 9 4 1, questions 1 3 5 -> 1 4 9
list 5 5 5 5, questions 1 2 3 4 -> 5 5 5 5
list 3 1 2, questions 2 2 1 3 2 -> 2 2 1 3 2
In the first row the sorted list is 1 2 4 7 9, so rank 1 is 1, rank 3 is 4
and rank 5 is 9.
## This problem is here to show where a heap is the WRONG tool
The rest of this topic is about heaps, so it is worth being explicit about when not to reach for one, and this is that case. A heap fails here for two separate reasons.
A heap only knows its extreme. It can tell you the minimum in constant
time and it knows nothing whatever about the 3rd smallest. To reach rank j
you must pop j - 1 values first.
Popping destroys it. Those j - 1 pops removed the values, so the next
question starts from a different structure. You would have to rebuild the heap
for every question, which is O(n) to build and O(j log n) to walk. The
rebuilding alone is q * n = 4 * 10^10 operations at the limits, and the
walking is several times that again.
## What to use instead
Sort the list ONCE, then every question is a single array read.
sort a O(n log n), done one time
for each question j:
print a[j - 1] O(1) each
Total O(n log n + q). At the limits that is roughly 3.5 * 10^6 steps.
## Why a sorted array is enough here, when it is not enough elsewhere
A sorted array's weakness is insertion: putting a new value in the middle shifts everything after it. That weakness never appears in this problem, because nothing is ever inserted. The list is fixed before the first question and fixed after the last one.
That is the rule to carry away from this block: a heap earns its keep only when the collection CHANGES between queries. When it does not, a sort is simpler, faster in practice, and answers questions a heap cannot answer at all, such as "what is the 3rd smallest".
## The off-by-one that breaks a careless implementation
Ranks count from 1 and array indices count from 0, so rank j is
a[j - 1]. Reading a[j] is off by one in every single answer, and on
j = n it reads past the end. Two tests here deliberately ask for rank 1 and
rank n in the same input so that both ends are checked.
## Repeated questions
The same rank may be asked more than once, and the answer is the same every time. A program that consumes something per question, rather than reading an array, gets the third row wrong.
## What is NOT an overflow here
Values reach 10^9 in magnitude, and ranks and counts reach 200000.
Nothing is summed and nothing is multiplied, so every quantity fits in a
signed 32-bit integer, whose ceiling is 2147483647. The only arithmetic in
the whole solution is j - 1.
## Related problems
Problem 243 asks a single rank of a fixed array and problem 272 asks it from
the other end; both are this problem with q = 1. Problem 880 turns the
question into "the k largest, listed", which is where a heap starts paying
for itself, and problem 882 makes the list GROW between questions, which is
where a sorted array finally breaks.
## Cost
Time O(n log n + q). Space O(n + q).
Constraints - `1 ≤ n ≤ 200000` - `1 ≤ q ≤ 200000` - `1 ≤ j ≤ n` - `-1000000000 ≤ a[i] ≤ 1000000000`
Input
The first line contains two integers n and q.
The second line contains n integers, the list.
The third line contains q integers, the ranks being asked for.
Output
Print q integers on one line, separated by single spaces: the answer to each question in the order asked. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.