splay 练手用;

杭电的oj要手动开栈;

#include<cstdio>
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstring>
#include<algorithm>
#define inf 999999
#define maxn 1500009
#define lch(rt) son[rt][0]
#define rch(rt) son[rt][1]
using namespace std; int son[maxn][],fa[maxn];
int val[maxn],size[maxn],flg[maxn];
int cnt,root;
int num[maxn];
int n,m; void push_up(int rt)
{
size[rt]=size[lch(rt)]+size[rch(rt)]+;
} void push_down(int rt)
{
if(flg[rt])
{
swap(lch(rt),rch(rt));
if(lch(rt))
flg[lch(rt)]^=;
if(rch(rt))
flg[rch(rt)]^=;
flg[rt]=;
}
} void newnode(int &rt,int f,int v)
{
rt=++cnt;
lch(rt)=rch(rt)=;
val[rt]=v;
fa[rt]=f;
size[rt]=;
flg[rt]=;
} void build(int l,int r,int &rt,int f)
{
if(l>r)return;
int mid=(l+r)>>;
newnode(rt,f,num[mid]);
build(l,mid-,lch(rt),rt);
build(mid+,r,rch(rt),rt);
push_up(rt);
} void ini()
{
cnt=root=;
lch()=rch()=;
fa[]=val[]=flg[]=size[]=;
newnode(root,,);
newnode(rch(root),root,inf);
build(,n,lch(rch(root)),rch(root));
push_up(rch(root));
push_up(root);
} void rotate(int x,int kind)//0->left,1->right
{
push_down(x);
int y=fa[x];
son[y][kind^]=son[x][kind];
fa[son[x][kind]]=y;
if(fa[y])
son[fa[y]][son[fa[y]][]==y]=x;
fa[x]=fa[y];
son[x][kind]=y;
fa[y]=x;
push_up(y);
} void splay(int rt,int goal)//将rt节点旋转到goal的右子节点
{
if(rt!=goal)
{
push_down(rt);
while(fa[rt]!=goal)
{
if(lch(fa[rt])==rt)
rotate(rt,);
else rotate(rt,);
}
push_up(rt);
if(!goal)root=rt;
}
} int select(int k)
{
int rt=root;
push_down(rt);
while(size[lch(rt)]+!=k)
{
if(size[lch(rt)]+>=k)
rt=lch(rt);
else
{
k-=(size[lch(rt)]+);
rt=rch(rt);
}
push_down(rt);//不加就超时;
}
return rt;
} int cntt;
void dfs(int rt)
{
push_down(rt);
if(lch(rt))
dfs(lch(rt));
num[cntt++]=val[rt];
if(rch(rt))
dfs(rch(rt));
} void flip(int a,int b)
{
a=select(a-);
splay(a,);
b=select(b+);
splay(b,a);
flg[lch(b)]^=;
} char s[],t[]; int main()
{
int tt;
int ld,rd;
scanf("%d",&tt);
while(tt--)
{
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%d",&num[i]);
ini();
scanf("%d%d",&ld,&rd);
ld++;
rd++;
int cot=;
int a,b;
scanf("%d",&m);
int dat;
while(m--)
{
scanf("%s",s);
if(s[]=='M')
{
scanf("%s",t);
if(t[]=='R'&&s[]=='R')
rd++;
else if(t[]=='R'&&s[]=='L')
rd--;
else if(t[]=='L'&&s[]=='R')
ld++;
else ld--;
}
else if(s[]=='I')
{
cot++;
scanf("%s%d",t,&dat);
if(t[]=='L')
{
a=select(ld-);
b=select(ld);
}
else
{
a=select(rd);
b=select(rd+);
}
rd++;
splay(a,);
splay(b,a);
newnode(lch(b),b,dat);
fa[lch(b)]=b;
push_up(b);
push_up(a);
}
else if(s[]=='R')
{
flip(ld,rd);
}
else if(s[]=='D')
{
cot--;
scanf("%s",t);
if(t[]=='L')
{
a=select(ld-);
b=select(ld+);
}
else
{
a=select(rd-);
b=select(rd+);
}
rd--;
splay(a,);
splay(b,a);
push_down(a);
push_down(b);
lch(b)=;
push_up(b);
push_up(a);
}
}
a=select();
b=select(n+cot+);
splay(a,);
splay(b,a);
n+=cot;
cntt=;
dfs(root);
for(int i=;i<n;i++)
printf("%d ",num[i]);
printf("%d\n",num[n]);
}
return ;
}

hdu 4286的更多相关文章

  1. HDU 4286 Data Handler 双向链表/Splay

    Data Handler Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  2. HDU 4286 Data Handler --双端队列

    题意:有一串数字,两个指针,然后一些添加,删除,反转,以及移动操作,最后输出序列. 解法:可以splay做,但是其实双端队列更简便. 维护三个双端队列LE,MI,RI分别表示[L,R]序列左边,[L, ...

  3. hdu 4286 (list的reverse时间复杂度为n)

    list 的翻转reverse源码: // 将链表倒置 // 其算法核心是历遍链表, 每次取出一个结点, 并插入到链表起始点 // 历遍完成后链表满足倒置 template <class T, ...

  4. [GodLove]Wine93 Tarining Round #8

    比赛链接: http://vjudge.net/contest/view.action?cid=47644#overview 比赛来源: 2012 ACM/ICPC Asia Regional Tia ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. Lodash Filter

    var persons = [{name:'1',age:'20'}, {name:'2', age:'25'}];_.filter(persons, {'age': '25'}); //return ...

  2. SQL语句打印四个方向的9 9 乘法表

    declare @i int ,@j int ,@s nvarchar(max) set @i = 1 while @i <=9 begin set @s = ' ' set @j = 1 wh ...

  3. spark下测试akka的分布式通讯功能

    采用的spark版本为1.1.0 scala版本为2.10.4 编写scala类文件myactors.scala: package bluejoe import akka.actor._ import ...

  4. Opencv——灰度直方图

    灰度直方图是灰度级的函数,它表示图像中具有某种灰度级的像素的个数,反映了图像中某种灰度出现的频率. 如果将图像总像素亮度(灰度级别)看成是一个随机变量,则其分布情况就反映了图像的统计特性,这可用pro ...

  5. artice与section的区别

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  6. [翻译][MVC 5 + EF 6] 8:更新相关数据

    原文:Updating Related Data with the Entity Framework in an ASP.NET MVC Application 1.定制Course的Create和E ...

  7. 排序算法SIX:冒泡排序BubbleSort

    /** *冒泡排序: * 两个两个比较,一轮过后最大的排在了最后面 * n个数变为n-1个没排好的数 * 再进行一轮 * 第二大的排在了倒数第二个 * 以此类推 * 直到排到第一个为止 * * 弄两个 ...

  8. iOS数据库操作流程

    SQLite最新的版本是3.0,使用之前应该先导入libsqlite3.0.dylib 1.导入流程 2.iOS中操作数据库的流程 打开数据库 准备SQL数据库 执行SQL数据库 语句完结 关闭数据库 ...

  9. jQuery Mobile里xxx怎么用呀? (事件篇)

    jQuery Mobile里$(document).ready()怎么用呀? 相关链接: http://stackoverflow.com/questions/14468659/jquery-mobi ...

  10. javascript 上传 预览图片 兼容 谷歌 ie

    最近的项目要用到这块,但是在网上找了很多资料,很多都是假的,都不行,最后终于找到一个,还是可以兼容主流的,特分享给大家,可以用 <!DOCTYPE html PUBLIC "-//W3 ...