hdu 3487
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的更多相关文章
- HDU 3487 Play with Chain(Splay)
题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...
- 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 ...
- 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 ...
- HDU 3487 Play with Chain | Splay
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 3487 Play with Chain 【Splay】
1-n的序列,有两种操作: 1,将一段区间翻转 2,将一段区间切下来放到剩余序列的第C个数后 采用延迟更新的方法维护区间的翻转,并维护一个size域. 添加一个最大点和一个最小点,防止出界 翻转时,将 ...
- HDU 3487 Splay
给定两种操作,一种是把一个数列的某一段切下来插到剩余数列的某一个位置上. 一种是翻转操作,把数列的某一段进行翻转. 都是Splay的基本操作.标准的Rotateto调整出 [a,b]区间.然后对[a, ...
- 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 ...
- HDU 3487 Splay tree
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 【HDU 3487】Play with Chain Splay
题意 给定$n$个数序列,每次两个操作,将区间$[L,R]$拼接到去掉区间后的第$c$个数后,或者翻转$[L,R]$ Splay区间操作模板,对于区间提取操作,将$L-1$ Splay到根,再将$R+ ...
随机推荐
- Redis常见问题及处理方法
一.Redis状态检查 唯一标记一个redis实例的是ip和端口,前端是用tcp方式来访问redis的,我们提供给应用访问的是一个ip+63379(一般使用63379) 端口.因此我们执行如下命令检查 ...
- nyoj 96 n-1位数(处理前导 0 的情况)
n-1位数 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则 ...
- Hive over HBase和Hive over HDFS性能比较分析
http://superlxw1234.iteye.com/blog/2008274 环境配置: hadoop-2.0.0-cdh4.3.0 (4 nodes, 24G mem/node) hbase ...
- 高级C#
使用delegates委托写插件方法: public delegate int Transformer (int x); public class Util { public static void ...
- php里ezpdo orm框架初探
http://jackyrong.iteye.com/blog/238930 http://www.oschina.net/project/tag/126/orm?sort=view&lang ...
- ###学习《C++ Primer》- 1
点击查看Evernote原文. #@author: gr #@date: 2014-09-30 #@email: forgerui@gmail.com 记录读书过程中一些知识点.可能不系统,:-). ...
- 第九篇、CSS布局
<!--css布局 标准流:从上到下 从左到右 脱离标准流:(浮在父控件的最左边或者最右边)(类似ios在window上添加的控件) 1.float: 2.position: absolute( ...
- 第一篇、Swift_Textkit的基本使用
简介: iOS7 的发布给开发者的案头带来了很多新工具.其中一个就是 TextKit(文本工具箱).TextKit 由许多新的 UIKit 类组成,顾名思义,这些类就是用来处理文本的. 1.NSTex ...
- ajax跨域请求的解决方案
一直打算改造一下自己传统做网站的形式. 我是.Net程序员,含辛茹苦数年也没混出个什么名堂. 最近微信比较火, 由于现在大环境的影响和以前工作的总结和经验,我打算自己写一个数据,UI松耦合的比较新潮的 ...
- Java实战之04JavaWeb-06DBUtils
一.DBUtils 1.DBUtils的简介 Commons DbUtils是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的 ...