## Editorial for DMOPC '21 Contest 2 P2 - Scrambling Swaps

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Riolku

Suppose our array of swaps were static. How would we go about solving this? Well, we can simply take the queried swap list and apply our swaps starting from the back. So if we want to produce and our swap list is , then first we get , and then , so we output .

This simulation is sufficient for this subtask.

Let be our count of type queries:

Time Complexity:

For this subtask, we need a slightly more efficient way of producing the final array. The idea is to use a list mapping query permutation indices to output indices. When we want to add a swap to the front of the list, well, given our current mapping, we can simply swap m[x] and m[y], since we just need to swap where those elements end up.
However, for adding a swap to the end of the list, we need to pretend that items x and y have been swapped this whole time. To do that, we should swap the places in a where x and y appear. This is the same as pretending that they were always swapped. To do this, we need to maintain a pos array, containing the index in a where i appears. Implementation details are left as an exercise to the reader.