Preparando MOJI
You are given a permutation, $$$p_1, p_2, \ldots, p_n$$$.
Imagine that some positions of the permutation contain bombs, such that there exists at least one position without a bomb.
For some fixed configuration of bombs, consider the following process. Initially, there is an empty set, $$$A$$$.
For each $$$i$$$ from $$$1$$$ to $$$n$$$:
After the process is completed, $$$A$$$ will be non-empty. The cost of the configuration of bombs equals the largest element in $$$A$$$.
You are given another permutation, $$$q_1, q_2, \ldots, q_n$$$.
For each $$$1 \leq i \leq n$$$, find the cost of a configuration of bombs such that there exists a bomb in positions $$$q_1, q_2, \ldots, q_{i-1}$$$.
For example, for $$$i=1$$$, you need to find the cost of a configuration without bombs, and for $$$i=n$$$, you need to find the cost of a configuration with bombs in positions $$$q_1, q_2, \ldots, q_{n-1}$$$.
The first line contains a single integer, $$$n$$$ ($$$2 \leq n \leq 300\,000$$$).
The second line contains $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \leq p_i \leq n)$$$.
The third line contains $$$n$$$ distinct integers $$$q_1, q_2, \ldots, q_n$$$ ($$$1 \leq q_i \leq n)$$$.
Print $$$n$$$ space-separated integers, such that the $$$i$$$-th of them equals the cost of a configuration of bombs in positions $$$q_1, q_2, \ldots, q_{i-1}$$$.
3 3 2 1 1 2 3
3 2 1
6 2 3 6 1 5 4 5 2 1 4 6 3
6 5 5 5 4 1
In the first test:
In the second test:
Let's consider the process for $$$i = 4$$$. There are three bombs on positions $$$q_1 = 5$$$, $$$q_2 = 2$$$, and $$$q_3 = 1$$$.
At the beginning, $$$A = \{\}$$$.
In the end, we have $$$A = \{1, 4, 5\}$$$, so the cost of the configuration is equal to $$$5$$$.