Time Limits: 1000 ms Memory Limits: 65536 KB Detailed Limits
1 #include <bits/stdc++.h> 2 #define MAX 100000 3 int n; 4 int numbers[MAX+1]; 5 int position[MAX+1]; 6 struct fenwick { 7 int a[MAX+1]; 8 fenwick() { 9 memset( a, 0, sizeof a ); 10 } 11 int query( int X ) { 12 int ret = 0; 13 for( int x = X; x > 0; x -= x&-x ) 14 ret += a[x]; 15 return ret; 16 } 17 int sum( int lo, int hi ) { 18 return query( hi ) - query( lo-1 ); 19 } 20 void add( int X, int val ) { 21 for( int x = X; x <= n; x += x&-x ) 22 a[x] += val; 23 } 24 } alive; 25 int main( void ) { 26 scanf( "%d", &n ); 27 for( int i = 1; i <= n; ++i ) { 28 scanf( "%d", &numbers[i] ); 29 position[numbers[i]] = i; 30 alive.add( i, 1 ); 31 } 32 int mini = 1, maxi = n; 33 34 for( int i = 1; i <= n; ++i ) { 35 if( i%2 == 1 ) { 36 alive.add( position[mini], -1 ); 37 printf( "%d ", alive.sum( 1, position[mini] ) ); 38 ++mini; 39 } else { 40 alive.add( position[maxi], -1 ); 41 printf( "%d ", alive.sum( position[maxi], n ) ); 42 --maxi; 43 } 44 } 45 return 0; 46 }