3223: Tyvj 1729 文艺平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 3313  Solved: 1883
[Submit][Status][Discuss]

Description

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1

Input

第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n)  m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n

Output

输出一行n个数字,表示原始序列经过m次变换后的结果

Sample Input

5 3

1 3

1 3

1 4

Sample Output

4 3 2 1 5

HINT

N,M<=100000

Source

RunID

  User Problem Result Memory Time Language Code_Length Submit_Time
1538282 ksq2013 3223 Accepted 5396 kb 2576 ms C++/Edit 2878 B 2016-07-08 20:57:17

区间翻转裸题,废话不多说,直接放代码:

#include<cstdio>
#include<iostream>
#define INF 0x3f3f3f3f
#define Key_value ch[ch[root][1]][0]
using namespace std;
bool rev[];
int n,m,root,tot,size[],key[],pre[],ch[][];
void NewNode(int &x,int father,int val)
{
x=++tot;
size[x]=;
key[x]=val;
pre[x]=father;
}
void Update_Rev(int x)
{
rev[x]^=;
swap(ch[x][],ch[x][]);
}
void Push_Up(int x)
{
size[x]=size[ch[x][]]+size[ch[x][]]+;
}
void Push_Down(int x)
{
if(rev[x]){
Update_Rev(ch[x][]);
Update_Rev(ch[x][]);
rev[x]=;
}
}
void build(int &x,int father,int l,int r)
{
if(l>r)return;
int mid=(l+r)>>;
NewNode(x,father,mid);
build(ch[x][],x,l,mid-);
build(ch[x][],x,mid+,r);
Push_Up(x);
}
void Init()
{
NewNode(root,,INF);
NewNode(ch[root][],root,INF);
build(Key_value,ch[root][],,n);
}
int Get_Kth(int x,int k)
{
Push_Down(x);//mistaken codes;
int t=size[ch[x][]]+;
if(t==k)return x;
if(t>k)return Get_Kth(ch[x][],k);
return Get_Kth(ch[x][],k-t);
}
void Rotate(int x,int kind)
{
int y=pre[x];
Push_Down(y);Push_Down(x);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y])ch[pre[y]][ch[pre[y]][]==y]=x;//unfixed;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
Push_Up(y);
}
void Splay(int x,int goal)
{
Push_Down(x);
while(pre[x]!=goal){
if(pre[pre[x]]==goal){
Push_Down(pre[x]);
Push_Down(x);
Rotate(x,ch[pre[x]][]==x);
}
else{
int y=pre[x];
int kind=ch[pre[y]][]==y;
Push_Down(pre[y]);//mistaken codes&&一遇到向下的操作就Push_Down;
Push_Down(y);//mistaken codes
Push_Down(x);//mistaken codes
if(ch[y][kind]==x){
Rotate(x,!kind);
Rotate(x,kind);
}
else{
Rotate(y,kind);
Rotate(x,kind);
}
}
}
Push_Up(x);
if(goal==)root=x;
}
void Rev(int l,int r)
{
Splay(Get_Kth(root,l),);
Splay(Get_Kth(root,r+),root);
Update_Rev(Key_value);
Push_Up(ch[root][]);
Push_Up(root);
}
void dfs(int x)
{
if(!x)return;
Push_Down(x);
dfs(ch[x][]);
if(<=key[x]&&key[x]<=n)printf("%d ",key[x]);
dfs(ch[x][]);
}
int main()
{
scanf("%d%d",&n,&m);
Init();
for(int x,y;m;m--){
scanf("%d%d",&x,&y);
Rev(x,y);
}dfs(root);
return ;
}

新splay模板上线,更简洁

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
inline int Rin(){
int x=,c=getchar(),f=;
for(;c<||c>;c=getchar())
if(!(c^))f=-;
for(;c>&&c<;c=getchar())
x=(x<<)+(x<<)+c-;
return x*f;
}
struct node{
node*ch[];
int r,s,v;bool b;
node (int v,node*k):v(v){
r=rand();
b=;s=;
ch[]=ch[]=k;
}
void exc(node*&x,node*&y)
{node*k=y;y=x;x=k;}
void pu(){s=ch[]->s+ch[]->s+;}
void pd(){
if(b){
b=;
exc(ch[],ch[]);
ch[]->b^=;
ch[]->b^=;
}
}
}*rt,*bf;
inline void rot(node*&o,int d){
node*k=o->ch[d^];o->ch[d^]=k->ch[d];k->ch[d]=o;
o->pu();k->pu();o=k;
}
inline int cmk(node*o,int k){
if(o->ch[]->s+==k)return -;
if(o->ch[]->s>=k)return ;
return ;
}
inline void ins(node*&o,int x){
if(o==bf){o=new node(x,bf);return;}
ins(o->ch[],x);
o->ch[]->r>o->r?rot(o,):o->pu();
}
inline void splay(node*&o,int k){
if(o==bf)return;
o->pd();int d=cmk(o,k);
if(!(d^))k-=o->ch[]->s+;
if(d!=-&&o->ch[d]!=bf){
node*p=o->ch[d];p->pd();
int dd=cmk(p,k);
if(dd!=-&&p->ch[dd]!=bf){
int kk=(dd?k-p->ch[]->s-:k);
splay(p->ch[dd],kk);
d^dd?rot(o->ch[d],d):rot(o,d^);
}
rot(o,d^);
}
}
inline node*mrg(node*l,node*r){
splay(l,l->s);l->ch[]=r;
l->pu();return l;
}
inline void slt(node*o,int k,node*&l,node*&r){
if(!k){l=bf;r=o;return;}
if(!(k^o->s)){l=o;r=bf;return;}
splay(o,k);l=o;r=o->ch[];
o->ch[]=bf;l->pu();
}
inline void ini(){
bf=new node(,);
bf->r=INT_MAX;bf->s=bf->v=;
bf->ch[]=bf->ch[]=bf;rt=bf;
ins(rt,);rt->r=-;
}
void opt(node*o){
if(o==bf)return;
o->pd();
opt(o->ch[]);
if(o->r!=-)printf("%d ",o->v);
opt(o->ch[]);
}
int n,m;
int main(){
n=Rin(),m=Rin();ini();
for(int i=;i<=n;i++)ins(rt,i);
while(m--){
int l=Rin(),r=Rin();
node *ll,*rr,*mm;
slt(rt,l,ll,rr);
slt(rr,r-l+,mm,rr);
mm->b^=;
rt=mrg(mrg(ll,mm),rr);
}
opt(rt);
return ;
}

