luogu#P7828. [CCO 2021] Swap Swap Sort

    ID: 7154 远端评测题 3000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>树状数组2021CCO(加拿大)根号分治

[CCO 2021] Swap Swap Sort

Problem Description

You have a sequence of length nn, where each element is a positive integer not greater than kk.

Your friend invented a sorting algorithm that can sort the sequence according to a permutation of 1k1 \sim k. After sorting, for any two unequal numbers in the sequence, their relative order is the same as their relative order in the permutation. His algorithm only uses adjacent swaps, and it always guarantees the minimum number of operations. For convenience, he calls this permutation of 1k1 \sim k the target permutation.

For example, if the sequence is [1,4,2,1,2][1, 4, 2, 1, 2] and the target permutation is [4,1,2,3][4, 1, 2, 3], then after sorting it becomes [4,1,1,2,2][4, 1, 1, 2, 2].

You are very interested in how many swaps your friend’s sorting algorithm performs under different target permutations. To study the pattern, you initially set the target permutation to 1k1 \sim k, and then perform qq operations on it. In each operation, you swap the positions of two adjacent numbers in the target permutation. After each swap, you want to know how many swaps would be performed if you use his sorting algorithm to sort the original sequence.

Input Format

The first line contains three integers n,k,qn, k, q.

The second line contains nn positive integers a1,a2,,ana_1, a_2, \cdots, a_n, representing the original sequence.

The next qq lines each contain one positive integer bb, meaning you swap the bb-th and the (b+1)(b + 1)-th numbers in the target permutation.

Output Format

For each query, output one integer, representing the required value.

5 4 3
1 4 2 1 2
3
2
1
4
2
2

Hint

Constraints

Because the official data package is too large, this problem only includes 2027\frac{20}{27} of the official testdata.

For 427\frac{4}{27} of the testdata, 1n,q5×1031 \leq n, q \leq 5 \times 10^3.

For another 427\frac{4}{27} of the testdata, 1q1001 \leq q \leq 100.

For another 754\frac{7}{54} of the testdata, 1k5001 \leq k \leq 500.

For 100%100\% of the testdata, 1n,k1051 \leq n, k \leq 10^5, 1q1061 \leq q \leq 10^6, 1aik1 \leq a_i \leq k, 1b<k1 \leq b < k.

Source

CCO2021 D1T1

Translated by ChatGPT 5