merge intervals, O(nlogn + n)