You need to find the smallest number > P in [L,R]. Since there is a set in every node of the segment tree, you can use upper_bound to find the smallest number > P in the range for that node. Since every query will visit at most O(logN) nodes, you only need to do this those many times. Finally, you will return the minimum of all upper_bounds that you found in all the nodes that you checked.