Solution #
Let the input be and the answer be . is the number of numbers that are greater than , so we can also know the how many numbers are smaller than . Now we build a segment tree which counts the occurrence of numbers in and iterate reversely, we could know how many unused numbers are smaller than and then find the corresponding number in the segtree and decrease the occurrence of that number by one.