#P10150. Prefix Reversal and Intersection Count

    ID: 12139 Type: Default 1000ms 256MiB

Prefix Reversal and Intersection Count

Prefix Reversal and Intersection Count

You are given an integer sequence \(a_1, a_2, \dots, a_n\) satisfying the condition that there does not exist indices \(1 \le i < j < k < l \le n\) such that \(a_i = a_j = a_k = a_l\) (i.e. no number appears 4 or more times). You need to process \(m\) operations. In each operation, a number \(x\) is given. First, you should reverse the prefix of the sequence \(a_1, a_2, \dots, a_x\) so that it becomes \(a_x, a_{x-1}, \dots, a_1\). Then, you must query the number of distinct values \(k\) for which there exist indices \(1 \le i \le x .

Input Format: The first line contains two integers \(n\) and \(m\). The second line contains \(n\) integers denoting the initial sequence \(a_1, a_2, \dots, a_n\). Then \(m\) lines follow, each containing an integer \(x\) representing an operation.

Output Format: For each operation, output the count of distinct numbers \(k\) that appear in both the reversed prefix and the remaining suffix.

inputFormat

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

The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (each separated by a space).

Each of the following \(m\) lines contains a single integer \(x\) (\(1 \le x \le n\)).

outputFormat

For each of the \(m\) operations, output a single integer on a new line representing the number of distinct integers \(k\) such that there exists at least one occurrence of \(k\) in both the first \(x\) elements (after reversal) and in the remaining elements of the array.

sample

5 3
1 2 3 2 1
3
4
2
2

1 2

</p>