C - Remove and Append Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) が与えられます。
q=1,2,\dots,Q の順に、以下の操作を行います。

  • P から値が a_q である要素を削除し、それを P の末尾に追加する。

Q 個の操作を行った後の P の各要素の値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • (P_1,P_2,\dots,P_N) は (1,2,\dots,N) の順列
  • 1 \leq a_q \leq N
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N Q  
P_1 P_2 \ldots P_N
a_1  
a_2  
\vdots  
a_Q  

出力

Q 個の操作を行った後の P_1,P_2,\dots,P_N をこの順に空白区切りで 1 行で出力せよ。


入力例 1

4 2
2 4 3 1
3
2

出力例 1

4 1 3 2

最初、P=(2,4,3,1) です。
1 回目の操作を行うと、P=(2,4,1,3) となります。
2 回目の操作を行うと、P=(4,1,3,2) となります。


入力例 2

3 3
1 2 3
1
1
1

出力例 2

2 3 1

入力例 3

2 5
2 1
1
1
2
2
1

出力例 3

2 1

Score : 300 points

Problem Statement

You are given a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N).
For q=1,2,\dots,Q in this order, perform the following operation.

  • Remove the element with value a_q from P, and append it to the end of P.

Find the value of each element of P after performing the Q operations.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • (P_1,P_2,\dots,P_N) is a permutation of (1,2,\dots,N).
  • 1 \leq a_q \leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N Q  
P_1 P_2 \ldots P_N
a_1  
a_2  
\vdots  
a_Q  

Output

Output P_1,P_2,\dots,P_N after performing the Q operations, in this order, separated by spaces, in one line.


Sample Input 1

4 2
2 4 3 1
3
2

Sample Output 1

4 1 3 2

Initially, P=(2,4,3,1).
After the first operation, P=(2,4,1,3).
After the second operation, P=(4,1,3,2).


Sample Input 2

3 3
1 2 3
1
1
1

Sample Output 2

2 3 1

Sample Input 3

2 5
2 1
1
1
2
2
1

Sample Output 3

2 1