I was solving this problem. But I keep getting RTE on test2 and I dont understand why.
My idea is selecting the smallest 2*n elements by putting them into a set. Then I use two pointers on the array sorted by coordinate component in order to get the system of nested segments. No wonder what goes wrong with that.