8650: Fast Bubble Sort

内存限制:128 MB 时间限制:5 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:3 通过:1

题目描述

Given an array A=(a1,a2,,am) of length mm, 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):

  1. Select an interval [i,j][i,j] where 1ijm, and cyclically shift all elements of ai,ai+1,,aj1,aj in either direction, so that they become aj,ai,ai+1,,aj1 or ai+1,,aj1,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 permutationP=(p1,p2,,pn) of length n and you need to answer q independent queries of the following form:

  1. In the ii-th query, you are given parameters 1lirin 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).


输入格式

The first line contains an integer T(1T10) denote the number of test cases.

For each test case, the first line contains two integers n,q 1n,q105), denoting the length of permutation P and the number of queries, respectively.

The second line contains n distinct integers p1,p2,,pn  1pin).

Each of the following q lines contains two integers 1lirin), denoting the parameters for the i-th query.

输出格式

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

输入样例 复制

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

输出样例 复制

2
1
0
1
0