http://acm.hdu.edu.cn/showproblem.php?pid=4453

普通的splay,但是出题人很无聊的给题目加上了很多限制,使得双向链表也可以处理,但是我还是比较喜欢splay这种数据结构,试了一下现在手写无压力。。

hdu4453_splay

  1 //By Lin
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #define maxn 100050
  6 using namespace std;
  7
  8 struct    SplayNode{
  9     SplayNode *fa,*ch[2];
 10     int    key,add,size;
 11     bool rev;
 12     SplayNode(int key = 0 ){
 13         fa = ch[0] = ch[1] = NULL;
 14         this->key = key;
 15         size = 1;
 16         add = 0 , rev = false;
 17     }
 18     void clear(){
 19         if ( ch[0] ) ch[0]->clear();
 20         if ( ch[1] ) ch[1]->clear();
 21         delete this;
 22     }
 23     void updata(){
 24         size = (ch[0]?ch[0]->size:0)+(ch[1]?ch[1]->size:0)+1;
 25     }
 26     void down(){
 27         if ( add ){
 28             if ( ch[0] ) ch[0]->key += add , ch[0]->add += add;
 29             if ( ch[1] ) ch[1]->key += add , ch[1]->add += add;
 30             add = 0;
 31         }
 32         if ( rev ) {
 33             swap( ch[0] , ch[1] );
 34             if ( ch[0] ) ch[0]->rev ^= 1;
 35             if ( ch[1] ) ch[1]->rev ^= 1;
 36             rev = false;
 37         }
 38     }
 39     SplayNode* maxnode(){
 40         down();
 41         if ( ch[1] ) return ch[1]->maxnode();
 42         return this;
 43     }
 44     void show(){
 45         if ( ch[0] ) ch[0]->show();
 46         printf("%d ", key );
 47         if ( ch[1] ) ch[1]->show();
 48     }
 49 }*root,*tmp;
 50 SplayNode* build(int data[],int l,int r,SplayNode* fa = NULL){
 51     int mid = (l+r)/2;
 52     SplayNode *ret = new SplayNode(data[mid]);
 53     ret->fa = fa;
 54     if ( l<mid ) ret->ch[0] = build(data,l,mid-1,ret);
 55     if ( mid<r ) ret->ch[1] = build(data,mid+1,r,ret);
 56     ret->updata();
 57     return ret;
 58 }
 59 void Rotate(SplayNode* tmp){
 60     SplayNode* y = tmp->fa;
 61     if ( tmp->fa = y->fa )
 62         y->fa->ch[y->fa->ch[1]==y] = tmp;
 63     int d = y->ch[0] == tmp;
 64     if ( y->ch[d^1] = tmp->ch[d] )
 65         tmp->ch[d]->fa = y;
 66     tmp->ch[d] = y;
 67     y->fa = tmp;
 68     y->updata();
 69     tmp->updata();
 70 }
 71 void Splay(SplayNode *tmp, SplayNode *y ) {
 72     while ( tmp->fa != y )
 73         if ( tmp->fa->fa == y ) Rotate(tmp);
 74         else {
 75             int d1 = tmp->fa->ch[0] == tmp , d2 = tmp->fa->fa->ch[0] == tmp->fa;
 76             if ( d1 == d2 ) Rotate(tmp->fa),Rotate(tmp);
 77             else Rotate(tmp),Rotate(tmp);
 78         }
 79 }
 80 SplayNode* Select(SplayNode* tmp ,int x){
 81     tmp->down();
 82     if ( tmp->ch[0] ){
 83         if ( tmp->ch[0]->size >= x ) return Select( tmp->ch[0] , x );
 84         x -= tmp->ch[0]->size;
 85     }
 86     if ( x == 1 ) return tmp;
 87     return Select( tmp->ch[1] , x-1 );
 88 }
 89 SplayNode* Join(SplayNode* l ,SplayNode* r ){
 90     SplayNode* tmp = l->maxnode();
 91     Splay( tmp , NULL );
 92     tmp->ch[1] = r;
 93     r->fa = tmp;
 94     tmp->updata();
 95     return tmp;
 96 }
 97 void    delet( int x ) {
 98     root = Select( root , x );
 99     Splay( root , NULL );
100     root->ch[0]->fa = root->ch[1]->fa = NULL;
101     root = Join( root->ch[0], root->ch[1] );
102 }
103 void    insert(int key ,int x ){
104     root = Select( root,x-1 );
105     Splay( root , NULL );
106     tmp = Select( root , x );
107     Splay( tmp , root );
108     tmp->ch[0] = new SplayNode(key);
109     tmp->ch[0]->fa = tmp;
110     tmp->updata();
111     root->updata();
112 }
113
114 int        n,m,K1,K2,data[maxn];
115 int        main(){
116     int tt = 0,x;
117     char s[50];
118     while ( ~scanf("%d%d%d%d", &n, &m, &K1, &K2 ) ) {
119         if ( n == 0 && m == 0 && K1 == 0 && K2 == 0 ) break;
120         for (int i = 1; i<=n; i++) scanf("%d", &data[i] );
121         root = build( data , 0 , n+1 );
122         printf("Case #%d:\n" , ++tt );
123         while ( m-- ) {
124             scanf("%s", s );
125             if ( s[0] == 'q' ) {
126                 root = Select( root , 2 );
127                 Splay( root , NULL );
128                 printf("%d\n" , root->key );
129             }else
130             if ( s[0] == 'r' ) {
131                 root = Select(root,1);
132                 Splay( root , NULL );
133                 tmp = Select(root,K1+2);
134                 Splay( tmp , root );
135                 root->ch[1]->ch[0]->rev ^= 1;
136             }else
137             if ( s[0] == 'i' ) {
138                 scanf("%d", &x );
139                 insert( x , 3 );
140             }else
141             if ( s[0] == 'd' ) delet(2); else
142             if ( s[0] == 'm' ) {
143                 scanf("%d", &x );
144                 if ( x == 1 ) {
145                     tmp = Select(root,root->size-1);
146                     int key = tmp->key;
147                     delet( root->size-1 );
148                     insert( key , 2 );
149                 }
150                 else {
151                     tmp = Select(root,2);
152                     int key = tmp->key;
153                     delet( 2 );
154                     insert( key , root->size );
155                 }
156             }else {
157                 scanf("%d", &x );
158                 root = Select( root , 1 );
159                 Splay( root , NULL );
160                 tmp = Select( root , K2+2 );
161                 Splay( tmp , root );
162                 tmp->ch[0]->add += x;
163                 tmp->ch[0]->key += x;
164             }
165         }
166         root->clear();
167     }
168     return 0;
169 }

