Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1786 Accepted Submission(s): 644
#include <iostream>
#include <stdio.h>
#include <string.h>
#define Y 100003
#include <algorithm>
using namespace std;
int c[Y];
int low[Y];
int a[20003],b[20003];
void updata(int i)
{
for(;i<=100000;i+=low[i])
c[i]+=1;
}
int rs(int i)
{
int sum;
for(sum=0;i>0;i-=low[i])
sum+=c[i];
return sum;
}
int n;
int bf(int x)
{
int l=1,r=n,m;
while(l<=r)
{
m=(l+r)>>1;
if(b[m]>x) r=m-1;
else if(b[m]<x) l=m+1;
else return m;
}
}
int main()
{
int i,k;
for(i=1;i<=100000;i++) low[i]=i&-i;
int T;
__int64 sum;
int lmin,rmin,lmax,rmax;
scanf("%d",&T);
while(T--)
{
sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]),b[i]=a[i];
sort(b+1,b+n+1);
memset(c,0,sizeof(c));//我把sizeof(c)写成sizeof(0),坑爹呀、、、
for(i=1;i<=n;i++)
{
k=bf(a[i]);
lmin=rs(a[i]);
lmax=i-1-lmin;
rmin=k-1-lmin;
rmax=n-k-lmax;
sum+=lmin*rmax+lmax*rmin;
updata(a[i]);
}
printf("%I64d\n",sum);
}
return 0;
}