There are three kinds of methods for reverse order: 1, Discrete tree array, 2, line tree, 3, merge sort
Today did the next rokua of P1908 reverse order to;
1, the beginning of a tree-like array, has been re, and later in the discovery of their own has been neglected discretization.
#include <iostream>#include<algorithm>using namespacestd;Const intMAXN =400000+5; typedefLong LongLl;ll N,HASH[MAXN],BT[MAXN];structNode {intV,id;} A[maxn];ll Lowbit (ll x) {return(x) & (-x);}voidAdd (ll i,ll b) { while(i<=N) {Bt[i]+=b; I+=lowbit (i); }}ll Qu (ll i) {ll ans=0; while(i>0) {ans+=Bt[i]; I-=lowbit (i); } returnans;}BOOLCMP (node A,node b) {returna.v<B.V;}intMain () {CIN>>N; for(intI=1; i<=n; i++) {cin>>a[i].v; A[i].id=i; } sort (A+1, A +1+n,cmp); for(intI=1; i<=n;i++) {Hash[a[i].id]=i; } //The end of discretization;ll sum =0; for(intI=1; i<=n;i++) {sum+ = (-1-qu (Hash[i]));//the number of i-1, Quary (A[i]) prefixes and a[i] before the current tree arrayAdd (Hash[i],1);//a value of a[i] adds another} cout<<sum<<Endl; return 0;}
Inverse pairs + discrete tree array + ....