splay

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 300009
#define lch(rt) son[rt][0]
#define rch(rt) son[rt][1]
using namespace std; int son[maxn][],fa[maxn],size[maxn],val[maxn],st[maxn];
int root,cnt;
int num[maxn],flg[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])
{
int tmp=rch(rt);
rch(rt)=lch(rt);
lch(rt)=tmp;
if(rch(rt))
flg[rch(rt)]^=;
if(lch(rt))
flg[lch(rt)]^=;
flg[rt]=;
}
} void newnode(int &rt,int f,int v)
{
rt=++cnt;
rch(rt)=lch(rt)=;
fa[rt]=f;
val[rt]=v;
size[rt]=;
flg[rt]=;
} void build(int l,int r,int &rt,int f)
{
if(l>r)return;
int m=(l+r)>>;
newnode(rt,f,num[m]);
build(l,m-,lch(rt),rt);
build(m+,r,rch(rt),rt);
push_up(rt);
} void ini()
{
lch()=rch()=;
fa[]=size[]=val[]=;
root=;
cnt=;
newnode(root,,);
newnode(rch(root),root,n+);
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;
} void cut(int a,int b,int c)
{
a=select(a-);
splay(a,);
b=select(b+);
splay(b,a);
int res=lch(b);
lch(b)=;
push_up(b);push_up(a);
a=select(c);b=select(c+);
splay(a,);splay(b,a);
lch(b)=res;
fa[res]=b;
} void flip(int a,int b)
{
a=select(a-);
splay(a,);
b=select(b+);
splay(b,a);
flg[lch(b)]^=;
} int cot=;
void dfs(int rt)
{
push_down(rt);
if(lch(rt))
dfs(lch(rt));
if(val[rt]>&&val[rt]<=n&&cot<n)
{
printf("%d ",val[rt]);
cot++;
}
else if(val[rt]>&&val[rt]<=n&&cot==n)
{
printf("%d",val[rt]);
}
if(rch(rt))
dfs(rch(rt));
} char s[]; int main()
{
for(int i=; i<maxn; i++)
num[i]=i;
while(scanf("%d%d",&n,&m)&&n>)
{ ini();
int a,b,c;
while(m--)
{
scanf("%s",s);
if(s[]=='C')
{
scanf("%d%d%d",&a,&b,&c);
cut(a+,b+,c+);
}
else
{
scanf("%d%d",&a,&b);
flip(a+,b+);
}
}
cot=;
a=select();b=select(n+);
splay(a,);splay(b,a);
dfs(root);
puts("");
}
return ;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 300009
#define lch(rt) son[rt][0]
#define rch(rt) son[rt][1]
using namespace std; int son[maxn][],fa[maxn],size[maxn],val[maxn],st[maxn];
int root,cnt;
int num[maxn],flg[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])
{
int tmp=rch(rt);
rch(rt)=lch(rt);
lch(rt)=tmp;
flg[rch(rt)]^=;
flg[lch(rt)]^=;
flg[rt]=;
}
} void newnode(int &rt,int f,int v)
{
rt=++cnt;
rch(rt)=lch(rt)=;
fa[rt]=f;
val[rt]=v;
size[rt]=;
flg[rt]=;
} void build(int l,int r,int &rt,int f)
{
if(l>r)return;
int m=(l+r)>>;
newnode(rt,f,num[m]);
build(l,m-,lch(rt),rt);
build(m+,r,rch(rt),rt);
push_up(rt);
} 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的右子节点
{
push_down(rt);
while(fa[rt]!=goal)
{
int y=fa[rt];
if(fa[y]==goal)
rotate(rt,son[y][]==rt);
else
{
int kind=son[fa[y]][]==y;
if(son[y][kind]==rt)
{
rotate(rt,kind^);
rotate(rt,kind);
}
else
{
rotate(y,kind);
rotate(rt,kind);
}
}
}
push_up(rt);
if(goal==) root=rt;
} int select(int k)
{
int rt=root;
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;
} void rotate_to(int k,int goal)//将第k节点旋转到goal的右儿子节点;
{
int rt=root;
rt=select(k);
splay(rt,goal);
} void cut(int a,int b,int c)
{
rotate_to(a-,);
rotate_to(b+,root);
int x=rch(root);
int tmp=lch(x);
lch(x)=;
push_up(x);
push_up(root);
rotate_to(c,);
rotate_to(c+,root);
fa[tmp]=rch(root);
lch(rch(root))=tmp;
push_up(rch(root));
push_up(root);
} void flip(int a,int b)
{
rotate_to(a-,);
rotate_to(b+,root);
flg[lch(rch(root))]^=;
} void dfs(int rt)
{
push_down(rt);
if(lch(rt))
dfs(lch(rt));
if(val[rt]!=)
printf(" %d",val[rt]);
if(rch(rt))
dfs(rch(rt));
} void ini()
{
lch()=rch()=;
fa[]=size[]=;
root=;
cnt=;
newnode(root,,);
newnode(rch(root),root,);
push_up(root);
build(,n,lch(rch(root)),rch(root));
push_up(rch(root));
push_up(root);
} char s[]; int main()
{
while(scanf("%d%d",&n,&m)&&n>)
{
for(int i=; i<=n; i++)
num[i]=i;
ini();
int a,b,c;
while(m--)
{
scanf("%s",s);
if(s[]=='C')
{
scanf("%d%d%d",&a,&b,&c);
cut(a,b,c);
// rotate_to(1,0);
// printf("%d",val[root]);
// dfs(rch(root));
// puts("");
}
else
{
scanf("%d%d",&a,&b);
flip(a,b);
}
}
rotate_to(,);
printf("%d",val[root]);
dfs(rch(root));
puts("");
}
return ;
}

