Race in Fooland
Submissions: 1043   Accuracy:

14.11%

  Difficulty: Easy   Marks: 2

Fooland city has a special multi-lane running track. Lanes are numbered from 1 to TL. The lanes have N horizontal barriers lying on it (due to some digging and construction work). The ith barrier cover lanes from Li1 to Li2 (both inclusive). Also, the barriers may overlap. The athletes can’t run on the lanes on which barriers are present because they aren’t allowed to switch lanes anytime in the race. (Once a particular lane is allotted to an athlete he/she can’t switch his/her lane anytime during the race and there can be only one athlete on each lane.)

Now the race has to be organised tomorrow itself and the digging work could not be finished on time. The race would have to be organised on the same track only because Fooland city doesn’t have any other track. Help organizers in finding out how many athletes can run in a single race so that none of them would have to switch lanes.

Input:
The first line contains a single integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains two integers - N (total number of barriers) and TL (total number of lanes). The second line of each test case contains space separated 2*N integers which denote Li1 and Li2 denoting the lanes which the ith barrier covers.

Output:
For each test case, print on a new line a single integer denoting the number of athletes that can run in a single race.

Constraints:
1<=T<=10^5
1 < N < 10^6
1 < TL, Li1, Li2 < 10^6

Example:

Input:
1
5 20
1 3 5 8 8 13 14 15 9 12

Output:
6

Explanation:

The barriers cover lanes from 1 to 3, 5 to 8, 8 to 13, 14 to 15 and 9 to 12. Hence, barriers are present on the lanes: {1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}.

Hence, the lanes which doesn't come under any barriers are {4, 16, 17, 18, 19, 20}.
So, there are 6 lanes on which athletes can run – {4, 16, 17, 18, 19, 20}

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Sajal Agrawal
Author: sajalagrawal


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.