Under construction

Still being built. Things may change

Card Flipping 2

Time limit2sMemory limit1024 MB

Summary
Each card i flips every later card j whose top face is smaller than card i's top face. Print the final top face of each card.
Level

Medium7 of 10

Topics
Segment tree, Simulation, Array
Solved
No attempts yet

Problem

Card Flipping 1 and 2 are different problems.

Junwon has NN cards. Each card has an integer from 11 to NN written on each side. Junwon lays the NN cards in a row. The integers on the top faces are a1,a2,⋯ ,aNa_1,a_2,\cdots ,a_N, and the integers on the bottom faces are b1,b2,⋯ ,bNb_1,b_2,\cdots ,b_N.

Junwon increases ii from 11 to NN by 11 each time and repeats the following action.

  • Compare the ii-th card with the jj-th card for i<j≤Ni<j\le N. Let XX and YY be the integers on the top faces of the ii-th and jj-th cards. Flip every jj-th card that satisfies X>YX>Y.

After all actions are finished, find the integer on the top face of each card.

Input

The first line contains an integer NN (2≤N≤200 0002\le N\le 200\,000).

The second line contains the integers a1,a2,⋯ ,aNa_1,a_2,\cdots ,a_N (1≤ai≤N1\le a_i\le N), separated by spaces.

The third line contains the integers b1,b2,⋯ ,bNb_1,b_2,\cdots ,b_N (1≤bi≤N1\le b_i\le N), separated by spaces.

Output

After all actions are finished, print the integer on the top face of each card, separated by spaces.

Examples2

  1. Example 1

    Input
    4
    1 2 3 4
    4 3 2 1
    
    Expected output
    1 2 3 4
    
  2. Example 2

    Input
    5
    3 1 4 2 4
    2 3 2 5 1
    
    Expected output
    3 3 4 5 1