Sauna
Time limit2sMemory limit1024 MB
Given n friend intervals and a limit t, choose Max's stay of length at most t to maximize friends he outranks minus friends who outrank him.
- Level
Medium6 of 10
- Topics
- Sorting, Segment tree, Two pointers
- Solved
- No attempts yet
Statement
Max loves going to the sauna with his friends. Today he visited the sauna again and brought friends. Each friend enters the steam room exactly once, and the time interval each friend spends there is known (time is measured in seconds from the moment of arrival at the sauna). Max also wants to enter the steam room exactly once, but he has not yet decided when.
Max cares about his reputation. From the point of view of the people in the sauna, person is cooler than person if entered the steam room strictly earlier than and left strictly later than , which shows that is more persistent. Max's reputation is the number of people who are less cool than him minus the number of people who are cooler than him.
Max cannot stay in the steam room for more than seconds. Help him choose the time interval for his stay that makes his reputation as large as possible.
Input
The first line contains two integers and (, ), the number of Max's friends and the maximum time Max can spend in the steam room.
Each of the next lines contains two integers and (), the arrival and departure times of friend .
All and are non-negative integers, but the endpoints of Max's time interval do not have to be integers or non-negative.
Output
Print one number, the maximum reputation Max can get while staying in the steam room for at most seconds.
Hints
In the first example, Max can arrive at time and leave at time , which makes him cooler than all of his friends.
In the second example, Max cannot be cooler than all of his friends, but he can arrive at time and leave at time . Then he is cooler than friends and but not friend . Friend is not cooler than Max either, so the answer is .
In the third example, Max can be cooler only than friend , but then he is less cool than friend . So a reputation greater than is impossible.
In the fourth example, Max gets reputation if he arrives before all of his friends or after all of them.