CF1619G.Unusual Minesweeper

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

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.

波利卡普非常喜爱玩扫雷游戏。最近,他发现了一款类似的游戏,其规则如下:

游戏场地中分布着若干颗地雷,每颗地雷的位置坐标 (xi,yi)(x_i, y_i) 已知。每颗地雷具有一个以秒为单位的寿命,到期后将自动爆炸。爆炸发生后,该地雷还会在水平和垂直方向上、距离不超过 kk 的范围内引爆所有其他地雷(即沿两条互相垂直的直线)。因此,一次爆炸会在场地上形成一个“加号”('+')形状的爆炸区域。于是,一次爆炸可能引发新的爆炸,如此连锁进行。

此外,波利卡普可以从第 0 秒起,每秒手动引爆任意一颗尚未爆炸的地雷。随后,同样会按照上述规则触发连锁爆炸。所有爆炸均瞬时发生,且引爆其他地雷的过程也是瞬时完成的。

波利卡普希望打破纪录,因此请你帮助他计算:引爆所有地雷所需的最短时间(以秒为单位)。

输入格式

The first line of the input contains an integer tt (1≤t≤1041 \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 (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5, 0≤k≤1090 \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 (−109≤x,y≤109-10^9 \le x, y \le 10^9, 0≤timer≤1090 \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 2⋅1052 \cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

每个测试用例前均有一空行。

接下来的一行包含两个整数 nn 和 kk(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5,0≤k≤1090 \le k \le 10^9)—— 分别表示地雷的数量以及爆炸时波及的距离。

随后是 nn 行,其中第 ii 行描述第 ii 颗地雷的 xx 和 yy 坐标,以及其爆炸倒计时(−109≤x,y≤109-10^9 \le x, y \le 10^9,0≤timer≤1090 \le \text{timer} \le 10^9)。保证所有地雷的坐标互不相同。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \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.

输出 tt 行,每行必须包含对应输入数据的答案——即引爆所有地雷所需的最少秒数。

输入输出样例

  • 输入#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 examples

First 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.

示例中的图片

第一个示例:

  • 第 00 秒:我们在格子 (2,2)(2, 2) 引爆一颗地雷,由于 k=0k=0,它不会引爆其他任何地雷。
  • 第 11 秒:我们在格子 (0,1)(0, 1) 引爆一颗地雷,同时格子 (0,0)(0, 0) 处的地雷也被引爆。
  • 第 22 秒:我们在格子 (1,1)(1, 1) 引爆一颗地雷,同时格子 (1,0)(1, 0) 处的地雷也被引爆。

第二个示例:

  • 第 00 秒:我们在格子 (2,2)(2, 2) 引爆一颗地雷,得到如下图所示结果:

  • 第 11 秒:坐标 (0,0)(0, 0) 处的地雷爆炸,由于 k=2k=2,此次爆炸会引爆格子 (0,1)(0, 1) 和 (1,0)(1, 0) 处的地雷;而这两颗地雷的爆炸又进一步引爆了格子 (1,1)(1, 1) 处的地雷,此时场地上已无剩余地雷。

输入解题思路,AI测评打分。不知道怎么写?

首页