http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895
#include <iostream>#include<cstdio>#include<cstring>#include<string>using namespacestd;Const intMAXA =100000+5;intN;intC[maxa];intSuminti) { intRET =0; while(I >0) {ret+=C[i]; I-= i & (-i); } returnret;}voidAddintIintd) { while(I <= maxa-1) {C[i]+=D; I+ = i & (-i); }}intMain () {intT; int*a, *leftless, *rightless; scanf ("%d", &u); while(t--) {scanf ("%d", &N); A=New int[n]; Leftless=New int[n]; Rightless=New int[n]; for(intI=0; i<n; i++) {scanf ("%d", &A[i]); Leftless[i]= SUM (a[i]-1); Add (A[i],1); } memset (c,0,sizeof(c)); for(inti=n-1; i>=0; i--) {Rightless[i]= SUM (a[i]-1); Add (A[i],1); } Long LongRET =0; for(intI=0; i<n; i++) { //a[i] is distinct integers, so the opsite side of Leftless is LeftgreaterRET + = leftless[i] * 1LL * (n1-i-Rightless[i]); RET+ = (I-leftless[i]) * 1LL *Rightless[i]; } printf ("%lld\n", ret); Memset (c,0,sizeof(c)); Delete(a); Delete(leftless); Delete(rightless); } return 0;}
Uvalive-4329--ping Pong "tree-like array"