Permutation Cycles and Queries
Time limit7sMemory limit512 MB
Given a permutation A and a huge exponent m, answer queries that swap two entries of A and print the sum of S^m(i) times i.
- Level
Hard8 of 10
- Topics
- Number theory, Array, Implementation
- Solved
- No attempts yet
Problem
You are given an array of distinct integers from to , and an integer . Define and for and . There is a list of queries about . Each query is one of two types.
- Type 1,
1 i j: swap the values of and . - Type 2,
2: print .
Process the queries in in order, from the first to the -th, and print the result of each type 2 query.
Input
The first line contains and , separated by a space.
The second line contains the elements , separated by spaces.
The third line contains the number of queries .
The next lines each contain one query as a sequence of integers separated by spaces.
Output
For each type 2 query, print the result on its own line, in the order the queries appear.
Constraints
- for
- Every query in is type 1 or type 2.
- contains at least 1 and at most type 2 queries.
Hint
is the result of applying the move operation to the elements of array exactly times. Let be the position of the -th element after moves, so the initial position is , the position after one move is , and after moves it is .
For example, let and . For the first element, , , , and . For the second element, , , , and . For the third element, for every .