#P5416. [CTSC2016] 时空旅行

[CTSC2016] 时空旅行

Description

In 2045, human technology advanced rapidly, and people have found a way to travel through time and space. Little R obtained a time-space travel device and wants to use it to investigate the development of humans in different time-spaces.

According to the parallel time-space theory, there are many independent time-spaces in the universe. Each time-space will further split into several different time-spaces at the next time point. The universe is a three-dimensional space, and humans use a 3D Cartesian coordinate system to describe a position in space, with coordinates x,y,zx, y, z.

Assume that in the initial time-space (numbered 00), humans live on Earth (Earth’s coordinates are (0,0,0)(0,0,0)), and all other time-spaces are developed from an existing time-space. After one time step, a time-space will develop into another time-space (the original time-space does not change). Events that affect Little R are of two types:

  1. Humans colonize a new planet, and the planet’s state becomes “colonized”.
  2. Humans abandon a colonized planet, and the planet’s state becomes “not colonized”.

Each time Little R performs time-space travel, he first chooses a time-space. In this time-space, humans have already colonized some planets. As long as Little R reaches any colonized planet in that time-space, he can investigate the development of humans.

However, Little R’s device has some problems: the button for adjusting the xx coordinate is broken, so the xx coordinate of the destination point is fixed (the fixed xx value may differ for each trip). At the same time, he can still freely adjust the yy and zz coordinates of the destination.

This greatly increases Little R’s cost: time-space travel itself is free, but traveling in space costs money; also, investigating on different planets may incur different fees.

Suppose Little R sets the endpoint of the time-space travel to AA, and the planet he investigates is BB. If the Euclidean distance between AA and BB is dd, then the cost of space travel is d2d^2. If the investigation fee on planet BB is cc, then the total cost of this investigation is d2+cd^2 + c.

Now you are given the time-space Little R arrives at for each trip and the fixed xx coordinate value on the device. Please compute the minimum total cost for Little R to complete the investigation for each trip.

Input Format

The first line contains three non-negative integers n,m,c0n, m, c_0. nn is the number of parallel time-spaces, numbered from 00 to n1n-1, and the initial time-space is numbered 00. mm is the number of time-space trips Little R makes. c0c_0 is the investigation fee on Earth. It is guaranteed that 0<n,m5×1050 < n, m \leq 5 \times 10^5, and 0c010120 \leq c_0 \leq 10^{12}.

The next n1n-1 lines describe parallel time-spaces 11 to n1n-1 in order. For time-space ii, the ii-th line is one of the following two cases:

  1. 0 fr id x y z c0 \ \mathrm{fr} \ \mathrm{id} \ x \ y \ z \ c: time-space ii develops from time-space fr\mathrm{fr}, and humans colonize a planet with number id\mathrm{id}. The planet’s coordinates are (x,y,z)(x, y, z), and the investigation fee on this planet is cc. The testdata guarantees that the given planet numbers are all distinct, and 0<id<n0 < \mathrm{id} < n. Also, x,y,z106|x|, |y|, |z| \leq 10^6, and 0c10120 \leq c \leq 10^{12}.
  2. 1 fr id1 \ \mathrm{fr} \ \mathrm{id}: time-space ii develops from time-space fr\mathrm{fr}, and humans abandon the planet with number id\mathrm{id}. The testdata guarantees that this planet is colonized in time-space fr\mathrm{fr}. Also, id>0\mathrm{id} > 0, meaning Earth will never be abandoned.

In both cases above, all parameters are positive integers, and adjacent integers are separated by one space. It is also guaranteed that 0fr<i0 \leq \mathrm{fr} < i. No other cases will appear.

The next mm lines each describe one time-space trip by Little R. Each line contains two positive integers ss and x0x_0, meaning that Little R travels to parallel time-space ss, and the device fixes the xx coordinate to x0x_0. It is guaranteed that 0s<n0 \leq s < n and x0106|x_0| \leq 10^6.

Output Format

Output mm lines. The ii-th line should be the minimum total cost to complete the investigation for the ii-th trip.

4 4 2
0 0 1 8 2 3 7
0 1 2 10 1 6 2
1 1 1
1 4
2 8
2 6
3 8
18
6
11
66

Hint

For the first 5%5\% of the testdata, n,m100n, m \leq 100.

There is another 35%35\% of the testdata with n,m105n, m \leq 10^5. Among them, for 15%15\% of the testdata, humans never abandon planets, and for another 10%10\% of the testdata, the xx value is the same for every trip.

For the remaining testdata, n,m5×105n, m \leq 5 \times 10^5. Among them, for 10%10\% of the testdata, the xx value is the same for every trip, and for 35%35\% of the testdata, time-space ii develops from time-space i1i - 1. In this 35%35\%, there is 15%15\% of the testdata where humans never abandon planets.

Because the compressed file is too large to upload, for each subtask there is one and only one test point, corresponding to its score.

Translated by ChatGPT 5