分析:只需要先模拟前n个选手即可,因为只要最大力量的选手出现在最前面后面不论多少轮都不会对更新答案有贡献
// 卡题点:It is guaranteed that the sum of n and the sum of q over all test cases do not exceed 10 ^ 5. 就是所有的t组测试用例加起来才10 ^ 5
// 枚举每个选手的第一次得分的局和最后一次得分的局,因为当能量最大的选手排在最前面后,之后的选手都不会再赢了
// q 次询问
// 如果k小于第一次得分点输出0
// 如果k大于等于第一次得分点,小于等于最后一次得分点 输出k - s + 1
// 如果k大于最后一次得分点,输出e - s + 1
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, q, king;
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
if (a[i] == n)
king = i;
}
int cnt = 1, mx = a[0], pos = 0;
vector<pair<int, int>> b(n);
for (int i = 0; i < n; i++)
b[i].first = 0, b[i].second = 0;
for (int i = 1; i < n; i++)
{
if (a[i] < mx)
{
if (!b[pos].first)
b[pos].first = cnt;
b[pos].second = cnt;
}
else
{
mx = a[i];
pos = i;
if (!b[pos].first)
b[pos].first = cnt;
b[pos].second = cnt;
}
cnt++;
}
// for(int i = 0; i < n; i ++ )
// printf("%d %d ", b[i].first, b[i].second);
// printf("\n");
while (q--)
{
int pos, k;
scanf("%d%d", &pos, &k);
pos -= 1;
if (pos > king || pos > k + 1 || b[pos].first > k || b[pos].first == 0)
{
cout << 0 << endl;
}
else if (pos == king)
{
cout << k - b[king].first + 1 << endl;
}
else
{
if (k >= b[pos].second)
cout << b[pos].second - b[pos].first + 1 << endl;
else
cout << k - b[pos].first + 1 << endl;
}
}
}
return 0;
}