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. 二、SQL语句映射文件(1)resultMap

    //备注:该博客引自:http://limingnihao.iteye.com/blog/106076 SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对 ...

  2. centos下完全卸载mysql

    版权声明:本文为博主原创文章,未经博主允许不得转载. yum方式安装的MySQL 1.yum remove mysql mysql-server mysql-libs compat-mysql51 2 ...

  3. bootstrap dialog自行控制窗口的关闭

    在使用dialog的时候,我们通常不希望点击btn的时候自动隐藏dialog,通常需要做一些清理或者ajax操作,在bootstrap dialog中,这是通过 data-dismiss=" ...

  4. 2015年第13本(英文第9本):Murder on the Orient Express 东方快车谋杀案

    书名:Murder on the Orient Express 东方快车谋杀案 作者:Agatha Christie 单词数:6.1万 不重复单词数:不详 首万词不重复单词数:不详 蓝思值:640 阅 ...

  5. Android studio打包APK混淆配置

    要在打包APK时加入混淆需要在Module中的buid.gradle中加入如下信息 buildTypes { release { minifyEnabled true shrinkResources ...

  6. IOS-UITextField-全解

    IOS-UITextField-全解   //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFrame: ...

  7. OC的项目网址(自己编写的项目)

    因为便于方便快速打开自己曾经写过的项目,所以就把链接保存在博客里了.一点击就能找到. <附注学习github排版:https://github.com/yangxuanxc/guide?file ...

  8. android network develop(1)----doing network background

    Develop network with HttpURLConnection & HttpClient. HttpURLConnection  is lightweight with Http ...

  9. ERP产品价格成本计算的几个方法(转)

          一般财务计算产品价格又很多方法,我这里做了几个供参考,实体属性主要是编号.数量.价格等,这里就不列出了. /// <summary> /// 先进先出算法 /// </s ...

  10. CentOS 7 安装Docker

    1.安装前检查: a.内核版本 uname -a b.检查Device Mapper ls -l /sys/class/misc/device-mapper 2.安装Docker: a.更新系统包到最 ...