Contest: Division 1
Contest: Division 2

Setter: Hasan
Tester: Teja Vardhan Reddy
Editorialist: Taranpreet Singh




Maps, Binary search or ordering queries would do fine.


Given N disjoint intervals [L_i, R_i) and M query times P_i, we need to print the minimum waiting time from query point to any time belonging to any interval, at or after query time.


  •   Sort intervals as well as query time, maintain two pointers and for every interval, iterate from earliest query time $X$ and answer is $L_i - T_i$ if query time is before interval, otherwise $0$ for every query time such that $R_{i-1} \leq X < R_i$.
  •   Alternate online solution is  for every query $X$, to use binary search to find earliest interval such that $X < R_i$. If query time lies in interval, Wait time is zero, else $L_i-X$.
  • Query times at or after R_n will wait forever (if possible without food :D)


Since intervals are disjoint, every query point can belong to at most one interval, which we need to find, in order to find out the waiting time. Suppose we know that for query time X, the required interval is [L, R). How to calculate waiting time?

See the secret box.

Click to view

If X belong to [L, R), Food is delivered immediately and thus, waiting time is zero.

Otherwise, we will have to wait for Interval to begin, that is, wait from time X up to time L, resulting in waiting time L-X.

This is enough to solve sub task 1, since we can check for every query time, waiting time for all intervals ending after query time and print the minimum of all waiting times, but this results in O(N*M) complexity, which is too slow for sub task 2.

We need to find this interval fast. We know that the interval we are seeking is in fact the earliest interval which ends after query time.

It is ideal to sort the intervals, whenever we need to find such earliest, first, last intervals. Sorting is almost used in every problem which involves intervals.

Anyways, after sorting, if interval j is required interval, we know that either j==1 of R_{j-1} \leq X < R_j. This can be used as condition for binary search, since all interval before jth interval has R_i \leq X and all intervals after jth interval has R_i < X.

Otherwise, we can sort the query points and use two pointers, one for query points and one for interval index. For every interval, assign minimum times to all query points which are before R_i, not assigned answer yet.

In case answer is not assigned to query, customer has to survive without food forever.

Time Complexity

For binary search solution, Time complexity is O((M+N)*logN).
For Offline solution, Time complexity is O(N*logN +M*logM).

Proving complexity is not hard for this problem, though may be referred in secret box.

Click to view

In binary search solution, sorting cost O(N*logN) and each query take O(logN) time, thus total time O((M+N)*logN)

For offline solution, sorting dominates time complexity. After sorting, rest part takes only O(M+N) time.


Setter’s solution
Tester’s solution
Editorialist’s solution

Edit: Until above links are not working, You may refer solution here.

Feel free to Share your approach, If it differs. Suggestions are always welcomed. :slight_smile:

1 Like

Can’t see the solutions - shows html with “Access denied” message.


Why does this question have line sweep tag?? I didn’t participate in the contest. But as soon as I have seen this tag I wanted to upvote. But now I’m reserving it for some other day.

Comment on editorial - I upsolved it using offline mode. But was amazed by seeing binary search approach. :slight_smile:

Python solution (short).

1 Like

@eaugenethomas You don’t check bounds in the loop while(v[f].first){f++;} - f runs beyond the size of the vector.

@eugalt Thanks for pointing out.
Now it works fine

can i get the test cases that u have been uploaded for this?? please, i need to check it out where i made my program to do mistake… thanks in advance…

@taran_1407 ,I applied binary search but it is showing tle.Could u explain?

@taran_1407 , can you please check my code for this problem ,because i dont understand that why i got only 30 point and WA for rest of test cases.

link of my solution :

why am i getting tle? please help!!

link to my solution is:

Whats offline mode???

Can anyone help me I have used Binary search, but it gives me TLE?
link: Ideone

using namespace std;

struct Interval
int start=-1, end=-1;

bool compareInterval(Interval i1, Interval i2)
return (i1.start < i2.start);

int main()
int t;
int n,m;
Interval ta[n+1]={{-1,-1}};
int ta_len=1;
for(int i=1;i<n+1;i++)
int x,y;
sort(ta, ta+ta_len, compareInterval);

	int ca[m+1]={-1};
	int new_ca[m+1]={-1};
	int maxv=-1;
	int ca_len=1;
	for(int i=1;i<m+1;i++)
		int temp;
		if (maxv<temp)
	int sol[maxv+1]={0};
	int j=1;
	int i=1;
		if (ta[i].start <= new_ca[j] && new_ca[j] < ta[i].end)
		else if(ta[i-1].end <= new_ca[j] && new_ca[j] < ta[i].start)
		else if(new_ca[j]>=ta[i].end)
	if (j<ca_len+1)
		for(int i=j;i<ca_len+1;i++)
	for(int i=1;i<=ca_len;i++)


what is wrong with my code? it’s complexity is nlogn (I think so) so why it is giving TLE

why my binary approach solution is giving tle. i think it’s complexity is nlogn.
Also,please suggest some cases for which my code is giving WA.

Can anyone tell me why my BS approach gives TLE but sorting queries approach does not. Both solutions have the same complexity. Both solutions here -
BS -
Sorting queries -

Hey. So this question i had solved in java on the contest day, and the submission from that day is
here. And today, after fruitlessly racking my brain for where the NZEC could have arised, i submitted the same code, and you know it, it showed full AC. Today’s code is here. So what did actually transpire by on the contest day? I am totally perplexed and an answer would be so satisfying.

My [solution][1] during contest got 30 pts with a WA for second test and the same

[2] in practice got AC(100 pts). 
This seems to be a bug as [this comment][3] is also similar and I am getting weird results in other problems too. 


I am using binary search and also pass my vector by reference, still got tle. Please help me, what could i do to decrease the time?
Here is my solution link - Please help.

I was amazed by seeing offline mode XD…
Idk how that idea came first to some of you…

1 Like

Solutions will be linked soon.

Until that, I am going to post ideone links for my solutions.