Jewel Heist

Time limit5sMemory limit128 MB

Summary
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 nn jewels on display in his shop. Every jewel has one of kk 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 xx-coordinate lies within the segment's horizontal span and whose yy-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 kk 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 TT. The test cases follow.

Each test case begins with a line containing two integers nn and kk (2≤n≤200 0002 \le n \le 200\,000, 2≤k≤n2 \le k \le n): the number of jewels and the number of distinct colors. Each of the next nn lines contains three integers xjx_j, yjy_j, cjc_j (1≤xj,yj≤1091 \le x_j, y_j \le 10^9, 1≤cj≤k1 \le c_j \le k), meaning that jewel jj lies at coordinates (xj,yj)(x_j, y_j) and has color cjc_j.

You may assume that every color from 11 to kk 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 kk colors.

Examples4

  1. Example 1

    Input
    1
    10 3
    1 2 3
    2 1 1
    2 4 2
    3 5 3
    4 4 2
    5 1 2
    6 3 1
    6 7 1
    7 2 3
    9 4 2
    
    Expected output
    5
    
  2. Example 2

    Input
    1
    2 2
    1 1 1
    5 5 2
    
    Expected output
    1
    
  3. Example 3

    Input
    1
    5 2
    1 1 2
    2 1 2
    3 1 2
    4 1 2
    10 10 1
    
    Expected output
    4
    
  4. Example 4

    Input
    1
    4 2
    5 1 1
    5 2 1
    5 3 1
    5 4 2
    
    Expected output
    3