Blue Bridge Cup children in line (tree array for reverse order)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.