Tree-like arrays--discretization of inverse pairs

Source: Internet
Author: User

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 #definell Long Long6 #defineMAX 5000057 using namespacestd;8 9 structNodeTen { One     intVal,pos; A     BOOL operator< (Constnode& A)Const -     { -         returnval<A.val; the     } - }d[max]; - intN,tree[max],p[max]; -  +InlineintLowbit (intx) - { +     returnx& (-x); A } at  - voidADD (intPosintx) - { -     intI=Pos; -      for(; i<=n;i+=lowbit (i)) -     { intree[i]+=x; -     } to } +  -ll Getsum (intN) the { *ll ans=0; $      for(intI=n;i>0; i-=lowbit (i))Panax Notoginseng     { -ans+=Tree[i]; the     } +     returnans; A } the  + intMain () - { $      while(~SCANF ("%d", &n) &&N) $     { -Memset (P,0,sizeof(P)); -memset (Tree,0,sizeof(tree)); the          for(intI=1; i<=n;i++) -         {Wuyiscanf"%d",&d[i].val); thed[i].pos=i; -         } WuSort (d+1, d+n+1); -         //discretization of the original array; stored in the new array p, is the one by one mapping of the source array; About         intnum=1; $p[d[1].pos]=1; -          for(intI=2; i<=n;i++) -         { -             if(d[i].val==d[i-1].val) Ap[d[i].pos]=num; +             Else thep[d[i].pos]=++num; -         } $ll ans=0; the          for(intI=1; i<=n;i++) the         { theADD (P[i],1); theans+=i-getsum (P[i]); -             /*Getsum asked for a number of values in front of itself when inserting this value, in inserted in the order of entry, the size is discrete; the to the present altogether inserted I number, inserted in front of him is the order than he leaned forward, the value is smaller than him, the the latter is the order than he is small but the value is bigger than him, also and he formed the number of reverse number pairs;*/ About         } theprintf"%lld\n", ans); the          the     } +  -     return 0; the}

Tree-like arrays--discretization of inverse pairs

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.