Let's suppose that we go through the points in p from smallest to largest. And let's suppose we're looking at the first point. This will be the smallest point in p. We want to match it with a segment, and if there are multiple segments that could match, we have to choose one of them to match. We'd like to do that in the way that makes things best for the rest of the points.
Here's my idea of how to choose the segment -- choose the one that has the smallest r value. That is the segment that is going to be the least useful for subsequent p values.
Now, the constraints on this one are so small that you use pretty much whatever algorithm you want to solve it. I sorted p, and then added a vector called done, which I set to all zeros. For each value in p, I ran through l and r, and found the segment with the smallest value of r, such that:
That algorithm is O(n2), where n is the size of p, l and r.