Practise
Keep the top items ready with a heap and sort and sweep ranges to merge, insert and schedule intervals.