PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000033. Hoán vị dần biến mất (Disappearing Permutation)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hoán vị dần biến mất (Disappearing Permutation)

Disappearing Permutation

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two permutations pp and dd of the integers from 11 to nn. Initially, the array is pp. Its positions are then deleted in the order d1,d2,…,dnd_1,d_2,\ldots,d_n.

At step ii, the element at position did_i is replaced by 00; all positions deleted earlier remain 00. After this deletion, determine the minimum number of operations needed to turn the current array into a permutation of 1,2,…,n1,2,\ldots,n. A restoration operation chooses a position jj and assigns exactly the value jj to it.

Restoration operations are performed only hypothetically to compute each answer. They are not applied to the array used for subsequent deletions. Output one answer after every deletion, in order.

Input

The first line contains nn, the second the permutation pp, and the third the permutation dd.

Output

Print nn answers in query order.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le40; pp and dd are both permutations of 1,2,…,n1,2,\ldots,n.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le5000; pp and dd are both permutations of 1,2,…,n1,2,\ldots,n.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le10^5; pp and dd are both permutations of 1,2,…,n1,2,\ldots,n.

Examples

Input

5
4 5 3 1 2
4 5 1 3 2

Output

2 4 4 5 5

Explanation

Once a deleted position touches a permutation cycle, repairing the array requires correcting that entire cycle.