#P10028. Prefix Reversal and Increasing Pairs Query

    ID: 12005 Type: Default 1000ms 256MiB

Prefix Reversal and Increasing Pairs Query

Prefix Reversal and Increasing Pairs Query

Given a permutation \(a_1, a_2, \dots, a_n\) of the numbers \(1, 2, \dots, n\). You have \(m\) operations. In each operation, an integer \(x\) is given. First, reverse the prefix \(a_1, a_2, \dots, a_x\) (i.e. change it to \(a_x, a_{x-1}, \dots, a_1\)). Then, compute the number of pairs \((i, j)\) such that \(1 \le i < j \le x\) and \(a_i < a_j\).

Note: After each operation, the permutation is permanently changed.

inputFormat

The first line contains two integers \(n\) and \(m\) separated by a space.

The second line contains \(n\) integers representing the initial permutation \(a_1, a_2, \dots, a_n\) of \(\{1,2,\dots,n\}\).

Each of the next \(m\) lines contains an integer \(x\) representing an operation.

outputFormat

Output \(m\) lines. The \(i\)-th line should contain the answer for the \(i\)-th operation, which is the number of pairs \((i,j)\) with \(1 \le i < j \le x\) satisfying \(a_i < a_j\) after performing the reversal.

sample

5 3
1 2 3 4 5
3
5
2
0

3 1

</p>