#P9066. [yLOI2023] 腐草为萤

[yLOI2023] 腐草为萤

Background

At the end of midsummer, the night is still scorching.
Another parting and reunion begins in sorrow.
Is it hiding under a fan, or crushed by rain.
Everywhere is worth it, unwilling to let go.

— Yin Lin, Rotting Grass Turns Into Fireflies

Problem Description

As night falls, on a straight path in the woods, fireflies respond to the call of the night and go out to move around.

Treat the path as a number line. Initially, there are nn fireflies located at some integer points on the number line, labeled from left to right as 1n1 \sim n. The initial coordinate of firefly ii is xix_i. Each firefly has a different brightness value, and the brightness of firefly ii is aia_i.

At any moment, for any surviving firefly ii, it flies according to the following rules:

  • Among the fireflies adjacent to ii (there may be one or two) that are still alive, find the one with the maximum brightness, and denote its index as jj. If ai<aja_i < a_j, then ii flies toward jj; otherwise, ii stays in place.
  • Here, two fireflies are defined to be “adjacent” if there is no other surviving firefly between them.
  • All fireflies fly at a speed of 1 unit length per second.

Fireflies have short lives. When two fireflies meet (i.e., their coordinates become the same), the firefly with the lower brightness will run out of life and disappear from the path. Clearly, in the end only 1 firefly will remain. For each of the other fireflies, find the coordinate where it runs out of life.

Input Format

The first line contains an integer, representing the number of fireflies nn.
The second line contains nn integers, where the ii-th integer represents the initial coordinate xix_i of firefly ii. The testdata guarantees that xix_i is strictly increasing.
The third line contains nn integers, where the ii-th integer represents the brightness value aia_i of firefly ii. The testdata guarantees that all brightness values are distinct.

Output Format

Output one line with nn integers separated by single spaces. The ii-th integer represents the coordinate where firefly ii runs out of life. If firefly ii survives to the end, output 0 for the ii-th number.

4
1 2 3 4
2 3 1 4

2 4 4 0
5
1 2 3 4 5
5 3 2 1 4

0 1 1 5 1
5
2 4 6 10 12
5 3 1 4 2

0 2 2 2 10
7
2 4 6 8 12 14 16
5 3 2 6 1 4 7

8 2 8 16 16 16 0
7
2 4 6 8 12 14 16
7 1 6 3 5 4 2

0 2 2 6 2 12 2

Hint

Explanation of Sample 1

  • In the first second, firefly 11 moves to the right, firefly 22 stays still, firefly 33 moves to the right, and firefly 44 stays still.
  • At the beginning of the second second, firefly 11 meets firefly 22. The former has lower brightness and runs out of life, and its coordinate is 22. Firefly 33 meets firefly 44. The former has lower brightness and runs out of life, and its coordinate is 44.
  • Next, firefly 22 moves to the right until it meets firefly 44 at coordinate 44, and then runs out of life.

Constraints

  • For 5%5\% of the testdata, n=2n = 2.
  • For 30%30\% of the testdata, n100n \leq 100, xi200x_i \leq 200.
  • For 60%60\% of the testdata, n103n \leq 10^3.
  • Another 5%5\% of the testdata satisfies Special Condition A.
  • Another 5%5\% of the testdata satisfies Special Condition B.
  • For 100%100\% of the testdata, it is guaranteed that 2n5×1052 \leq n \leq 5 \times 10^5, 1xi1091 \leq x_i \leq 10^9, 1ain1 \leq a_i \leq n. Also, xi<xi+1x_i < x_{i + 1}, and aia_i is a permutation of length nn.

Where:

  • Special Condition A: the sequence aa is strictly increasing.
  • Special Condition B: the sequence aa is unimodal with only one maximum. That is, there exists pp satisfying 1p<n1 \leq p < n such that a1apa_1 \sim a_p is strictly increasing, and apana_p \sim a_n is strictly decreasing.

Notes

  • Please note that input and output on large testdata may affect program efficiency. Choose appropriate I/O methods to avoid TLE.
  • Please note that the constant factor in time complexity may affect program running efficiency.

Additional Information

There are 7 additional sample files for this problem. See glowworm.zip in the attachments.

Translated by ChatGPT 5