转载于:https://www.cnblogs.com/lzqxh/archive/2012/11/20/2778609.html

【splay】hdu 4453 2012杭州赛区A题相关推荐

  1. 2013 年亚洲赛杭州赛区卡题总结

         今天这场比赛,真的打的很痛苦,从头卡到尾,实在是无语,哭死,这是一次严重的战略失误!!! 好吧!就先描述一下所有的比赛过程吧! 赛前安排,中场12:30无论如何必须把所有题看一遍,然后挑几题 ...

  2. hdu 4278 2012天津赛区网络赛 数学 *

    8进制转为10进制 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include ...

  3. hdu 4738 2013杭州赛区网络赛 桥+重边+连通判断 ***

    题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥,使得这n座岛不连通,求最少要派多少人去. 处理重边 边在遍历的时候,第一个返回的一定是之前去的边,所以 ...

  4. 2021 ICPC 沈阳赛区J题 Luggage Lock

    2021 ICPC 沈阳赛区J题 Luggage Lock 题意 有TTT组样例,其中每组样例为: 给定一个密码为b0b1b2b3b_0b_1b_2b_3b0​b1​b2​b3​的密码锁,已知当前密码 ...

  5. “温莎当下 麦克成风”2021赛季杭州赛区决赛精彩收官

    9月11日,"温莎当下 麦克成风"2021赛季杭州赛区决赛在中粮大悦城水秀广场如期举行.绚烂的灯光.华丽的舞台.选手优秀的唱功.粉丝团热力的应援,无疑在打造一场巅峰盛宴. 据悉,一 ...

  6. 2012年职称计算机,职称计算机考试2012年word2003真题试题

    职称计算机考试2012年word2003真题试题 导语:为帮助考生有效备考2017年职称计算机考试,百分网小编整理了职称计算机历年真题,希望对您通过职称计算机考试有所帮助! 1 设置显示操作向导. 2 ...

  7. 2012网赛杭州赛区

    1002 arrest 有k个警察在0点按顺序遍历1到n去抓小偷, 这样构图时就要对编号小的连向编号大的, 之前要floyd处理. 我赛后的构图:对每个点的遍历有个限制是必须是1次, 由于是费用流, ...

  8. HDU 4292 Food (成都赛区网络赛第五题,拆点网络流)

      题意:跟POJ3281基本上一样的拆点网络流. 建图:建一超级源点和一超级汇点,源点与食物相连,边权为其数量,汇点与饮料相连,边权也为其数量,把人分成两个点,之间的边权为1.每个人与之需要的食物和 ...

  9. HDU 4565So Easy!2012长沙邀请赛A题(共轭构造+矩阵的快速幂)

    So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

