Assume there is an array $A$ of $n$ elements. $A$ contains every number from $1$ to $n$. We are given an array $B$ of $n$ elements. If we move each element $A[i]$ left by $B[i]$ places starting from $i = 0$ until $n-1$, in the end array $A$ will be sorted in increasing order, i.e. it will be $A = [1, 2, 3, ..., n]$. Given array $B$, find the initial value of array $A$.For example, if $B = [0, 1, 2, 0, 1]$, $A$ will be $[3, 2, 1, 5, 4]$: We move $A[1]$ left by $1$, then $A[2]$ left by $2$, then $A[4]$ left by $1$, and $A$ becomes $[1, 2, 3, 4, 5]$.A $(n^2)$ algorithm for finding $A$ is trivial. I know that there is an $(n \log n)$ algorithm using segment trees, but I can't figure it out. 
Find initial ranks
sorting;binary trees