#P16725. [GKS 2019 #A] Contention

[GKS 2019 #A] Contention

Problem Description

You are selling tickets for the front row of seats at a movie theater. The front row has NN seats, numbered 11 to NN from left to right. You have been out of the office the last week, and upon your return, QQ bookings for seats have piled up! The ii-th booking requests all the seats from LiL_i to RiR_i inclusive. You now have the boring job of entering each booking into the system, one at a time.

Since some of the bookings may overlap, the system might not be able to fulfill each booking entirely. When you enter a booking into the system, it will assign every seat requested by the booking that hasn't already been assigned to a booking entered into the system earlier.

What is the largest integer kk where there exists an order that you can enter the bookings into the system, such that each booking is assigned at least kk seats?

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case starts with a line containing two integers NN and QQ, the number of seats and the number of bookings, respectively. Then, there are QQ more lines, the ii-th of which contains the two integers LiL_i and RiR_i, indicating that the ii-th booking would like to book all the seats from LiL_i to RiR_i, inclusive.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the largest value kk, as described above.

3
5 3
1 2
3 4
2 5
30 3
10 11
10 10
11 11
10 4
1 8
4 5
3 6
2 7
Case #1: 1
Case #2: 0
Case #3: 2

Hint

In Sample Case #1, there are N=5N = 5 seats and Q=3Q = 3 bookings. One possible order is:

  • Put in the second booking, where the system will book 22 seats (33 and 44).
  • Put in the first booking, where the system will book 22 seats (11 and 22).
  • Put in the third booking, where the system will book 11 seat (only seat 55, since seats 11, 22, 33 and 44 are already booked).

Each booking is assigned at least 11 seat, and there is no order that assigns at least 22 seats to each booking, so the answer is 11.

In Sample Case #2, there are N=30N = 30 seats and Q=3Q = 3 bookings. No matter what order you assign the seats in, at least one booking will have no seats assigned to it. So the answer is 00. Notice that there can be seats that are not part of any bookings!

In Sample Case #3, there are N=10N = 10 seats and Q=4Q = 4 bookings. One possible order is:

  • Put in the second booking, where the system will book 22 seats (44 and 55).
  • Put in the third booking, where the system will book 22 seats (33 and 66, since 44 and 55 are already booked). Notice that the seats booked are not necessarily adjacent to each other.
  • Put in the fourth booking, where the system will book 22 seats (22 and 77).
  • Put in the first booking, where the system will book 22 seats (11 and 88).

Each booking is assigned at least 22 seats, and there is no order that assigns at least 33 seats to each booking, so the answer is 22.

Note: We do not recommend using interpreted/slower languages for the Large dataset of this problem.

Limits

T=100T = 100.

1N1061 \le N \le 10^6.

1LiRiN1 \le L_i \le R_i \le N.

Test set 1 (Visible)

1Q3001 \le Q \le 300.

Test set 2 (Hidden)

1Q300001 \le Q \le 30000.

For at least 8585 of the test cases, Q3000Q \le 3000.