luogu#P16364. [OOI 2026] Permutations and Queries
[OOI 2026] Permutations and Queries
Problem Description
You are given a permutation of length . A permutation of length is an array consisting of distinct integers from to in arbitrary order. We define the of the permutation as the sum over all from to of the quantity (the -th element of the permutation raised to the power of ). Thus, the of the permutation is given by
There are queries of three types:
- Reverse. After this, your permutation is replaced by the permutation , such that for all from to .
- Invert. After this, your permutation is replaced by the permutation , such that for all from to .
- Take the inverse. After this, your permutation is replaced by the permutation , such that for all from to .
Note that after each operation, remains a permutation.
After each query, you need to output the of the permutation.
Input Format
The first line contains two integers and () --- the length of the permutation and the number of queries.
The second line contains positive integers , , , () --- the elements of the permutation. It is guaranteed that all are distinct.
The third line contains positive integers , , , () --- the description of the queries. The number means that the -th modification query to be applied to the permutation is of type .
Output Format
Output numbers, the -th of which is the remainder of the of the permutation modulo , after applying the first queries.
5 5
1 2 3 4 5
1 2 3 1 2
65 3413 3413 65 3413
5 6
5 3 1 4 2
3 3 1 2 3 1
293 303 3225 215 317 3209
Hint
Note
Let's analyze the second example.
Initially, .
The first query is of type , meaning take the inverse. The permutation after this query becomes . The \textit{cost} of this permutation is $3^1 + 5^2 + 2^3 + 4^4 + 1^5 = 3 + 25 + 8 + 256 + 1 = 293$.
The second query is of type , meaning take the inverse. The permutation after this query becomes . The of this permutation is $5^1 + 3^2 + 1^3 + 4^4 + 2^5 = 5 + 9 + 1 + 256 + 32 = 303$.
The third query is of type , meaning reverse. The permutation after this query becomes . The of this permutation is .
The fourth query is of type , meaning invert. The permutation after this query becomes . The of this permutation is .
The fifth query is of type , meaning take the inverse. The permutation after this query becomes . The of this permutation is .
The last query is of type , meaning reverse. The permutation after this query becomes . The of this permutation is .
Scoring
The tests for this problem consist of five groups. Points for each group are awarded only if all tests in the group and all tests in some of the previous groups are passed. Note that passing the tests from the statement is not required for some groups. The final score for each group is the maximum score obtained for that group of tests across all submitted solutions.
| Group | Points | Additional constraints | < | Required groups | Comment |
|---|---|---|---|---|---|
| 0 | -- | Tests from the statement. | |||
| 1 | 15 | 0 | |||
| 2 | 22 | -- | -- | for all | |
| 3 | 26 | for all | |||
| 4 | 16 | for all | |||
| 5 | 21 | 0--4 | |||