O(n). Given a comparison function and a vector, this function constructs a
with the property that the minimum index between
in the result vector is the same as the minimum index between
j in the
original vector. (In both cases, ties are broken by which index comes first.)
Internally, this function constructs the Cartesian tree of the input vector (implicitly, to save memory and stack space), and returns the vector of the depth of each element in the tree.