Create a single, sorted array of transitions. Each transition has a position and a cumulative number based on how many intervals you join or leave. When you go through the list, keep track of how many intervals you are on. When you are in as many intervals as a series, this is when you are in the total interval.
For your example, the transitions will be as follows:
[2, 1], [6, -1], [7, 1], [11, -1], [1, 1], [3, -1], [5, 1], [10, -1], [11, 1], [13, -1] [2, 1], [5, -1], [6, 1], [8, -1]
which, after sorting by position and merge, collapses to:
[1, 1], [2, 2], [3, -1], [5, 0], [6, 0], [7, 1], [8, -1], [10, -1], [11, 0], [13, -1]
which gives you transitions for doing the totals:
[1, 1], [2, 3], [3, 2], [7, 3], [8, 2], [10, 2], [13, 1]
And then we can read the intervals where we are at level 3, starting at 2 and going to 3 , and the other starting at 7 and going to 8 . What answer.
The idea of ββcreating one long list and sorting is, admittedly, extra work. Instead, you can create these lists and combine them on the fly. Savings are a factor in the log of the number of episodes, not the log of the number of events.