最新文章

  1. android 蓝牙 鼠标 app_Razer 雷蛇 那伽梵蛇 Pro 专业版 无线蓝牙鼠标 899元
  2. 如何彻底关闭windows update
  3. 软件工程 之 动物世界
  4. CodeForces - 1326D2 Prefix-Suffix Palindrome (Hard version)(马拉车/回文自动机)
  5. 客户让无数销售员卑躬屈膝的四大陷阱
  6. 聚焦消费和产业两大物联网赛道,腾讯连连全新升级
  7. ieee754标准_比特与信息在计算机中的表示及补码和浮点数的IEEE 754标准
  8. 【图像隐写】基于matlab DWT数字水印嵌入+攻击+提取【含Matlab源码 1759期】
  9. 【UML】聊聊系统建模
  10. 华为交换机端口vlan详解
  11. 用户帐户控制---为了对电脑进行保护,已经阻止此应用。---管理员已阻止你运行此应。有关详细信息,请与管理员联系。
  12. 基于matlab特征脸Eigenface算法的实现
  13. linux裸设备详解,Linux裸设备管理详解(原创)
  14. 捷信Q1经营大幅下滑,净利润0.3亿元,不良率走高
  15. linux关闭桌面快捷键设置,在XFCE4桌面上自定义键盘快捷键的方法
  16. wath修改data中的值后更新
  17. 鸿蒙系统适配的电视,搭载鸿蒙系统的荣耀智慧屏电视适配app太少?网友:感觉上当了...
  18. MAC删除多余的声音驱动文件
  19. centos 关于“Error: Failed to download metadata for repo ‘appstream‘” 问题
  20. 单位冲激函数与单位阶跃函数

热门文章

  1. Fedora 8安装非官方compiz-fusion
  2. java实现arp断网攻击,可攻击局域网内所有的主机
  3. Linux内存管理之内存管理单元(MMU)(二)
  4. kafka基础之核心概念
  5. c语言实现协议层层消息,纯C语言实现面向对象分析与示例分享.pdf
  6. (70)FPGA资源优化有哪些方法?手写FIFO代替BRAM
  7. (84)FPGA面试题-多bit跨时钟域
  8. (21)VHDL实现减法器
  9. php关于apache配置,关于PHP和apache的配置
  10. STM32F103:二.(2)串口控制LED