I need a data structure that stores a sequence of numbers and supports the following operations. The input to each operation includes the position of an item in the current sequence (not the value or the memory address of that item).
$\mathtt{NearestSmaller}(i)$: Return the value of the last item in the sequence that occurs before position $i$ and whose value is less than the value at position $i$.
$\mathtt{InsertBefore}(x, i)$: Insert a new item with value $x$ into the sequence just before the item with position $i$.
$\mathtt{Delete}(i)$: Delete item at position $i$ from the sequence.
In short, my question is whether there is a dynamic version of the all nearest smaller neighbors algorithm.
For example, if the current sequence is $X = [9,7,3,1,8,12,10,20,15,5]$, then
$\mathtt{NearestSmaller}(6)$ should return $8 = X[5]$, and
$\mathtt{NearestSmaller}(10)$ should return $1 = X[4]$.