bzoj3223 文艺平衡树 (treap or splay分裂+合并)的更多相关文章

  1. BZOJ3223文艺平衡树——非旋转treap

    此为平衡树系列第二道:文艺平衡树您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作: 翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...

  2. [BZOJ3223]文艺平衡树 无旋Treap

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一个 ...

  3. BZOJ3223 文艺平衡树(splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  4. 【模板】平衡树——Treap和Splay

    二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...

  5. 文艺平衡树(区间splay)

    文艺平衡树(luogu) Description 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列. 其中需要提供以下操作:翻转一个区间,例如原有序序列是 5\ 4\ 3\ 2\ ...

  6. JZYZOJ1998 [bzoj3223] 文艺平衡树 splay 平衡树

    http://172.20.6.3/Problem_Show.asp?id=1998 平衡树区间翻转的板子,重新写一遍,给自己码一个板子. #include<iostream> #incl ...

  7. [luogu3391][bzoj3223]文艺平衡树【splay】

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 分析 ...

  8. [bzoj3223]文艺平衡树(splay区间反转模板)

    解题关键:splay模板题. #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  9. [bzoj3223]文艺平衡树——splay

    题意 你应当编写一个数据结构,支持以下操作: 反转一个区间 题解 我们把在数组中的位置当作权值,这样原序列就在这种权值意义下有序,我们考虑使用splay维护. 对于操作rev[l,r],我们首先把l- ...

随机推荐

  1. Android图表类库:WilliamChart

    WilliamChart是基于Views的Android图表类库,帮助开发者在Android应用中实现折线图.柱状图和堆叠柱状图.数值发生变化时图表也会以动画的效果发生变化. At the momen ...

  2. iOS之属性修饰符 retain、strong和copy区别测试

    时不时会有点迷惑属性修饰符retain.strong.copy三者之间的区别,还是把测试过程记录下来好一点! 1.属性修饰符结论 2.给retain.strong.copy修饰的字符串属性赋值指针变化 ...

  3. Android 创建一个新的Activity

    本文转载自:http://www.cnblogs.com/wuyudong/p/5658020.html 新建一个项目,创建一个java类,继承自Activity 新建一个Android.xml文件: ...

  4. iOS关于CoreAnimation动画知识总结

    一:UIKit动画 在介绍CoreAnimation动画前先简单介绍一下UIKit动画,大部分简单的动画都可以使用UIKit动画实现,如果想实现更复杂的效果,则需要使用Core Animation了: ...

  5. View的事件体系

    View的滑动 实现手段 优点 缺点 备注 scrollTo/scrollBy 使用简单 只能滑动view的内容,并不会滑动view本身. 且内容超出view本身的布局范围部分的不会显示 不适合有交互 ...

  6. 安装SqlServer的时候性能计数器注册表配置单元一致性失败的解决办法

    http://www.2cto.com/database/201204/126772.html

  7. Ajax最详细的参数解析和场景应用

    4.1.定义和用法 AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 J ...

  8. 【shell--批量远程MySQL,执行命令】-【工作总结】

    昨天下班前,老板给了一批LOG数据库IP地址,需要统计LOG表里Message字段top 10的结果,并输出到一个excel文件里.抽查看了下,有两种格式的以当天日期结尾的表名.由于数量太多,时间紧迫 ...

  9. 0007《SQL必知必会》笔记03-汇总与分组数据

    1.有些时候需要数据的汇总值,而不是数据本身,比如对某些数据求和.计数.求最大最小值.求平均值,因此就有了5个聚集函数:AVE().COUNT().MAX().MIN().SUM(): (1)求平均值 ...

  10. jquery中attr和prop的区别、 什么时候用 attr 什么时候用 prop (转自 芈老头 )

    jquery中attr和prop的区别. 什么时候用 attr 什么时候用 prop   在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这 ...