## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

# [Solution] Scheduling a Meeting Round F 2022 - Kick Start 2022 Solution

### Problem

Scheduling meetings at Google is not an easy task. Even with the help of Google Calendar, Ada has a lot of difficulty with it!

Ada works as a Software Engineer at Google, and needs to get approval for her new project. In order to get an approval, she needs to meet with at least $K$ of $N$ Tech Leads.

Ada has access to the calendars of all $N$ Tech Leads. For each Tech Lead, Ada can see all their scheduled meetings. The timeline in this problem can be viewed as $D$ consecutive hours, and all meetings are in $\left[0,\mathbf{D}\right]$ hours range, with both ends being integer numbers. Scheduled meetings, even for the same person, can overlap (people are notorious for this at Google!).

Ada needs to schedule an $X$-hour-long meeting in the interval of $\left[0,\mathbf{D}\right]$ hours, with both ends being integer numbers as well. At least $K$ of $N$ Tech Leads should be present for the whole meeting, that is their calendar should be completely free for the entire meeting duration.

Unfortunately, it might be the case that it is already impossible to find a slot to schedule such an $X$-hour-long meeting. In that case, Ada will need to persuade some Tech Leads to cancel their existing meetings.

What is the minimum number of scheduled meetings that need to be canceled so that Ada can meet with at least $K$ Tech Leads?

### Input

The first line of the input gives the number of test cases, $T$$T$ test cases follow.

The first line of each test case contains four integers $N$$K$$X$$D$$N$ represents the number of Tech Leads, $K$ is the minimum number of Tech Leads Ada needs to meet, $X$ is the length of the meeting that needs to be set up, and $D$ is the upper bound of the $\left[0,\mathbf{D}\right]$ hour range representing the timeline of the problem — no meeting can end after $D$.

The second line of each test case contains an integer $M$, representing the number of scheduled meetings.

$M$ lines follow. The $i$-th of these contains three integer numbers ${\mathbf{P}}_{\mathbf{i}}$${\mathbf{L}}_{\mathbf{i}}$, and ${\mathbf{R}}_{\mathbf{i}}$. These numbers represent that a Tech Lead ${\mathbf{P}}_{\mathbf{i}}$ has a scheduled meeting between the hours ${\mathbf{L}}_{\mathbf{i}}$ and ${\mathbf{R}}_{\mathbf{i}}$, not including the endpoints (that is, the meeting can be seen as an $\left({\mathbf{L}}_{\mathbf{i}},{\mathbf{R}}_{\mathbf{i}}\right)$ interval).

Note that all $M$ meetings in the test case are independent, even if some of them have the same starting and ending time.

### Output

For each test case, output one line containing Case #x$x$: y$y$, where $x$ is the test case number (starting from 1) and $y$ is the minimum number of scheduled meetings that needs to be canceled so that Ada can schedule an $X$-hour-long meeting with at least $K$ Tech Leads.

### Note on the Time Format

The timeline of this problem can be seen as an $\left[0,\mathbf{D}\right]$ interval — that is, $D$ consecutive hours, where $D$ can be bigger than $24$.

A meeting in the interval $\left(L,R\right)$ means the meeting starts at the beginning of the $L$-th hour, and ends at the beginning of the $R$-th hour, covering the whole time period in between, without any gaps (i.e. the interval is continuous). Endpoints are not included in an $\left(L,R\right)$ interval. For Tech Leads attending Ada's scheduled meeting, Ada's new meeting can border some of their other non-canceled meetings — that is, it can start right when another meeting ends, or end right when another meeting starts, or both. A Tech Lead cannot attend Ada's meeting if they have any other non-canceled meetings overlapping with Ada's meeting at any point.

See explanation of the sample test cases for more clarity.

### Limits

Time limit: 40 seconds.
Memory limit: 1 GB.
$1\le \mathbf{T}\le 100$.
$1\le {\mathbf{P}}_{\mathbf{i}}\le \mathbf{N}$, for all $i$.
$0\le {\mathbf{L}}_{\mathbf{i}}<{\mathbf{R}}_{\mathbf{i}}\le \mathbf{D}$, for all $i$.

#### Test Set 1

$1\le \mathbf{X}\le \mathbf{D}\le 8$.
$1\le \mathbf{K}\le \mathbf{N}\le 10$.
$0\le \mathbf{M}\le 20$.

#### Test Set 2

$1\le \mathbf{X}\le \mathbf{D}\le {10}^{5}$.
$1\le \mathbf{K}\le \mathbf{N}\le {10}^{5}$.
$0\le \mathbf{M}\le {10}^{5}$.

In Sample Case #1, Ada needs to schedule a two-hour-long meeting with at least two Tech Leads. She can schedule such a meeting between hours $1$ and $3$ with Tech Leads $\mathrm{#}1$ and $\mathrm{#}3$. In this case, no existing meetings need to be canceled.

In Sample Case #2, Ada needs to schedule a two-hour-long meeting with all three Tech Leads. She can schedule such a meeting in the interval $\left[0,2\right]$, which will require meetings $2$ and $4$ to be canceled. Another option is to schedule a meeting in the interval $\left[1,3\right]$. Both options require two meetings to be canceled, which is the minimum number possible.

In Sample Case #3, Ada needs to schedule a three-hour-long meeting with at least two Tech Leads. She can schedule this meeting in the interval $\left[0,3\right]$, and meet with Tech Leads $\mathrm{#}1$ and $\mathrm{#}3$. This will require meeting $4$ to be canceled, and this is the optimal solution here.