Card Flipping 2
Time limit2sMemory limit1024 MB
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 cards. Each card has an integer from to written on each side. Junwon lays the cards in a row. The integers on the top faces are , and the integers on the bottom faces are .
Junwon increases from to by each time and repeats the following action.
- Compare the -th card with the -th card for . Let and be the integers on the top faces of the -th and -th cards. Flip every -th card that satisfies .
After all actions are finished, find the integer on the top face of each card.
Input
The first line contains an integer ().
The second line contains the integers (), separated by spaces.
The third line contains the integers (), separated by spaces.
Output
After all actions are finished, print the integer on the top face of each card, separated by spaces.