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. C#总结3

    第四章:文件管理 File类:      对于File类,里面的方法都是静态方法,就是直接可以用FIle来“.”: 记几个方法吧:File.Copy(string filename1,string f ...

  2. 问题:loadrunner录制event为0

     loadrunner录制问题问题1:录制时出现event为0的状况 解决办法: 1.如果是IE浏览器,把启用第三方浏览器扩展*钩给去掉    2使用火狐浏览器,这个就比较好,在lr启动的时候就去勾选 ...

  3. hadoop-2.5安装与配置

    安装之前准备4台机器:bluejoe0,bluejoe4,bluejoe5,bluejoe9 bluejoe0作为master,bluejoe4,5,9作为slave bluejoe0作为nameno ...

  4. 使用SQLdiag Utility搜集SQL Server诊断信息

    SQLdiag Utility用于搜集诊断信息,给Microsoft技术支持人员做为判断依据. 使用SQLdiag 会进行信息搜集类型 Windows 系统性能日志 Windows 系统日志 SQL ...

  5. Webbrowers控件的小技巧

    我最近接触webbrowers 这个控件比较多,感觉用起来比较顺手吧.可以做很多操作. 貌似很多网络模拟有时候都内置这个控件或者类似的控件,但这样子速度就不能跟那些单纯用API 构建数据包比了. 我一 ...

  6. UDP 广播 Java

    1.服务端 public class UdpBroadcastServer { /** * @param args */ public static void main(String[] args) ...

  7. 经历:Java中字符串中按照多个字符拆分或替换:split()和replaceAll()

    一.replaceAll() 今天,遇到了这样的一个字符串[如下代码]: String s="@0|新港@0|天津@0|东莞@0|南沙@0|营口@0|钦州@0|上海@0|汕头@0|连云港@0 ...

  8. Swift学习的新工具---REPL

    从xcode6.1开始,苹果官方提供了一个新的辅助开发swift的工具,即repl(read eval print loop) OS X Yosemite系统下,打开终端应用程序,输入swift: 如 ...

  9. ###Canny边缘检测算子

    开源中国. #@date: 2014-06-20 #@author: gerui #@email: forgerui@gmail.com 一.一阶微分边缘算子 1. 一阶微分边缘检测算子也称梯度边缘算 ...

  10. iOS创建UUID

    - (NSString *)getUUID { CFUUIDRef uuidObj = CFUUIDCreate(nil); //create a new UUID NSString * uuidSt ...