HDU 2689 POJ 2299 树状数组 + 离散化
以前都是直接树状数组裸奔,昨天一个题数据量好大,过不了,没办法只能学离散化,今天A了第一个离散化的题
#include<stdio.h> #include<string.h> #include<stdlib.h> #define Max 500005 long long s,a[Max]; int n,max; struct e { int num,f; }N[Max]; int cmp( const void *a,const void *b ) { return ( ( e * )a ) -> num - ( ( e * )b ) -> num; } inline int lowbit( int x ) { return x & ( -x ); } void update( int x ) { while( x <= max ) { a[x]++; x += lowbit( x ); } } long long sum( int x ) { long long s = 0; while( x ) { s += a[x]; x -= lowbit( x ); } return s; } int main( ) { while( scanf( "%d",&n ) != EOF && n ) { max = s = 0; memset( a,0,sizeof( a ) ); for( int i = 1; i <= n; ++i ) { scanf( "%d",&N[i].num ); N[i].f = i; } qsort( N + 1,n,sizeof( N[1] ),cmp ); max = n; for( int i = 0; i < n; ++i ) { int x = N[i+1].f; s += i - sum( x ); update( x ); } printf( "%I64d\n",s ); } return 0; }
本人还是新手 ,转载请注明来自Lvsi‘s home