UVA 11922 Splay tree
题意:
有n个数1~n
操作a,b表示取出第a~b个数,翻转后添加到数列的尾部
输入n,m
输入m条指令a,b
输出最终的序列
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define Key_Value ch[ch[root][1]][0]
const int maxn=;
int n,m;
int pre[maxn],rev[maxn],key[maxn],size[maxn],ch[maxn][],root,tot1;
void Update_Rev(int r){
if(r==) return;
swap(ch[r][],ch[r][]);
rev[r]^=;
}
void Push_Down(int r){
if(rev[r]){
Update_Rev(ch[r][]);
Update_Rev(ch[r][]);
rev[r]=;
}
}
void Push_Up(int r){
size[r]=+size[ch[r][]]+size[ch[r][]];
}
void New_Node(int &r,int fa,int k){
r=++tot1;
pre[r]=fa;
key[r]=k;
rev[r]=ch[r][]=ch[r][]=;
size[r]=;
}
void Build(int &x,int l,int r,int fa){
if(l>r) return;
int mid=(l+r)>>;
New_Node(x,fa,mid);
Build(ch[x][],l,mid-,x);
Build(ch[x][],mid+,r,x);
Push_Up(x);
}
void Init(){
root=tot1=;
pre[root]=rev[root]=key[root]=size[root]=ch[root][]=ch[root][]=;
New_Node(root,,-);
New_Node(ch[root][],root,-);
Build(Key_Value,,n,ch[root][]);
Push_Up(ch[root][]);
Push_Up(root);
}
void Rotate(int x,int kind){
int y=pre[x];
Push_Down(y);
Push_Down(x);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y]) ch[pre[y]][ch[pre[y]][]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
Push_Up(y);
}
void Splay(int r,int goal){
while(pre[r]!=goal){
if(pre[pre[r]]==goal)
Rotate(r,ch[pre[r]][]==r);
else{
int y=pre[r];
int kind=ch[pre[y]][]==y;
if(ch[y][kind]==r){
Rotate(r,!kind);
Rotate(r,kind);
}else{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
Push_Up(r);
if(goal==) root=r;
}
int Get_Kth(int r,int k){
Push_Down(r);
int t=size[ch[r][]];
if(k==t+) return r;
else if(k<=t) return Get_Kth(ch[r][],k);
else return Get_Kth(ch[r][],k-t-);
}
void Solve(int a,int b){
Splay(Get_Kth(root,a),);
Splay(Get_Kth(root,b+),root);
Update_Rev(Key_Value);
Push_Up(ch[root][]);
Push_Up(root);
int temp=Key_Value;
Key_Value=; Push_Up(ch[root][]);
Push_Up(root); //更改了Key_Value要pushup
Splay(Get_Kth(root,size[root]-),);
Splay(Get_Kth(root,size[root]),root);
Key_Value=temp;
pre[Key_Value]=ch[root][];
Push_Up(ch[root][]);
Push_Up(root);
}
int cnt;
void print(int r){
if(r==) return;
Push_Down(r);
print(ch[r][]);
if(cnt<n&&key[r]>){
cnt++;
printf("%d\n",key[r]);
}
print(ch[r][]);
}
int main()
{
scanf("%d%d",&n,&m);
Init();
int x,y;
while(m--){
scanf("%d%d",&x,&y);
Solve(x,y);
}
cnt=;
print(root);
return ;
}
UVA 11922 Splay tree的更多相关文章
- UVa 11922 & splay的合并与分裂
题意: 1个1—n的排列,实现一下操作:将a—b翻转并移动至序列的最后. SOL: splay维护区间的裸题——不过平衡树的题目貌似都是裸的吧...就是看操作的复杂程度罢... 如何取区间呢,我们在s ...
- Uva 11922 Splay
Splay(伸展树)实现可分裂与合并的序列 对于BST,除了Treap树之外,还有一种Splay的伸展树,他能快速的分裂与合并. 重要的操作是伸展操作,将一个指定的结点 x 旋转到根的过程. 分三种情 ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
- 纸上谈兵:伸展树(splay tree)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们讨论过,树的搜索效率与树的深度有关.二叉搜索树的深度可能为n,这种情况下,每次 ...
- bzoj 3223/tyvj 1729 文艺平衡树 splay tree
原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...
- 伸展树 Splay Tree
Splay Tree 是二叉查找树的一种,它与平衡二叉树.红黑树不同的是,Splay Tree从不强制地保持自身的平衡,每当查找到某个节点n的时候,在返回节点n的同时,Splay Tree会将节点n旋 ...
- [转] Splay Tree(伸展树)
好久没写过了,比赛的时候就调了一个小时,差点悲剧,重新复习一下,觉得这个写的很不错.转自:here Splay Tree(伸展树) 二叉查找树(Binary Search Tree)能够支持多种动态集 ...
- 树-伸展树(Splay Tree)
伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...
- HDU-3436 Queue-jumpers 树状数组 | Splay tree删除,移动
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3436 树状数组做法<猛戳> Splay tree的经典题目,有删除和移动操作.首先要离散化 ...
随机推荐
- hibernate 异常a different object with the same identifier value was already associated with the session
在使用hibernate的时候发现了一个问题,记录一下解决方案. 前提开启了事务和事务间并无commit,进行两次save,第二次的时候爆出下面的异常a different object with t ...
- struts2 result type属性说明
首先看一下在struts-default.xml中对于result-type的定义: <result-types><result-type name="chain" ...
- python学习摘要(3)--字符串处理函数
python没有字符类型, "字符串" '字符串' '''字符串''' """字符串""" 三引号可以支持字符串跨行 字 ...
- IT启示录
引用电影<夏洛特烦恼>中夏洛的一句话:"一直以来,我根本就不知道自己想要什么".可以说在写这篇博客之前我仍然没有考虑清楚之后的道路,即使早已明确了走游戏开发的道理,却不 ...
- iOS开发UIColor,CGColor,CIColor三者的区别和联系
最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清楚它们之间的区别和联系.下面我们分别看看它们三个的概念: 一.UIColo ...
- zabbix概念
Zabbix是一个企业级的.开源的.分布式监控解决方案. Zabbix可以监控网络和服务的监控状况. Zabbix利用灵活的告警机制,允许用户对事件发送基于Email的告警. 这样可以保证快速的对问题 ...
- C#部分语法总结
1. Frst和FirstOrDefault 1. Fist 如果查询的数据不存在, 则抛System.InvalidOperationException异常 2. FirstOrdefault 如果 ...
- java获取时间整点工具代码
/**获取上n个小时整点小时时间 * @param date * @return */ public static String getLastHourTime(Date date,int n){ C ...
- 【Todo】【转载】JVM学习
先参考如下这个系列<聊聊JVM> http://blog.csdn.net/column/details/talk-about-jvm.html
- winform全局异常捕获
/// <summary> /// 应用程序的主入口点. /// </summary> public static ApplicationContext context; [S ...