#define DEFAULT_CAPACITY 3 typedef int Rank; class Fib { private: int f, g; public: Fib( int n ) { f = 1; g = 0; while( g < n ) next(); } inline int get() { return g; } inline int next() { g += f; f = g - f; return g; } inline int prev() { f = g - f; g -= f; return g; } }; template <typename T> class Vector { protected: Rank _size; int _capacity; T* _elem; inline void copyfrom( T const *A, Rank l, Rank r ) { _elem = new T[_capacity = ( r - l ) << 1]; _size = 0; while( l < r ) _elem[_size++] = A[l++]; } inline void expand() { if( _size < _capacity ) return; if( _capacity < DEFAULT_CAPACITY ) _capacity = DEFAULT_CAPACITY; T* oldElem = _elem; _elem = new T[_capacity <<= 1]; for( register int i = 0; i <= _size; ++i ) _elem[i] = oldElem[i]; delete [] oldElem; } inline void shrink() { if( _capacity < DEFAULT_CAPACITY << 1 ) return; if( _size << 2 > _capacity ) return; T* oldElem = _elem; _elem = new T[_capacity >>= 1]; for( register int i = 0; i <= _size; ++i ) _elem[i] = oldElem[i]; delete [] oldElem; } inline Rank fibsearch( T *A, T const &e, Rank l, Rank r ) { Fib fib( r - l ); while( l < r ) { while( r - l < fib.get() ) fib.prev(); Rank mid = l + fib.get() - 1; if( e < A[mid] ) r = mid; else if( A[mid] < e ) l = mid + 1; else return mid; } return -1; } inline Rank find( T const &e, Rank l, Rank r ) { while( ( l < r-- ) && e != _elem[r] ); return r; } public: Vector( int c = DEFAULT_CAPACITY, int s = 0, T v = 0 ) { _elem = new T[_capacity = c]; for ( _size = 0; _size < s; _elem[_size++] = v ); } Vector( T const* A, Rank n ) { copyFrom ( A, 0, n ); } Vector ( T const* A, Rank lo, Rank hi ) { copyFrom ( A, lo, hi ); } Vector ( Vector<T> const& V ) { copyFrom ( V._elem, 0, V._size ); } Vector ( Vector<T> const& V, Rank lo, Rank hi ) { copyFrom ( V._elem, lo, hi ); } ~Vector() { delete [] _elem; } Rank size() const { return _size; } bool empty() const { return !_size; } Rank find ( T const& e ) const { return find ( e, 0, _size ); } Rank find ( T const& e, Rank lo, Rank hi ) const; Rank search ( T const& e ) const { return ( 0 >= _size ) ? -1 : search ( e, 0, _size ); } Rank search ( T const& e, Rank lo, Rank hi ) const; T& operator [] ( Rank r ) const; Vector<T> & operator = ( Vector<T> const& ); inline T remove( Rank r ) { T e = _elem[r]; remove( r, r + 1 ); return e; } inline Rank insert( Rank r, T const &e ) { expand(); for( register int i = _size; r < i; --i ) _elem[i] = _elem[i - 1]; _elem[r] = e, ++_size; return r; } };