After reading the questions before the class, I thought about the structure in the class. After the class is over 10 minutes, I just handed in A... Okay... This question is so weak!
Question:
Returns the number of reverse orders in a series ....
You can use coordinates for discretization. Then... update the tree array from a large number... sum the query time, and obtain the large number before the number.
Water burst... 1Y
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;struct node{ int val,pos;}a[555555];int b[555555],c[555555];bool cmp( node a,node b ){ return a.val>b.val; }int lowbit( int x ){ return x&-x; }int n;int sum( int x ){ int rec=0; while( x ) { rec+=c[x]; x-=lowbit(x); } return rec;}void add( int x,int val ){ while( x<=n ) { c[x]+=val; x+=lowbit(x); }}int main(){ while( scanf("%d",&n)!=EOF ) { memset( c,0,sizeof(c) ); if( n==0 )break; for( int i=1;i<=n;i++ ) { scanf( "%d",&a[i].val ); a[i].pos=i; } sort( a+1,a+n+1,cmp ); for( int i=1;i<=n;i++ ) b[a[i].pos]=i; __int64 ans=0; for( int i=1;i<=n;i++ ) { ans+=sum(b[i]-1); add(b[i],1); } printf( "%I64d\n",ans ); } return 0;}