I have a list of sorted intervals and another list, which describes the index of every interval. Here's an example:
intervals = [(0, 2), (2, 3), (4, 6)] // (0, 2) includes 0, but doesn't include 2
values = [2, 3, 7]
Now, I want to know, how often every interval is "mentioned". An interval is mentioned, when it's value is in another interval. The problem is, that when an interval is mentioned n-times, all the intervals it mentions are also mentioned n-times. All intervals are by default mentioned one time. So, (4, 6) is mentioned one time, (2, 3) one time too, but (0, 2) is mentioned two times, because it's index 2 is in the interval (2, 3). I would like to get the list [2, 1, 1] as an output in this example. Note that the values in the list are always increasing and that an interval can only mention intervals above itself.
I already have a solution, but its time complexity is O(n**2) and that's too slow for me. Does anybody now a linear or a O(n*log n) solution?