O(n). Given a comparison function and a lookup function, 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 from the original
elements. (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.