HDU 4286 Data Handler 双向链表/Splay
Data Handler
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4286
Description
In one day, the company gives you many commands to handle these data, so you should finish them one by one. At the beginning, there are N data, and hand "L" and "R" are in some positions. Each command is one the following formats:
(1)MoveLeft L/R: it means that you should move the hand "L"/"R" left one data unit;

(2)MoveRight L/R: it means that you should move the hand "L"/"R" right one data unit;
(3)Insert L X: it means that you should insert the data that contains X at the right of the hand "L";
(4)Insert R X: it means that you should insert the data that contains X at the left of the hand "R";
(5)Delete L: it means that you should delete the one data at the right of the hand "L";
(6)Delete R: it means that you should delete the one data at the left of the hand "R";
(7)Reverse: it means that you should reverse all the data between hand "L" and hand "R".
After finish all the commands, you should record all the data from left to right. So please do it.
Input
The first line contains an integer T(1<=T<=10), the number of test cases.
Then T test cases follow. For each test case, the first line contains an integer N(1<=N<=500000), the number of data at the beginning. The second line contains N integers, means the integer in each data, from left to right. The third line contains two integers L and R (1<=L<=R<=N), the positions of hand "L" and hand "R". It means that hand "L" is at the left of the L-th data and hand "R" is at the right of the R-th data. The fourth line contains one integer M(1<=M<=500000), the number of commands. Then M lines follow, each line contains a command in the above format. All the integers in the data will in range [-10000,10000].
It is guaranteed that there are always some data between hand "L" and "R", and if the hand is at the left/right end of the data line, it will not receive the command MoveLeft/MoveRight.
Because of large input, please use scanf instead of cin.
Output
For each test case, output the integers in the data from left to right in one line, separated in a single space.
Because of large output, please use printf instead of cout.
Sample Input
2
5
1 2 3 4 5
1 5
5
MoveLeft R
Insert R 6
Reverse
Delete R
Insert L 7
5
6536 5207 2609 6604 -4046
1 3
5
Delete L
Insert R -9221
Reverse
Delete L
MoveRight L
Sample Output
7 6 4 3 2 5
2609 5207 6604 -4046
HINT
题意
翻转,删除,插入操作
题解:
splay或者双端队列,但是我智商太低了,所以splay过不了
代码
- #include<cstdio>
- #include<cstring>
- using namespace std;
- struct node
- {
- int value;
- int pre,next;
- } num[];
- int l,ll,r,rr,cnt;
- void MoveLefe()
- {
- char ch[];
- scanf("%s",ch);
- if(ch[]=='L')
- {
- if(num[ll].pre==l)
- {
- l=ll;
- ll=num[ll].next;
- }
- else
- {
- l=ll;
- ll=num[ll].pre;
- }
- }
- else
- {
- if(num[r].pre==rr)
- {
- rr=r;
- r=num[r].next;
- }
- else
- {
- rr=r;
- r=num[r].pre;
- }
- }
- }
- void MoveRight()
- {
- char ch[];
- scanf("%s",ch);
- if(ch[]=='L')
- {
- if(num[l].pre==ll)
- {
- ll=l;
- l=num[l].next;
- }
- else
- {
- ll=l;
- l=num[l].pre;
- }
- }
- else
- {
- if(num[rr].pre==r)
- {
- r=rr;
- rr=num[rr].next;
- }
- else
- {
- r=rr;
- rr=num[rr].pre;
- }
- }
- }
- void Insert()
- {
- char ch[];
- int value;
- scanf("%s%d",ch,&value);
- if(ch[]=='L')
- {
- num[cnt].pre=ll;
- num[cnt].next=l;
- if(num[ll].pre==l) num[ll].pre=cnt;
- else num[ll].next=cnt;
- if(num[l].pre==ll) num[l].pre=cnt;
- else num[l].next=cnt;
- l=cnt++;
- num[l].value=value;
- }
- else
- {
- num[cnt].pre=rr;
- num[cnt].next=r;
- if(num[rr].pre==r) num[rr].pre=cnt;
- else num[rr].next=cnt;
- if(num[r].pre==rr) num[r].pre=cnt;
- else num[r].next=cnt;
- r=cnt++;
- num[r].value=value;
- }
- }
- void Delete()
- {
- char ch[];
- int next;
- scanf("%s",ch);
- if(ch[]=='L')
- {
- if(num[l].pre==ll) next=num[l].next;
- else next=num[l].pre;
- if(num[ll].pre==l) num[ll].pre=next;
- else num[ll].next=next;
- if(num[next].pre==l) num[next].pre=ll;
- else num[next].next=ll;
- l=next;
- }
- else
- {
- if(num[r].pre==rr) next=num[r].next;
- else next=num[r].pre;
- if(num[rr].pre==r) num[rr].pre=next;
- else num[rr].next=next;
- if(num[next].pre==r) num[next].pre=rr;
- else num[next].next=rr;
- r=next;
- }
- }
- void Reverse()
- {
- if(num[r].pre==rr) num[r].pre=ll;
- else num[r].next=ll;
- if(num[l].pre==ll) num[l].pre=rr;
- else num[l].next=rr;
- if(num[ll].pre==l) num[ll].pre=r;
- else num[ll].next=r;
- if(num[rr].pre==r) num[rr].pre=l;
- else num[rr].next=l;
- l=l^r;
- r=l^r;
- l=l^r;
- }
- void out(int n)
- {
- bool first=true;
- int pre=;
- for(int i=num[].next;i!=n+;)
- {
- if(first)
- {
- printf("%d",num[i].value);
- first=false;
- }
- else printf(" %d",num[i].value);
- if(num[i].next!=pre)
- {
- pre=i;
- i=num[i].next;
- }
- else
- {
- pre=i;
- i=num[i].pre;
- }
- }
- printf("\n");
- }
- int main()
- {
- int cas,n,m;
- char s[];
- scanf("%d",&cas);
- for(; cas--;)
- {
- scanf("%d",&n);
- cnt=n+;
- for(int i=; i<=n; ++i)
- {
- scanf("%d",&num[i].value);
- num[i].pre=i-;
- num[i].next=i+;
- }
- scanf("%d%d",&l,&r);
- num[].pre=;
- num[].next=;
- num[n+].pre=n;
- ll=num[l].pre;
- rr=num[r].next;
- scanf("%d",&m);
- for(; m--;)
- {
- scanf("%s",s);
- if(strcmp(s,"MoveLeft")==)
- MoveLefe();
- else if(strcmp(s,"MoveRight")==)
- MoveRight();
- else if(strcmp(s,"Insert")==)
- Insert();
- else if(strcmp(s,"Delete")==)
- Delete();
- else if(strcmp(s,"Reverse")==)
- Reverse();
- }
- out(n);
- }
- return ;
- }
HDU 4286 Data Handler 双向链表/Splay的更多相关文章
- HDU 4286 Data Handler --双端队列
题意:有一串数字,两个指针,然后一些添加,删除,反转,以及移动操作,最后输出序列. 解法:可以splay做,但是其实双端队列更简便. 维护三个双端队列LE,MI,RI分别表示[L,R]序列左边,[L, ...
- BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap
1588: [HNOI2002]营业额统计 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger ...
- HDU 4453:Looploop(Splay各种操作)
http://acm.hdu.edu.cn/showproblem.php?pid=4453 题意:很多种操作:1.add x,将从光标起的 k2 个数全部加上 x:2.reverse,将从光标起的 ...
- HDU 4441 Queue Sequence(splay)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4441 题意:一个数列,三种操作:(1)插入:找到没在当前数列中的最小的正整数i,将其插在位置p之后,并 ...
- HDU 1890 Robotic Sort(splay)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...
- hdu 1754 I Hate It (splay tree伸展树)
hdu 1754 I Hate It 其实我只是来存一下我的splay模板的..请大牛们多多指教 #include<stdio.h> #include<string.h> #i ...
- HDU 3487 Play with Chain | Splay
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 2217 Data Structure?
C - Data Structure? Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- hdu 1890 Robotic SortI(splay区间旋转操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题解:splay又一高级的功能,区间旋转这个是用线段树这些实现不了的,这题可以学习splay的旋 ...
随机推荐
- Loadrunner脚本之C语言文件处理函数
一.打开文件 打开文件使用库函数中的fopen函数.fopen函数会为要打开的文件新建一个流,然后返回一个指向file型对象的指针,该file型对象中保存了控制这个流所需要的信息. fp=fopen( ...
- js 判断页面是否加载完成
javascript代码如下: document.onreadystatechange = subSomething; //当页面加载状态改变的时候执行这个方法 function subSomethi ...
- 传统企业,"哀兵必胜"的想法要不得
[文/ 任英杰]同事在内网上发了一篇文章『哀兵必胜』,思量数日,作文应对.文中表达的积极精神让人敬佩,但背后似乎隐含着一股莫名的“情绪”.对行业大格局的基本看法会影响公司转型的成败,觉得还是有必要讨论 ...
- 使用VMware10虚拟机安装Linux系统(能力工场)
作为IT从业人员,我们经常使用到Linux系统,但是实际开发过程我们通常在Windows平台操作,为了满足工作需要,这个时候我们通常在windows平台安装虚拟机,并在其上建立linux系统,这样就极 ...
- Latex 横排图片
\begin{figure} \begin{minipage}[t]{0.5\linewidth} \centering \includegraphics[width=2.2in]{figure/an ...
- (window)Android Studio安装以及Fetching android sdk component information超时的解决方案
转自:http://www.cnblogs.com/sonyi/p/4154797.html 在经过两年的开发之本后,Google 公司终于发布了 Android Studio 1.0,喜欢折腾的童鞋 ...
- Linux下的crontab定时执行任务命令详解
在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为“cr ...
- 应用TcpListener实现的socket服务器端
前言 项目中要实现一个简单的socket服务器端,采用了TcpListener这个类.除了基本的功能之外,有几处需要注意的点. 要能同时接收多个客户端的连接,当然,不需要几千个那么多. 要能探测到客户 ...
- Java沙箱技术
自从Java技术出现以来,有关Java平台的安全性及由Java技术发展所引发的新的安全性问题,引起了越来越多的关注.目前,Java已经大量应用在各个领域,研究Java的安全 性对于更好地使用Java具 ...
- 关于设置MX记录
简介:正确设置MX(Mail Exchanger)邮件交换记录是企业电子邮件服务稳定运行的基本条件,我们经常发现很多企业电子邮箱管理员因为设置了不符合规范的MX记录,导致重要的外部邮件退回或者丢失. ...