#P8876. [传智杯 #5 初赛] H-二人的世界
[传智杯 #5 初赛] H-二人的世界
Background
Lianzi designed a 3D space software. This software can simulate a real physics engine, including solid blocks and flowing water blocks. However, computing a large amount of water flow at the same time will put a heavy load on the software.
So, Lianzi hopes to find an algorithm that can quickly compute the result after these water flow simulations.
Problem Description
The water flow model designed by Lianzi is as follows:
Consider a 3D space. There are cubes in this space. We use coordinates to describe the position of each cube. These cubes are called solid blocks.

Now we will simulate a water flow mechanism in this space. Specifically, we define water blocks. A water block has an intensity , with range .
Simulation Rules
- Suppose there is a water block with intensity at , and there is no solid block at . Then at the next moment, a water block with intensity will be generated at . Note: no matter what the current value of is, the water block generated at always has intensity .
- Suppose there is a water block with intensity at , and , and there is a solid block at . Then a diffusion operation will be performed.
- If at the next moment, multiple water blocks would be generated at the same position , then the intensity of the final generated water block is the maximum intensity among them.


Diffusion Operation
Since the diffusion operation is abstract, it is recommended to understand it together with the diagrams.
For a water block at , it will perform pathfinding on the plane at height . To describe this process, consider the plane at height :
- If there is a solid block at in space, then on the plane, the cell is not passable. Otherwise, if there is no solid block, then is passable.
- When is passable, if there is no solid block at in space, then on the plane, the cell is called a target position. There can be more than one target position.
From the diffusion preconditions, we know that on the plane the cell is passable, but it is not a target position.
Starting from on the plane, perform a path search. Each step expands from to . The search will find all target positions whose distance to is the minimum, and that minimum distance is at most , or it may find no such target position.
- If such target positions exist, then at the next moment, in the direction of the shortest paths to those target positions, water blocks with intensity will be generated.
- If no such target position exists, then at the next moment, water blocks with intensity will be generated at all of (if those positions are reachable).
Please refer to the diagrams to understand the diffusion process.

As shown in the figure, is the position of this water block on the plane. The white blocks are target positions, and the blocks marked with are not passable. We compute the minimum distance from to the nearest target position as . The red paths marked in the figure are three possible shortest paths.
If , then at the next moment, water blocks with intensity will be generated at the positions indicated by the blue arrows. Otherwise, if , then at the next moment, besides the blue arrows, the directions corresponding to the gray paths will also generate water blocks with intensity .
To verify whether the water flow model is reasonable and efficient, Lianzi raises the following question: At , there is a water block with intensity . The question is: after a sufficiently long time (for example, after moments), how many pairs of points satisfy that at position , a water block has ever been generated.
Input Format
- The first line contains two positive integers and two integers , describing the number of solid blocks, the maximum intensity of water blocks, and the position of the initial water block.
- The next lines each contain three integers , describing the position of each solid block. It is guaranteed that no two solid blocks share the exact same position.
Output Format
- Output one integer in a single line, indicating how many pairs have had a water block generated at after a sufficiently long time.
8 3 3 4
4 3 1
4 4 1
3 3 2
3 4 2
4 5 2
5 4 2
2 4 3
4 1 4
3
8 2 3 4
4 3 1
4 4 1
3 3 2
3 4 2
4 5 2
5 4 2
2 4 3
4 1 4
1
Hint
Sample 1 Explanation
(The picture is really too hard to draw, so please bear with it.) To prevent blocks from blocking the view, all blocks are replaced with transparent ones.

In the initial state, a column of water falls from a high altitude and lands on the block , then it diffuses. The water block is at with intensity .

- As in Figure 3, according to the pathfinding mechanism, it generates water blocks with intensity at and .
- As in Figure 4, there are no blocks below both generated branches, so water blocks with intensity are generated at and .
- As in Figure 5, there is still no solid block below the water block , so it generates a water block with intensity at , and it keeps flowing down to ; the water block has a solid block below it, so it generates a water block with intensity at .

Now we only focus on the water block . There is a solid block below it, so it diffuses to both sides, generating two water blocks both with intensity . Below these two blocks there are no more solid blocks, so they keep flowing downward to and .
Therefore, in the end, there will be water blocks passing through three positions: , , and .
Constraints and Notes
For all testdata, , , , .
Translated by ChatGPT 5