Chinaunix首页 | 论坛 | 博客
  • 博客访问: 57277
  • 博文数量: 16
  • 博客积分: 306
  • 博客等级: 二等列兵
  • 技术积分: 162
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-20 15:08
文章分类

全部博文(16)

文章存档

2013年(1)

2012年(15)

我的朋友

分类: C/C++

2012-09-18 19:14:55


  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cmath>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <set>
  11. using namespace std ;

  12. #pragma comment(linker, "/STACK:1024000000,1024000000")
  13. #define KEY_TREE ch[ ch[root][1] ][0]
  14. #define lson l , m , rt<<1
  15. #define rson m+1 , r , rt<<1|1
  16. #define lowbit(x) (x&(-x))
  17. #define pb push_back

  18. template<typename T> T Max( T a , T b ) { return a > b ? a : b ; }
  19. template<typename T> T Min( T a , T b ) { return a < b ? a : b ; }
  20. template<typename T> T Abs( T A ) { return A > 0 ? A : -1*A ; }
  21. template<typename T>void Swap( T &a , T &b) { T tmp = a ; a=b ; b = tmp ; }

  22. typedef pair<int , int> pii ;
  23. typedef long long llong ;

  24. const llong LINF = 0x3FFFFFFFFFFFFFFLL ;
  25. const int INF = 0X3FFFFFFF ;
  26. const int MAXN = 1000005 ;
  27. int a[MAXN] ;

  28. class SplayTree
  29. {
  30.     public :
  31.         void PushUp( int rt ) ;
  32.         void PushDown( int rt ) ;

  33.         void Build( int &rt , int left, int right , int fa ) ;
  34.         void NewNode( int &rt , int nd_val , int fa ) ;
  35.         void Init(const int &n) ;

  36.         void Rotate( int x , int col ) ;
  37.         void Splay( int rt , int goal ) ;
  38.         void RotateTo( int k , int goal ) ;

  39.         void Reversal( int pos , int k ) ;
  40.         void Insert( int pos , int nd_val ) ;
  41.         void Delete( int pos , int k ) ;

  42.         void InOrder( int rt , int &cnt ) ;
  43.         void Print( const int &n ) ;

  44.     private :
  45.         int ch[MAXN][2] ;
  46.         int val[MAXN] , sz[MAXN] , pre[MAXN] ;
  47.         bool rev[MAXN] ;
  48.         int root , top ;
  49. } spt ;

  50. inline void SplayTree::PushUp( int rt )
  51. {
  52.     int l = ch[rt][0] , r = ch[rt][1] ; //************
  53.     sz[rt] = sz[l] + sz[r] +1 ;
  54. }

  55. inline void SplayTree::PushDown( int rt )
  56. {
  57.     if ( rev[rt] )
  58.     {
  59.         int &l = ch[rt][0] , &r = ch[rt][1] ;
  60.         rev[l] ^= 1 , rev[r] ^= 1 ;
  61.         Swap( l , r ) ;
  62.         rev[rt] = 0 ;
  63.     }
  64. }

  65. inline void SplayTree::NewNode(int &rt , int nd_val , int fa)
  66. {
  67.     rt = ++top ;
  68.     rev[rt] = ch[rt][0] = ch[rt][1] = 0 ;
  69.     val[rt] = nd_val ;
  70.     sz[rt] = 1 ;
  71.     pre[rt] = fa ;
  72. }

  73. void SplayTree::Build( int &rt , int left , int right , int fa )
  74. {
  75.     if ( left > right ) return ;
  76.     int m = ( left + right ) >> 1;
  77.     NewNode( rt , a[m] ,fa ) ;
  78.     Build( ch[rt][0] , left , m-1 , rt ) ;
  79.     Build( ch[rt][1] , m+1 , right , rt ) ;
  80.     PushUp( rt ) ;
  81. }

  82. inline void SplayTree::Init( const int &n )
  83. {
  84.     root = top = 0 ;
  85.     ch[root][0] = ch[root][1] = rev[root] = pre[root] = sz[root] = 0 ;
  86.     NewNode( root , -100 , 0 ) ;
  87.     NewNode( ch[root][1] , -1, root ) ;
  88.     PushUp( root ) ;
  89.     Build( KEY_TREE , 1 , n , ch[root][1] ) ;
  90.     PushUp( ch[root][1] ) ;
  91.     PushUp( root ) ;
  92. }

  93. inline void SplayTree::Rotate( int x , int col )
  94. {
  95.     int y = pre[x] ;
  96.     PushDown( y ) ;
  97.     PushDown( x ) ;
  98.     ch[y][!col] = ch[x][col] ;
  99.     pre[ ch[x][col] ] = y ;
  100.     if ( pre[y] )
  101.         ch[ pre[y] ][ ch[ pre[y] ][1] == y ] = x ; //***************
  102.     pre[x] = pre[y] ;
  103.     pre[y] = x ;
  104.     ch[x][col] = y ;
  105.     PushUp( y ) ;
  106. }

  107. void SplayTree::Splay( int rt , int goal )
  108. {
  109.     PushDown( rt ) ;
  110.     while ( pre[rt] != goal )
  111.     {
  112.         if ( pre[ pre[rt] ] == goal )
  113.             Rotate( rt , ch[ pre[rt] ][0] == rt ) ;
  114.         else
  115.         {
  116.             int y = pre[rt] ;
  117.             int col = ( ch[ pre[y] ][0] == y ) ;

  118.             if ( ch[y][col] == rt ) //***********
  119.                 Rotate( rt , !col ) ;
  120.             else
  121.                 Rotate( y , col ) ;
  122.             Rotate( rt , col ) ;
  123.         }
  124.     }
  125.     PushUp( rt ) ;
  126.     if ( goal == 0 ) root = rt ;
  127. }

  128. inline void SplayTree::RotateTo(int k , int goal )
  129. {
  130.     int x = root ;
  131.     PushDown( x ) ;
  132.     while ( sz[ ch[x][0] ] +1 != k )
  133.     {
  134.         if ( sz[ ch[x][0] ] >= k )
  135.             x = ch[x][0] ;
  136.         else
  137.         {
  138.             k = k - sz[ ch[x][0] ] - 1 ;
  139.             x = ch[x][1] ;
  140.         }
  141.         PushDown(x) ;
  142.     }
  143.     Splay( x , goal ) ;
  144. }

  145. inline void SplayTree::Reversal( int pos , int k )
  146. {
  147.     RotateTo( pos , 0 ) ;
  148.     RotateTo( pos+k+1 , root ) ;
  149.     rev[KEY_TREE] ^= 1;
  150. }

  151. inline void SplayTree::Insert( int pos , int nd_val )
  152. {
  153.     RotateTo( pos , 0 ) ;
  154.     RotateTo( pos+1 , root ) ;
  155.     NewNode( KEY_TREE , nd_val , ch[root][1] ) ;
  156.     PushUp( ch[root][1] ) ;
  157.     PushUp( root ) ;
  158. }

  159. inline void SplayTree::Delete( int pos , int k )
  160. {
  161.     RotateTo( pos , 0 ) ;
  162.     RotateTo( pos+k+1 , root ) ;
  163.     KEY_TREE = 0 ;
  164.     PushUp( ch[root][1] ) ;
  165. }

  166. void SplayTree::InOrder( int rt , int &cnt )
  167. {
  168.     if ( rt == 0 ) return ;
  169.     PushDown(rt) ; //*************
  170.     InOrder( ch[rt][0] , cnt ) ;
  171.     if ( cnt ) putchar(' ') ;
  172.     printf("%d" , val[rt] ) ;
  173.     InOrder( ch[rt][1] , ++cnt ) ;
  174. }

  175. inline void SplayTree::Print( const int &n )
  176. {
  177.     RotateTo( 1 , 0 ) ;
  178.     RotateTo( n+2 , root ) ;
  179.     int cnt = 0 ;
  180.     InOrder( KEY_TREE , cnt ) ;
  181.     puts("") ;
  182. }

  183. int main()
  184. {
  185.     int T ;
  186.     scanf( "%d", &T ) ;
  187.     for ( int nca = 1; nca <= T ; nca++ )
  188.     {
  189.         int n ;
  190.         scanf("%d", &n) ;
  191.         for ( int i = 1 ; i <= n; i++ )
  192.             scanf("%d", a+i) ;

  193.         spt.Init( n ) ;
  194.         char opt[20] ;
  195.         const int k = 1 ;
  196.         int posl , posr , Q ;
  197.         scanf( "%d%d%d", &posl , &posr , &Q ) ;

  198.         while ( Q-- )
  199.         {
  200.             scanf( "%s" , opt ) ;

  201.             if ( opt[0] == 'M' && opt[4] == 'L' )
  202.             {
  203.                 scanf( "%s" , opt ) ;
  204.                 if ( opt[0] == 'L' ) posl-- ;
  205.                 else posr-- ;
  206.             }
  207.             else if ( opt[0] == 'M' && opt[4] == 'R' )
  208.             {
  209.                 scanf( "%s" , opt ) ;
  210.                 if ( opt[0] == 'L' ) posl++ ;
  211.                 else posr++ ;
  212.             }
  213.             else if ( opt[0] == 'I' )
  214.             {
  215.                 scanf( "%s" , opt ) ;
  216.                 scanf( "%d" , &a[1] ) ;
  217.                 if ( opt[0] == 'L' ) spt.Insert( posl , a[1] ) ;
  218.                 else spt.Insert( posr+1 , a[1] ) ;
  219.                 n++ , posr++ ;
  220.             }
  221.             else if ( opt[0] == 'R' )
  222.             {
  223.                 spt.Reversal( posl , posr- posl+1 ) ;
  224.             }
  225.             else if ( opt[0] == 'D' )
  226.             {
  227.                 scanf( "%s" , opt ) ;
  228.                 if ( opt[0] == 'L' )
  229.                     spt.Delete( posl , 1 ) ;
  230.                 else spt.Delete( posr , 1 ) ;
  231.                 n-- , posr-- ;
  232.             }

  233.         }
  234.         spt.Print( n ) ;
  235.     }
  236.     return 0 ;
  237. }

 
阅读(1716) | 评论(0) | 转发(0) |
0

上一篇:我的纸质书目

下一篇:HDU4117

给主人留下些什么吧!~~