#P3157. Counting Inversions in a Permutation with Deletions

    ID: 16414 Type: Default 1000ms 256MiB

Counting Inversions in a Permutation with Deletions

Counting Inversions in a Permutation with Deletions

Given a permutation of the integers from \(1\) to \(n\), the inversion count of a sequence \(a\) is defined as the number of pairs \((i, j)\) such that \(i a_j\). That is, the inversion count is the size of the set

\[ \{(i,j)\mid i a_j \} \]

You are given a permutation of \(1\) to \(n\). Then, \(m\) elements are removed one by one in a specified order. Your task is to output the inversion count of the current sequence before each deletion.

Note: The inversion count should be calculated on the current sequence at each deletion step after previous deletions have been applied.

inputFormat

The first line contains two integers \(n\) and \(m\) where \(n\) is the size of the permutation and \(m\) is the number of deletion operations.

The second line contains \(n\) space-separated integers representing a permutation of \(1\) to \(n\).

The third line contains \(m\) space-separated integers. Each integer corresponds to an element to be deleted in the given order. It is guaranteed that each deletion target exists in the sequence at the moment of deletion.

outputFormat

Output \(m\) lines. The \(i\)-th line should contain a single integer representing the inversion count of the current sequence before the \(i\)-th deletion.

sample

5 3
5 3 2 1 4
3 1 4
7

4 2

</p>