1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define MAXN 100005 5 #define M 20005 6 typedef __int64 LL ; 7 using namespace std; 8 LL n,max_num ; 9 LL s[M],a[M],b[M],c[MAXN]; 10 LL lowbit(LL x){ 11 return x&(-x); 12 } 13 LL sum(LL x){ 14 LL ret = 0; 15 while(x > 0){ 16 ret += c[x]; 17 x -= lowbit(x); 18 } 19 return ret; 20 } 21 void add(LL x, LL d){ 22 while(x<=max_num){ 23 c[x] += d; 24 x += lowbit(x); 25 } 26 } 27 int main(){ 28 LL t,i,j,ans; 29 cin>>t; 30 while(t--){ 31 cin>>n; 32 max_num = 0; 33 for(i = 1;i <= n ;i++){ 34 scanf("%I64d",&s[i]); 35 if(s[i]>max_num) 36 max_num = s[i]; 37 } 38 memset(c,0,sizeof(c)); 39 for(i = 1;i <= n ; i++){ 40 add(s[i],1); 41 a[i] = sum(s[i]-1); 42 } 43 memset(c,0,sizeof(c)); 44 for(i = n; i>0 ;i--){ 45 add(s[i],1); 46 b[i] = sum(s[i]-1); 47 } 48 ans = 0; 49 for(i = 1;i <= n ;i++){ 50 //cout<<a[i]<<" "<<b[i]<<endl; 51 ans = ans+a[i]*(n-i-b[i])+(i-a[i]-1)*b[i]; 52 } 53 cout<<ans<<endl; 54 } 55 return 0; 56 }
时间: 2024-10-05 11:14:40