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+ ...
随机推荐
- 用happen-before规则重新审视DCL(转载)
编写Java多线程程序一直以来都是一件十分困难的事,多线程程序的bug很难测试,DCL(Double Check Lock)就是一个典型,因此对多线程安全的理论分析就显得十分重要,当然这决不是说对多线 ...
- 读jQuery官方文档:样式
样式 使用jQuery,无论是设置或者获取元素样式都十分简便. // 支持驼峰式和分割线式,两种方式等价(原生JavaScript只支持驼峰式) $('h1').css('fontSize'); $( ...
- aspjpeg 半透明描边的实现函数
'参数说明 'big 原图路径(相对) 'small 生成图路径(相对) 'width_s 生成后宽度(数值型) 'height_s生成后高度(数值型) 'images/Alpha.jpg 为一个像素 ...
- ASP.NET生成日历
public string GetDayList(DateTime? date) { ; var minDate = Convert.ToDateTime(((DateTime)(date ?? Da ...
- 使用c#生成Identicon图片
Identicon是什么 我们在站点注册的时候通常系统会在我们没有提供自定义头像时为我们指定一个默认的头像,不过,样子千篇一律很是难看.聪明的程序员想了很多办法来解决这个问题,比如你能在这里看到很漂亮 ...
- touches,motion触摸事件响应
//触摸事件响应需要重写方法 1 // 触摸时触发该方法(消息发送) - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent: ...
- [PR & ML 5] [Introduction] Decision Theory
- Headfirst设计模式的C++实现——适配器(Adapter)
duck.h #ifndef _DUCK_H_ #define _DUCK_H_ class DUCK { public: ; ; }; #endif mallard_duck.h #ifndef _ ...
- iOS 分类思想(2)
/******************* NSString+NumCount.h文件 ******************************/ #import <Foundation/Fo ...
- mysql---整体备份和增量备份
整体备份: 对整张表或者整个数据库甚至所有数据库进行备份. 增量备份: 对某一范围内的数据进行备份. 1.整体备份: 对表进行备份: 针对存储引擎为myisam的表,可以直接复制frm.myd.myi ...