1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineMAXN 1000055 #defineM 200056 typedef __int64 LL;7 using namespacestd;8 LL N,max_num;9 LL S[M],A[M],B[M],C[MAXN];Ten ll Lowbit (ll x) { One returnx& (-x); A } - ll sum (ll x) { -LL ret =0; the while(X >0){ -RET + =C[x]; -X-=lowbit (x); - } + returnret; - } + voidAdd (ll x, ll D) { A while(x<=max_num) { atC[X] + =D; -X + =lowbit (x); - } - } - intMain () { - LL T,i,j,ans; inCin>>T; - while(t--){ toCin>>N; +Max_num =0; - for(i =1; I <= N; i++){ thescanf"%i64d",&s[i]); * if(s[i]>max_num) $Max_num =S[i];Panax Notoginseng } -Memset (c,0,sizeof(c)); the for(i =1; I <= N; i++){ +Add (S[i],1); AA[i] = SUM (s[i]-1); the } +Memset (c,0,sizeof(c)); - for(i = n; i>0; i--){ $Add (S[i],1); $B[i] = SUM (s[i]-1); - } -Ans =0; the for(i =1; I <= N; i++){ - //cout<<a[i]<< "" <<b[i]<<endl;WuyiAns = ans+a[i]* (N-i-b[i]) + (i-a[i]-1)*B[i]; the } -cout<<ans<<Endl; Wu } - return 0; About}
Hdu 2492 tree-like array Ping pong