CF1619G.Unusual Minesweeper

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Polycarp is very fond of playing the game Minesweeper. Recently he found a similar game and there are such rules.

There are mines on the field, for each the coordinates of its location are known ( xi,yix_i, y_i ). Each mine has a lifetime in seconds, after which it will explode. After the explosion, the mine also detonates all mines vertically and horizontally at a distance of kk (two perpendicular lines). As a result, we get an explosion on the field in the form of a "plus" symbol ('+'). Thus, one explosion can cause new explosions, and so on.

Also, Polycarp can detonate anyone mine every second, starting from zero seconds. After that, a chain reaction of explosions also takes place. Mines explode instantly and also instantly detonate other mines according to the rules described above.

Polycarp wants to set a new record and asks you to help him calculate in what minimum number of seconds all mines can be detonated.

输入格式

The first line of the input contains an integer tt ( 1t1041 \le t \le 10^4 ) — the number of test cases in the test.

An empty line is written in front of each test suite.

Next comes a line that contains integers nn and kk ( 1n21051 \le n \le 2 \cdot 10^5 , 0k1090 \le k \le 10^9 ) — the number of mines and the distance that hit by mines during the explosion, respectively.

Then nn lines follow, the ii -th of which describes the xx and yy coordinates of the ii -th mine and the time until its explosion ( 109x,y109-10^9 \le x, y \le 10^9 , 0timer1090 \le timer \le 10^9 ). It is guaranteed that all mines have different coordinates.

It is guaranteed that the sum of the values nn over all test cases in the test does not exceed 21052 \cdot 10^5 .

输出格式

Print tt lines, each of the lines must contain the answer to the corresponding set of input data — the minimum number of seconds it takes to explode all the mines.

输入输出样例

  • 输入#1

    3
    
    5 0
    0 0 1
    0 1 4
    1 0 2
    1 1 3
    2 2 9
    
    5 2
    0 0 1
    0 1 4
    1 0 2
    1 1 3
    2 2 9
    
    6 1
    1 -1 3
    0 -1 9
    0 1 7
    -1 0 1
    -1 1 9
    -1 -1 7

    输出#1

    2
    1
    0

说明/提示

Picture from examplesFirst example:

  • 00 second: we explode a mine at the cell (2,2)(2, 2) , it does not detonate any other mine since k=0k=0 .
  • 11 second: we explode the mine at the cell (0,1)(0, 1) , and the mine at the cell (0,0)(0, 0) explodes itself.
  • 22 second: we explode the mine at the cell (1,1)(1, 1) , and the mine at the cell (1,0)(1, 0) explodes itself.

Second example:

  • 00 second: we explode a mine at the cell (2,2)(2, 2) we get:

- 11 second: the mine at coordinate (0,0)(0, 0) explodes and since k=2k=2 the explosion detonates mines at the cells (0,1)(0, 1) and (1,0)(1, 0) , and their explosions detonate the mine at the cell (1,1)(1, 1) and there are no mines left on the field.

首页