You have a list of swaps, initially empty. Each swap is a pair of integers
- Add a swap
to the beginning of the list. - Add a swap
to the end of the list. - Output a permutation of the first
positive integers such that when the list of swaps is applied in order from beginning to end, the resulting array is a given target permutation .
A swap
Constraints
There are at most
Subtask 1 [50%]
Subtask 2 [50%]
No additional constraints.
Input Specification
The first line contains
Then B
, E
, or Q
. If it is B
or E
, then two integers B
indicates that you should add the swap to the beginning of the list, whereas E
indicates that you should add it to the end. If the first character is Q
, then
Output Specification
For each query of the third type, output any initial permutation on a single line such that applying the list of swaps in order yields the target permutation
Sample Input
4 5
B 3 4
E 2 3
Q 2 4 1 3
E 2 3
Q 3 1 2 4
Sample Output
2 1 3 4
3 1 4 2
Explanation
Consider the first query. If we take 2 1 3 4
and apply the swaps 2 4 1 3
.
In the second query, our swap list is
Comments