HDU 3487 Play with Chain 【Splay】
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】的更多相关文章
- HDU 3487 Play with Chain(Splay)
题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...
- 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 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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 YaoYao is fond of playing his chains. He has a c ...
- 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 ...
- HDU 5969 最大的位或 【贪心】 (2016年中国大学生程序设计竞赛(合肥))
最大的位或 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem De ...
- HDU 5968 异或密码 【模拟】 2016年中国大学生程序设计竞赛(合肥)
异或密码 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Des ...
- HDU 5949 Relative atomic mass 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
Relative atomic mass Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
随机推荐
- 打造高仿QQ的友盟反馈界面(MVP模式)
什么是MVP呢,简单来说就是将view层和逻辑完全独立出来,让逻辑和显示完全独立.本例中就是采用了这种模式,让activity作为view层,activity中涉及了适配器,所以这里尝试让适配器作为P ...
- ADB server didn't ACK 解决方法
在安卓开发的过程中 连接真机的时候 连接不上 提示 The connection to adb is down, and a severe error has occured.[2015-01-22 ...
- JSP--JavaBean
JSP 最强有力的一个方面就是能够使用 JavaBean 组件. 按照 Sun 公司的定义, JavaBean是一个可重复使用的软件组件.实际上 JavaBean 是一种 Java 类,通过封装属性和 ...
- HTML5--Audio
一.Audio标签 Web 上的音频 直到现在,仍然不存在一项旨在网页上播放音频的标准. 今天,大多数音频是通过插件(比如 Flash)来播放的.然而,并非所有浏览器都拥有同样的插件. HTML5 规 ...
- iOS项目上传到AppStore步骤流程
1.登录developer.apple.com 2.点击member center后 进下图 3.点击certificates Identifiers进下图 4.点击Certificates进下图,首 ...
- Cornerstone无法上传静态库文件(.a文件)
在用Cornerstone同步文件时出现一个错误 检查后发现是缺少了一个文件 查询了网上的资料后发现是Cornerstone自动忽略了.a文件,所以上传到svn服务器时.a文件不会跟随工程一起传上去, ...
- 小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(四)Alert View视图 学习笔记
当我们的应用电量不足的时候,就需要警告提示,那么我们可以用Alert View视图 实现:
- mysql字段不能为空的字段为空时也能插入的方法
接手了一个项目,设计数据库的时候字段全部是不能为空,但是空值又可以插入数据,刚拿过来的时候就提示各种sql语法错误,现记录一下解决办法. 将my.ini中设置: #sql-mode=STRICT_TR ...
- Effective Java 26 Favor generic types
Use generic types to replace the object declaration Add one or more type parameters to its declarati ...
- Effective Java 33 Use EnumMap instead of ordinal indexing
Wrong practice: Putting sets into an array indexed by the type's ordinal /** * Added demo for the &q ...