There is a data of height of 0 ...
The principle of reverse order in a tree-like array:
Add (h[j],1); The number of occurrences of the data with height of h[j] plus 1
SUM (h[j]);//I<j and H[i] <=h[j] The number of data occurrences and i-sum (H[j]) for i > J and H[i] > H[j] The number of occurrences of the data
Which is the inverse logarithm
#include"Cstdio"#include"CString"#defineLowbit (i) i& (-i)using namespacestd;Const intmaxn=1000105;//determined by the range of HtypedefLong LongLL;intBIT[MAXN];voidAddintIintx) { while(i<=MAXN) {Bit[i]+=x; I+=lowbit (i); }}intSuminti) { intans=0; while(i>0) {ans+=Bit[i]; I-=lowbit (i); } returnans;}intN;intH[MAXN];intT[MAXN]; LL S[MAXN];Long Longans;intMain () {scanf ("%d",&N); for(intI=0; i<n;i++) {scanf ("%d",&H[i]); H[i]++;//Note: The test data has a height of 0 childrent[i]=i-sum (h[i]); Add (H[i],1); S[i+1]=s[i]+ (i+1); } memset (bit,0,sizeof(bit)); for(inti=n-1; i>=0; i--) {T[i]+=sum (h[i]); Add (H[i]+1,1);//Note Add 1 This will remove the same heightans+=S[t[i]]; } printf ("%lld\n", ans); return 0;}
Blue Bridge Cup children in line (tree array for reverse order)