我去,数组最大值 跟 数据最大值 没搞清楚,WA了好多次都看不出来错在哪。这种错误一旦犯了,小数据根本看不不出来。。
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 100005
int lowbit[N], low[N], a[N];
long long c[N], n;
void Init()
{
for (int i=1;i<N;i++)
lowbit[i]=i&(-i);
}
void init()
{
for (int i=0;i<N;i++)
c[i]=0;
}
void add(int x,int d)
{
while (x<N) {
c[x]+=d;
x+=lowbit[x];
}
}
long long sum(int x)
{
long long js=0;
while (x) {
js+=c[x];
x-=lowbit[x];
}
return js;
}
int main()
{
//freopen("1.in","r",stdin);
int T, n1;
long long ans;
cin>>T;
Init();
while (T--){
scanf("%d",&n1);
n=n1;
init();
for (int i=1;i<=n;i++) {
scanf("%d",&a[i]);
low[i]=sum(a[i]);
add(a[i],1);
}
init();
ans=0;
add(a[n],1);
for (int j=n-1;j>1;j--){
long long i=j, tmp=sum(a[i]);
ans+=low[i]*(n-i-tmp)+(i-1-low[i])*tmp;
add(a[i],1);
}
cout<<ans<<endl;
}
return 0;
}