1-n的序列,有两种操作:

1,将一段区间翻转

2,将一段区间切下来放到剩余序列的第C个数后

采用延迟更新的方法维护区间的翻转,并维护一个size域。

添加一个最大点和一个最小点,防止出界

翻转时,将第L-1个点伸展到跟,再将第R+1个点伸展到L-1的右子树,这时R+1的左子树就是要翻转的区间,加上一个标记。

切区间时,跟翻转操作差不多,只是不加标记。然后找到C+1和C,将C伸展到根,C+1伸展到C的右子树,此时C+1的左子树就是要插入的位置。

其实我说了这么多并没有什么卵用。。。。最后还是得自己画图看才能懂。

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
const int N=3e5+;
int data[N],num[N],t[N][],id,fa[N];
int flip[N],root,f;
inline void pushup(int x){
num[x]=num[t[x][]]+num[t[x][]]+;
}
inline void pushdown(int x){
if(flip[x]){
flip[x]=;
flip[t[x][]]^=;
flip[t[x][]]^=;
swap(t[x][],t[x][]);
}
}
inline void Rotate(int x,int w){
int y=fa[x];
int z=fa[y];
pushdown(y);
t[y][!w]=t[x][w];
fa[t[x][w]]=y;
t[z][t[z][]==y]=x;
fa[x]=z;
t[x][w]=y;
fa[y]=x;
pushup(y);
}
void newnode(int &x,int y,int v){
x=++id;
t[x][]=t[x][]=;
fa[x]=y;
data[x]=v,flip[x]=;
num[x]=;
}
void build(int &x,int y,int l,int r){
if(l<=r){
int mid=(l+r)>>;
newnode(x,y,mid);
build(t[x][],x,l,mid-);
build(t[x][],x,mid+,r);
pushup(x);
}
}
void init(int n){
f=id=root=;
t[][]=t[][]=num[]=data[]=flip[]=fa[]=;
newnode(root,,-);
newnode(t[][],root,-);
build(t[t[][]][],t[][],,n);
pushup(t[][]);
pushup();
}
void Splay(int x,int y){
if(x!=y){
pushdown(x);
while(fa[x]!=y){
if(t[fa[x]][]==x)
Rotate(x,);
else
Rotate(x,);
}
pushup(x);
if(!y)root=x;
}
}
int Kth(int k){
int x=root;
pushdown(root);
for(;num[t[x][]]+!=k;){
if(num[t[x][]]+>k)
x=t[x][];
else
k-=num[t[x][]]+,x=t[x][];
pushdown(x);
}
return x;
}
void Flip(int a,int b){
a=Kth(a);
b=Kth(b+);
Splay(a,);
Splay(b,a);
flip[t[b][]]^=;
}
void Cut(int a,int b,int c){
int tmp,d; a=Kth(a);b=Kth(b+);
Splay(a,);Splay(b,a);
tmp=t[b][];t[b][]=;
pushup(b);pushup(a); d=Kth(c+);c=Kth(c+);
Splay(c,);Splay(d,c);
t[d][]=tmp;fa[tmp]=d;
pushup(d);pushup(c);
}
void Inorder(int x){
if(x){
pushdown(x);
Inorder(t[x][]);
if(data[x]>){
if(f)
putchar(' ');
else f=;
printf("%d",data[x]);
}
Inorder(t[x][]);
}
}
int main(){
int n,m;
char op[];
while(scanf("%d%d",&n,&m)!=EOF){
if(n==-)break;
init(n);
int a,b,c;
while(m--){
scanf("%s",op);
if(op[]=='F'){
scanf("%d%d",&a,&b);
Flip(a,b);
}else{
scanf("%d%d%d",&a,&b,&c);
Cut(a,b,c);
}
}
Inorder(root);
puts("");
}
return ;
}

HDU 3487 Play with Chain 【Splay】的更多相关文章

  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 (splay tree)

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

  3. HDU 3487 Play with Chain | Splay

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

  4. 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 ...

  5. HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  6. HDU 5969 最大的位或 【贪心】 (2016年中国大学生程序设计竞赛(合肥))

    最大的位或 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem De ...

  7. HDU 5968 异或密码 【模拟】 2016年中国大学生程序设计竞赛(合肥)

    异或密码 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Des ...

  8. HDU 5949 Relative atomic mass 【模拟】 (2016ACM/ICPC亚洲区沈阳站)

    Relative atomic mass Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  9. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

随机推荐

  1. ASP.NET Core 1.0开发Web API程序

    .NET Core版本:1.0.0-rc2Visual Studio版本:Microsoft Visual Studio Community 2015 Update 2开发及运行平台:Windows ...

  2. Linux 学习手记(5):使用Vim文本编辑器

    Vim是从vi发展而来的文本编辑器.vi是Linux及类Unix系统中主流的命令行文本编辑器,Vim 除了对vi功能上进行了加强,还加入了对GUI的支持.绝大多数的Linux系统上均安装了vim,vi ...

  3. HTML5中的SVG

    * SVG * 基本内容 * SVG并不属于HTML5专有内容 * HTML5提供有关SVG原生的内容 * 在HTML5出现之前,就有SVG内容 * SVG,简单来说就是矢量图 * SVG文件的扩展名 ...

  4. elasticsearch索引的增删改查入门

    为了方便直观我们使用Head插件提供的接口进行演示,实际上内部调用的RESTful接口. RESTful接口URL的格式: http://localhost:9200/<index>/&l ...

  5. mvc项目架构分享系列之架构搭建初步

    mvc项目架构分享系列之架构搭建初步 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构各部分解析 ...

  6. 关于在EXCEL中输入01-01-01被转换为2001/1/1怎么解决

    当向EXCEL写入类似'01-01-01'或'01-01'这样的数据时,打开EXCEL时会发现数据变成了2001/1/1和1月1日. 这是由于EXCEL自动转换功能,我们得要在输入前多加一个’号. 而 ...

  7. SharePoint 2010 文档管理之过期归档工具

    前言:使用过SharePoint的人都知道,SharePoint对于操作是便捷的,但是对于数据量承载却是不令人满意的,这样,就要求我们需要更加合理的使用,规范大家的使用规则和习惯,所以,定期清理不必要 ...

  8. 复杂领域的Cynefin模型和Stacey模型

    最近好奇“复杂系统”,收集了点资料,本文关于Cynefin模型和Stacey模型.图文转自互联网后稍做修改. Cynefin模型提供一个从因果关系复杂情度来分析当前情况而作决定的框架,提出有五个领域: ...

  9. 【转】R语言笔记--颜色的使用

    转自该网站:http://research.stowers-institute.org/efg/R/Color/Chart/ 科学可视化中常用的一些颜色表:http://geog.uoregon.ed ...

  10. SparseArray<E>详解

    SparseArray<E> 是官方推荐的用来替代 HashMap<Integer, E> 的一个工具类,相比来说有着更好的性能(其核心是折半查找函数(binarySearch ...