标签:
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namespace std; int an[51000]; int s[110000]; const int N = 109999; int lb(int x) { return x&(-x); } void add(int i) { if(i>N) return; s[i]++; add(i+lb(i)); } int get(int i) { if(i == 0)return 0; return s[i]+get(i-lb(i)); } int taa[51000],tab[51000],tba[51000],tbb[51000]; int main() { int z; cin>>z; while(z--) { int n,i,j,k; cin>>n; memset(s,0,sizeof(s)); memset(taa,0,sizeof(taa)); memset(tab,0,sizeof(tab)); for(i = 1;i<=n;i++) scanf("%d",&an[i]); for(i = 1;i<=n;i++) { taa[i] = get(an[i]-1); tab[i] = i-1-get(an[i]); add(an[i]); } memset(s,0,sizeof(s)); memset(tba,0,sizeof(tba)); memset(tbb,0,sizeof(tbb)); for(i = n;i>=1;i--) { tba[i] = get(an[i]-1); tbb[i] = n-i-get(an[i]); add(an[i]); } long long ans = 0; for(i = 1;i<=n;i++) { ans += taa[i]*tbb[i]+tab[i]*tba[i]; ans += (i-1-taa[i]-tab[i])*(n-i)+(taa[i]+tab[i])*(n-i-tba[i]-tbb[i]); } cout<<ans<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/wos1239/p/4564554.html