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