Home => ProblemSet => 6.1-15:Fast Bubble Sort
Problem1758--6.1-15:Fast Bubble Sort

1758: 6.1-15:Fast Bubble Sort

Time Limit: 1 Sec  Memory Limit: 128 MB  Submit: 0  Solved: 0
[ Submit ] [ Status ] [ Creator: ][ 参考程序 ]

Description

Given an array A=(a1,a2,…,am) of length m, denote by array B(A) the output of a single iteration of bubble sort with input array A, i.e., the output of the following algorithm with input array A.


You may perform the following operation any number (including zero) of times on the array A=(a1,a2,…,am):

Select an interval [i,j] where 1≤i≤j≤m, and cyclically shift all elements of ai,ai+1,…,aj−1,aj in either direction, so that they become aj,ai,ai+1,…,aj−1 or ai+1,…,aj−1,aj,ai

For example, if we cyclically shift the interval [1,4] of the array A=[1,2,3,4,5] to the right, the resulting array would be A′=[4,1,2,3,5].

You are now given a permutation P=(p1,p2,…,pn) of length n and you need to answer q independent queries of the following form:

In the i-th query, you are given parameters 1≤li≤ri≤n and you are supposed to find the minimum number of above operations needed to transform the subarray P[li,ri] to B(P[li,ri]), where P[li,ri]=(pli,pli+1,…,pri)
题目大意:给定任何一个长度为 m 的数组A= (a1, a2, . . . , am),令B(A)表示对A进行一次bubble sort循环之后得到的数组。令num(A)表示从A到B(A)最少需要移动元素(数组区间循环移位)的次数。给定一个1 − n的排列Pn以及q组查询。1 ≤ li ≤ ri ≤ n,求num(B[li, ri])。



Input

The first line contains an integer T(1≤T≤10) denote the number of test cases.
For each test case, the first line contains two integers n,q (1≤n,q≤105), denoting the length of permutation P and the number of queries, respectively.
The second line contains n distinct integers p1,p2,…,pn (1≤pi≤n).
Each of the following q lines contains two integers li,ri (1≤li≤ri≤n), denoting the parameters for the i-th query.

Output

For each query of each test case, output an integer in one line, denoting the answer.

Sample Input Copy

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

Sample Output Copy

2
1
0
1
0

Source/Category