#K49067. Book Sequence Operations

    ID: 28560 Type: Default 1000ms 256MiB

Book Sequence Operations

Book Sequence Operations

You are given a row of n books arranged in a specific order. Each book is identified by its unique ISBN number. You are also given q operations to modify the sequence of books.

There are two types of operations:

  • pick x: Remove the book with ISBN x from the row.
  • place x y: Insert the book with ISBN x immediately after the book with ISBN y in the row.

It is guaranteed that every operation is valid. In particular, a pick operation will always be on a book that is currently in the row, and a place operation will always reference a book y that is in the row. After performing all operations in the given order, you are to output the final sequence of books.

For example, if the row initially is \( [101, 102, 103, 104, 105] \) and the operations are "pick 103", "place 103 102", and "pick 105", the final row is \( [101, 102, 103, 104] \).

inputFormat

The first line contains two integers n and q, representing the number of books in the initial row and the number of operations, respectively.

The second line contains n space-separated integers, the ISBN numbers of the books in their initial order.

Then follow q lines, each containing an operation in one of the following two formats:

  • "pick x"
  • "place x y"

outputFormat

Output the final sequence of ISBN numbers of the books in the row in a single line, separated by spaces.

## sample
5 3
101 102 103 104 105
pick 103
place 103 102
pick 105
101 102 103 104