After reading the Editorial, I am using MergeSort to sort and BinarySearch for finding lowest upper and highest lower bounds, its showing TLE in a number of cases.

At 1st i applied this:

Taking a string of 1000000 and marking Wormhole up and down times with 1 and 2 in the respective places.

Something like this : 0101000000002100000020001

Then for each input contest i was pointing its starting and ending values in the array and moving left till i find the 1st 1 and right until i find the 1st 2, if either one dosent exist then contest cant be attempted, else the ans if the index i find the 1st 2 - index of 1st 1 +1.

This way only 2 cases showed TLE and all other were AC with good timings.(i understand it takes time approximately O(n^2) time)

If you could please suggest a way i could optimise this method anyhow.

And also could you please explain why is my code(BinarySearch and MergeSort) is showing too many TLEs.

link: https://www.codechef.com/viewsolution/44651114