hdu 4286
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的更多相关文章
- HDU 4286 Data Handler 双向链表/Splay
Data Handler Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 4286 Data Handler --双端队列
题意:有一串数字,两个指针,然后一些添加,删除,反转,以及移动操作,最后输出序列. 解法:可以splay做,但是其实双端队列更简便. 维护三个双端队列LE,MI,RI分别表示[L,R]序列左边,[L, ...
- hdu 4286 (list的reverse时间复杂度为n)
list 的翻转reverse源码: // 将链表倒置 // 其算法核心是历遍链表, 每次取出一个结点, 并插入到链表起始点 // 历遍完成后链表满足倒置 template <class T, ...
- [GodLove]Wine93 Tarining Round #8
比赛链接: http://vjudge.net/contest/view.action?cid=47644#overview 比赛来源: 2012 ACM/ICPC Asia Regional Tia ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- [引]LINQ to XML 类概述
本文转自:http://msdn.microsoft.com/zh-cn/library/bb387023.aspx 本主题提供 System.Xml.Linq 命名空间中 LINQ to XML 类 ...
- webbroswer 后台注入脚本 的方法
HtmlElement script = webBrowser.Document.CreateElement("script"); script.SetAttribute(&quo ...
- c# 远程监控(1) 大纲
此篇文章主要讲了如何使用C# Winform程序模拟一个远程监控系统,并使用RTP实时传输协议传输数据. 应用场景:远程监控.局域网视频会议.客户端流式缓冲播放 这方面的资料还是有一些,但是都需要整合 ...
- ZooKeeper - 状态信息 Stat 的属性说明
运行%ZK_HOME%/bin目录下的zkCli.sh(zkCli.cmd),使用get命令可以获取指定ZNode的数据内容和属性信息.例如: [zk: localhost:2181(CONNECTE ...
- 转载:flash 跨域 crossdomain.xml
转载自http://hi.baidu.com/bozz_/item/e8b1c7c4ca31317489ad9e91 flash 跨域 crossdomain.xml 一.概述 位于www.cross ...
- Windows8.1下PHP环境配置(PHP5.6、Apache2.4、MySql5.6)
Step0 安装准备(均为64-bit版本) 下载php "Non Thread Safe"是IIS专用的,"Thread Safe"是Apache服务器用的. ...
- VS2012无法创建项目:未找到与约束……匹配的导出
故障情况:7月10号后用VS2012创建项目时,弹出如下对话框,无法创建新项目: 而后经网络搜索确定是7月10号更新了系统补丁后造成的 解决方案: 1.卸载这两个补丁后重启电脑: 2.到http:// ...
- 学习C++ Primer 的个人理解(二)
本身就一定基础的读者我想变量常量这些概念应该已经不是问题了.但是本章还是有几个重点,需要特别留意一下的: 1.初始化和赋值是不同的操作 2.任何非0值都是true 3.使用新标准列表初始化,在有丢失精 ...
- 生产项目加入到SVN版本控制
零.介绍 每天定时备份是通过ftp打包和同步的方式,这些都是比较粗的备份,没法恢复到指定时间的文件,所以需要用到svn控制版本. (请不要问我为什么不用git) 一.现有项目文件加入版本控制 因为项目 ...
- Linux网络应用编程之VLAN(Packet Tracer仿真)
VLAN 一.VLAN概述 VLAN(虚拟局域网):将多个设备和用户在逻辑上联网在一起,这些设备和用户不受物理位置的限制(物理设备可以在不同的地方),但是他们的通信就好像在同一网段中一样,这就叫VLA ...