#P9673. [ICPC 2022 Jinan R] Quick Sort

[ICPC 2022 Jinan R] Quick Sort

题目描述

When Prof. Pang was young, he wrote the following code for quick sort. Please calculate how many swaps are performed when calling quicksort(A,1,n)\text{quicksort}(A, 1, n). AA is a given permutation with length nn.

输入格式

The first line contains one integer T (1T105)T~(1\le T \le 10^5), the number of test cases.

For each test case, the first line contains one positive integer n (1n5×105)n~(1\le n \le 5\times 10^5). The next line contains nn integers a1,,an (1ain)a_1,\ldots, a_n~(1 \le a_i\le n) denoting the permutation AA. It is guaranteed that a1,,ana_1,\ldots, a_n form a permutation, i.e.~aiaja_i\neq a_j for iji \neq j.

It is guaranteed that the sum of nn over all test cases is no more than 5×1055\times 10^5.

输出格式

For each test case, output one line containing the number of swaps performed when calling quicksort(A,1,n)\text{quicksort}(A, 1, n).

3
3
3 2 1
5
2 4 5 3 1
10
7 2 4 6 1 9 10 8 5 3
1
4
7