hdu 3487的更多相关文章

  1. HDU 3487 Play with Chain(Splay)

    题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...

  2. hdu 3487 Play with Chain

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 YaoYao is fond of playing his chains. He has a c ...

  3. HDU 3487:Play with Chain(Splay)

    http://acm.hdu.edu.cn/showproblem.php?pid=3487 题意:有两种操作:1.Flip l r ,把 l 到 r 这段区间 reverse.2.Cut a b c ...

  4. HDU 3487 Play with Chain | Splay

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU 3487 Play with Chain 【Splay】

    1-n的序列,有两种操作: 1,将一段区间翻转 2,将一段区间切下来放到剩余序列的第C个数后 采用延迟更新的方法维护区间的翻转,并维护一个size域. 添加一个最大点和一个最小点,防止出界 翻转时,将 ...

  6. HDU 3487 Splay

    给定两种操作,一种是把一个数列的某一段切下来插到剩余数列的某一个位置上. 一种是翻转操作,把数列的某一段进行翻转. 都是Splay的基本操作.标准的Rotateto调整出 [a,b]区间.然后对[a, ...

  7. HDU 3487 Play with Chain (splay tree)

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. HDU 3487 Splay tree

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. 【HDU 3487】Play with Chain Splay

    题意 给定$n$个数序列,每次两个操作,将区间$[L,R]$拼接到去掉区间后的第$c$个数后,或者翻转$[L,R]$ Splay区间操作模板,对于区间提取操作,将$L-1$ Splay到根,再将$R+ ...

随机推荐

  1. 어느 도시 보유 하 면 사랑 이다(事態が発生すれば、ある都市の恋はしません)【Si les villes un amour】{If have love in a city}

    如果在北京拥有爱情 半夜在簋街喝啤酒 吃小龙虾 在后海的苦情歌声里 搂着你数那四合院的瓦片 如果在上海拥有爱情 去外滩手挽手 吹吹风 坐一下午 去城隍庙尝试各种小吃 嘲笑你嘴角残余的糯米糕 如果在杭州 ...

  2. MATLAB的循环结构

    循环结构有两种基本形式:while 循环和for 循环.两者之间的最大不同在于代码的重复是如何控制的.在while 循环中,代码的重复的次数是不能确定的,只要满足用户定义的条件,重复就进行下去.相对地 ...

  3. JavaScript高级程序设计(第三版)学习,第一次总结

    Array类型 var arr = []; arr.length; //返回数组元素个数 改变length可以动态改变数组大小 检测数组 instanceof可以检测某个对象是否是数组,限制:只能是一 ...

  4. JavaScript高级程序设计(第三版)学习笔记22、24、25章

    第22章,高级技巧 高级函数 安全的类型检测 typeof会出现无法预知的行为 instanceof在多个全局作用域中并不能正确工作 调用Object原生的toString方法,会返回[Object ...

  5. 跨站脚本攻击(XSS)

    跨站脚本攻击(XSS) XSS发生在目标网站中目标用户的浏览器层面上,当用户浏览器渲染整个HTML文档的过程中就出现了不被预期的脚本执行. 跨站脚本的重点不是在“跨站”上,而应该在“脚本上” 简单例子 ...

  6. js 获取url中的查询字符串

    function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)( ...

  7. ASP判断文件地址是否有效

    <% Response.Write("<head><style><!--span{ font-size: 9pt }--></style> ...

  8. 两天来学习C的感受

    大学的时候曾经学习过C语言,教科书是谭浩强的绿色的书.当时根本没有好好学习,期末考试是靠老师画重点才过的. 那个时候稀里哗啦的完全听不明白,最揪心的是指针和文件操作(当时根本不知道这个世界上还有DB存 ...

  9. Java_JVM学习笔记(深入理解Java虚拟机)___重点

    http://chenzhou123520.iteye.com/category/196061 转载 JVM学习笔记(一):运行时数据区 JVM学习笔记(二):JVM中对象访问的两种方式 JVM学习笔 ...

  10. ubuntu系统安装flashplayer

    打开浏览器,输入adobe flashplayer 进入官方网站,下载Linux 32-bit, 简体中文, Firefox,下载.tar.gz包. 然后点击立即下载.下载之后找到解压该文件夹,找到 ...