Advertisement
LinKin

Implicit Treap

Apr 11th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. /*
  2.  * Implicit Treap
  3.  * all the index is 1 based
  4.  */
  5. struct NODE{
  6.     long Value,Prior,Cnt;
  7.     bool Rev;
  8.     NODE *l, *r;
  9.     NODE( long Value = 0,long Prior = 0 ):Value( Value ),Prior( Prior ),Cnt(1),Rev( false ), l( NULL ), r( NULL ){}
  10. };
  11.  
  12. typedef NODE* P_NODE;
  13.  
  14. class TREAP{
  15.     P_NODE Root;
  16.     vector<NODE> Pool;
  17.     long PoolPointer;
  18.  
  19.     P_NODE Create( long Value,long Prior = rand( ) ){
  20.         P_NODE ret = &Pool[PoolPointer++];
  21.         *ret = NODE( Value,Prior );
  22.         return ret;
  23.     }
  24.     long Count( P_NODE t ){
  25.         return t ? t->Cnt: 0;
  26.     }
  27.     /* Lazy to reverse the child */
  28.     void Push( P_NODE t ){
  29.         if( t && t->Rev ){
  30.             t->Rev = false;
  31.             swap( t->l ,t->r );
  32.             if( t->l ) t->l->Rev ^= true;
  33.             if( t->r ) t->r->Rev ^= true;
  34.         }
  35.     }
  36.     void Update( P_NODE t ){
  37.         if( t ){
  38.             Push( t );
  39.             t->Cnt = 1 + Count(t->l ) + Count(t->r );
  40.         }
  41.     }
  42.     void Split( P_NODE t, long Pos, P_NODE &l, P_NODE &r ,long Add = 0 ){
  43.         if( !t ) return void( l = r = NULL );
  44.         Push( t );
  45.         long Cur_Pos = Add + Count( t->l ) + 1;
  46.         if( Pos <= Cur_Pos ) Split( t->l, Pos, l, t->l,Add ), r = t;
  47.         else Split( t->r, Pos, t->r, r , Add + 1 + Count( t->l ) ), l = t;
  48.         Update( t );
  49.     }
  50.     void Merge( P_NODE &t, P_NODE l, P_NODE r ){
  51.         Push( l ),Push( r );
  52.         if( !l or !r ) t = l?l:r;
  53.         else if( l->Prior > r->Prior ) Merge(l->r, l->r, r ), t = l;
  54.         else Merge(r->l, l, r->l ), t = r;
  55.         Update( t );
  56.     }
  57.     void Insert( P_NODE &t, P_NODE it ,long Pos ){
  58.         if( !t ) return void( t = it );
  59.         P_NODE l,r;
  60.         Split( t,Pos, l, r );
  61.         Merge( t,l,it );
  62.         Merge( t,t,r );
  63.         Update( t );
  64.     }
  65.     void Erase( P_NODE &t, long Pos ,long Add = 0 ){
  66.         if( !t ) return;
  67.         P_NODE l,r;
  68.         Split( t,Pos,l,t );
  69.         Split( t,2,t,r );
  70.         Merge( t,l,r );
  71.         Update( t );
  72.     }
  73.     void Output( P_NODE t ){
  74.         if( !t ) return;
  75.         Push( t );
  76.         Output( t->l );
  77.         printf("%ld\n",t->Value );
  78.         Output( t->r );
  79.     }
  80. public:
  81.     TREAP( long PoolSize ) : Root( NULL ), PoolPointer( 0 ){
  82.         srand(time(NULL ) );
  83.         Pool.resize( PoolSize );
  84.     }
  85.     /* Count tot number of node */
  86.     long COUNT( ){
  87.         return Count( Root );
  88.     }
  89.     /* Insert a value in a Pos th position */
  90.     void INSERT( long Pos,long Value ){
  91.         Insert( Root,Create( Value ),Pos );
  92.     }
  93.     /* Erase node from pos */
  94.     void ERASE( long Pos ){
  95.         Erase( Root,Pos );
  96.     }
  97.     /* Reverse a range l -> r */
  98.     void REVERSE( long l, long r){
  99.         P_NODE t1,t2,t3;
  100.         Split( Root,l,t1,t2 );
  101.         Split( t2,(r-l+1)+1,t2,t3 );
  102.         t2->Rev ^= true;
  103.         Merge( Root,t1,t2 );
  104.         Merge( Root,Root,t3 );
  105.     }
  106.     void OUTPUT( void ){
  107.         Output( Root );
  108.     }
  109. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement