Pre-processing each number in front of it is smaller than it has a few, than he has a few, behind him a few smaller, there are several than his big.
Figure out how many pairs are likely to be produced, and then subtract more of the answers.
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<iostream>using namespaceStd;typedefLong LongLL;Const DoublePi=acos (-1.0), eps=1e-8;voidFile () {freopen ("D:\\in.txt","R", stdin); Freopen ("D:\\out.txt","W", stdout);} InlineintRead () {Charc = GetChar (); while(!isdigit (c)) C =GetChar (); intx =0; while(IsDigit (c)) {x = x *Ten+ C-'0'; c =GetChar ();} returnx;}Const intmaxn=50000+Ten;intN,A[MAXN],B[MAXN],C[MAXN];intLMAX[MAXN],LMIN[MAXN],RMAX[MAXN],RMIN[MAXN];intLowbit (intx) {returnx& (-x);}intSumintP) {intres=0; while(p) res=res+c[p],p=p-lowbit (p);returnRes;}voidUpdateintP) { while(p<=n) c[p]=c[p]+1, p=p+lowbit (P);}intMain () { while(~SCANF ("%d",&N)) { for(intI=1; i<=n;i++) scanf ("%d", &a[i]), b[i-1]=A[i]; Sort (b, B+ N);intSZ = unique (b, B + N)-b; for(intI=1; i<=n;i++) A[i]=lower_bound (b, B + sz, a[i])-b+1; Memset (c,0,sizeofc); for(intI=1; i<=n;i++) {Lmin[i]=sum (a[i]-1); Lmax[i]=sum (N)-sum (a[i]); update (A[i]);} LL S=0; for(intI=1; i<=n;i++) s=s+Lmax[i]; LL ans=0; for(intI=1; i<=n;i++) ans=ans+ (LL) (S-lmax[i]) *(LL) lmin[i]; Memset (c,0,sizeofc); for(inti=n;i>=1; i--) {Rmin[i]=sum (a[i]-1); Rmax[i]=sum (N)-sum (a[i]); update (A[i]);} for(intI=1; i<=n;i++) ans=ans-(LL) rmax[i]* (LL) rmin[i]-(LL) rmin[i]* (LL) lmin[i]-(LL) rmax[i]*(LL) lmax[i]; printf ("%lld\n", ans); } return 0;}
HDU 5792 World is exploding