N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment).
Each player has a unique skill rank. To improve their skill rank, they often compete with each other. If two players want to compete, they must choose a referee among other ping pong players and hold the game in the referee's house. For some reason, the contestants can’t choose a referee whose skill rank is higher or lower than both of theirs.
The contestants have to walk to the referee’s house, and because they are lazy, they want to make their total walking distance no more than the distance between their houses. Of course all players live in different houses and the position of their houses are all different. If the referee or any of the two contestants is different, we call two games different. Now is the problem: how many different games can be held in this ping pong street?
Input
The first line of the input contains an integer T(1<=T<=20), indicating the number of test cases, followed by T lines each of which describes a test case.
Every test case consists of N + 1 integers. The first integer is N, the number of players. Then N distinct integers a1, a2 … aN follow, indicating the skill rank of each player, in the order of west to east. (1 <= ai <= 100000, i = 1 … N).
Output
For each test case, output a single line contains an integer, the total number of different games.
Sample Input
1 3 1 2 3
Sample Output
1
题意:就是给你一组队列,让你在队列里面找三个数Xi,Xy,Xz(i<y<z)保证这三个数是非递减序列 或者是非递增序列
思路:用2个数组记录从前开始到i的逆序对数目以及正序对数目。用2个数组记录从后开始到i的逆序对数目以及正序对数目。
然后队列数目就是ans+=x1[i]*Y2[i]+x2[i]*Y1[i];
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
using namespace std;
int Y1[100100];
int Y2[100100];
int C[100100];
int W[100100];
int x1[100100];
int x2[100100];
int n=100100;
int lowbit(int i)
{
return i&-i;
}
void updata(int i,int val)
{
while(i<=n)
{
C[i]=C[i]+val;
i=i+lowbit(i);
}
}
int sum(int i)
{
int ret=0;
while(i>0)
{
ret =ret+C[i];
i=i-lowbit(i);
}
return ret;
}
int main()
{
int K;
int T;
scanf("%d",&T);
while(T--)
{
memset(C,0,sizeof(C));
memset(W,0,sizeof(W));
scanf("%d",&K);
for(int i=1;i<=K;i++)
{
scanf("%d",&W[i]);
int temp=sum(W[i]);
x1[i]=temp;
x2[i]=i-temp-1;
updata(W[i],1);
}
memset(C,0,sizeof(C));
int j=1;
for(int i=K;i>=1;i--,j++)
{
int temp=sum(W[i]);
Y1[i]=temp;
Y2[i]=j-temp-1;
updata(W[i],1);
}
long long ans=0;
for(int i=1;i<=n;i++)
{
ans+=x1[i]*Y2[i]+x2[i]*Y1[i];
}
printf("%lld\n",ans);
}
return 0;
}