#P14452. [ICPC 2025 Xi'an R] Follow the Penguins

[ICPC 2025 Xi'an R] Follow the Penguins

题目描述

There are nn penguins standing on a number line. The ii-th penguin is initially located at coordinate aia_i. It is guaranteed that all aia_i-s are pairwise distinct.

Each penguin chooses a target penguin, denoted by tit_i (1tin1 \leq t_i \leq n, tiit_i \neq i). At time 00, all penguins start moving simultaneously. Each one runs towards the current position of its target penguin at a constant speed of 0.50.5 units per second.

When penguin ii meets penguin tit_i, it stops immediately. For every penguin, determine the time when it stops moving. Here, penguin ii meets penguin tit_i if and only if they are at the same coordinate at the same time.

It can be proven that every penguin will stop moving within a finite amount of time, and the stopping time is always an integer.

输入格式

The first line of the input contains a single integer nn (2n51052 \leq n \leq 5 \cdot 10^5), which is the number of penguins.

The second line of the input contains nn integers t1,t2,,tnt_1, t_2, \ldots, t_n (1tin1 \leq t_i \leq n, tiit_i \neq i), where tit_i is the target chosen by the ii-th penguin.

The third line of the input contains nn distinct integers a1,a2,,ana_1, a_2, \ldots, a_n (5108ai5108-5\cdot 10^8\leq a_i\leq 5 \cdot 10^8), where aia_i is the initial coordinate of the ii-th penguin.

输出格式

Output a single line containing nn integers, where the ii-th integer represents the time (in seconds) when the ii-th penguin stops moving.

3
2 3 1
-1 2 3
7 1 4
10
8 3 6 7 1 8 2 10 8 1
0 -14 5 -3 14 -12 11 8 -18 17
25 21 17 14 14 49 29 9 61 17

提示

In the example, initially, since the second penguin is in the positive direction of the first penguin, the first penguin runs in the positive direction. Similarly, the second penguin runs in the positive direction, while the third penguin runs in the negative direction. The initial positions of the three penguins on the number line are shown in the figure below:

:::align{center} :::

At second 11, the second penguin and the third penguin meet at x=2.5x = 2.5, at which point the second penguin stops moving.

:::align{center} :::

At this moment, the first penguin is at 0.5-0.5, and the second penguin is at 2.52.5. The distance between them is 33. Since the first penguin runs at a speed of 0.50.5 units per second, it will take 66 more seconds to reach the second penguin. Therefore, the first penguin stops moving at second 77.

Before the first penguin stops, the third penguin meets it at second 44. So the answers are 77, 11, and 44, respectively.