Jewel Heist
Time limit5sMemory limit128 MB
Given colored points, find the maximum number of points coverable by a horizontal-span rectangle extending down to minus infinity that avoids containing all k colors.
- Level
Medium7 of 10
- Topics
- Two pointers, Sorting, Binary search
- Solved
- No attempts yet
Problem
Arsène Lupin, the master thief, wants to steal the jewels of the villain Erwin. Erwin has jewels on display in his shop. Every jewel has one of distinct colors. The showroom is so large that we treat it as the Euclidean plane, with the jewels being distinct points. The display is guarded by some rather expensive alarms.
Lupin has invented a device: a large robotic hand that can grab some of Erwin's jewels without triggering any alarm. The hand can make exactly one grab. Lupin picks a horizontal segment, and the hand takes every jewel whose -coordinate lies within the segment's horizontal span and whose -coordinate is at most the segment's height — that is, every jewel lying on the segment or somewhere below it (see the figure).
Lupin could easily take all the jewels this way, but he knows that the more he takes, the harder they will be to fence. He decides that the safest haul is a set of jewels that does not contain all colors.

The robotic hand grabs jewels 1, 2, 4, 5 and 6, carefully leaving out the black ones.
Determine how many jewels Lupin can steal with a single grab of his device, without ending up with a jewel of every color.
Input
The first line contains the number of test cases . The test cases follow.
Each test case begins with a line containing two integers and (, ): the number of jewels and the number of distinct colors. Each of the next lines contains three integers , , (, ), meaning that jewel lies at coordinates and has color .
You may assume that every color from to appears on at least one jewel.
Output
For each test case, print a single line containing the maximum possible number of stolen jewels such that the stolen set does not contain all colors.