http://acm.hdu.edu.cn/showproblem.php?pid=5147
Test instructions: Ask how many such a four-tuple (a,b,c,d), satisfies the condition is 1<=a<b<c<d; aa<ab; ac<ad;
Idea: Use a tree-like array, from right to left to find out how many reverse-order number pairs are recorded in the R array, and then in the left-to-right to find out how many reverse number pairs exist in the L array before entering this number, then enumerate B on the line;
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMAXN 500015 #definell Long Long6 using namespacestd;7 8 intT;9 intN;Ten intA[MAXN]; One intC[MAXN]; A ll L[MAXN],R[MAXN],SUM[MAXN]; - - intLowbit (intx) the { - returnx&-x; - } - + voidInsertintXintv) - { + while(x<MAXN) A { atc[x]+=v; -x+=lowbit (x); - } - } - -ll Getsum (intx) in { -ll ans=0; to while(x>0) + { -ans+=C[x]; thex-=lowbit (x); * } $ returnans;Panax Notoginseng } - the intMain () + { Ascanf"%d",&t); the while(t--) + { -memset (SUM,0,sizeof(sum)); $Memset (A,0,sizeof(a)); $scanf"%d",&n); - for(intI=1; i<=n; i++) - { thescanf"%d",&a[i]); - }WuyiMemset (c,0,sizeof(c)); thesum[n+1]=0; - for(intI=n; i>=1; i--) Wu { -R[i]=getsum (N)-getsum (A[i]); AboutInsert (A[i],1); $sum[i]=sum[i+1]+R[i]; - } -Memset (c,0,sizeof(c)); - for(intI=1; i<=n; i++) A { +L[i]=getsum (a[i]-1); theInsert (A[i],1); - } $ll ans=0; the for(intI=2; i<=n; i++) the { theans+= (l[i]*sum[i+1]); the } -printf"%lld\n", ans); in } the return 0; the}
View Code
HDU 5147 Sequence II