题目背景

这是一道经典的Splay模板题——文艺平衡树。

题目描述

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

输入输出格式

输入格式:
第一行为n,m n表示初始序列有n个数,这个序列依次是 (1,2, \cdots n-1,n)(1,2,⋯n−1,n) m表示翻转操作次数

接下来m行每行两个数 [l,r][l,r] 数据保证 1 \leq l \leq r \leq n 1≤l≤r≤n

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

输入输出样例

输入样例#1:
5 3
1 3
1 3
1 4
输出样例#1: 
4 3 2 1 5

题解:打标记的splay!调了一个上午终于调出来了!继续挖坑~

代码如下:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100010
using namespace std; struct Splay
{
int key[N],son[N][],size[N],fa[N],tag[N],rt,sz,pos;
inline void push_up(int x)
{
size[x]=size[son[x][]]+size[son[x][]]+;
}
inline void push_down(int x)
{
if(!tag[x])
{
return;
}
tag[x]=;
tag[son[x][]]^=;
tag[son[x][]]^=;
swap(son[x][],son[x][]);
}
inline void rotate(int x)
{
int y=fa[x],z=fa[y],k=(son[y][]==x);
son[z][son[z][]==y]=x;
fa[x]=z;
son[y][!k]=son[x][k];
fa[son[x][k]]=y;
son[x][k]=y;
fa[y]=x;
push_up(y);
}
inline void splay(int x,int goal)
{
for(push_down(x); fa[x]!=goal; rotate(x))
{
int y=fa[x],z=fa[y];
if(z!=goal)
{
(son[y][]==x)^(son[z][]==y)?rotate(x):rotate(y);
}
}
push_up(x);
if(!goal)
{
rt=x;
}
}
inline int kth(int k)
{
for(int x=rt;;)
{
push_down(x);
int y=son[x][];
if(k>size[y]+)
{
k-=size[y]+;
x=son[x][];
}
else
{
if(k>size[y])
{
return x;
}
else
{
x=y;
}
}
}
}
inline void reverse(int l,int r)
{
if(l>r)
{
swap(l,r);
}
int x=kth(l),y=kth(r+);
splay(x,);
splay(y,x);
tag[son[y][]]^=;
}
inline int build(int *a,int father,int l,int r)
{
if(l>r)
{
return ;
}
int mid=(l+r)>>,x=++sz;
fa[x]=father;
key[x]=a[mid];
son[x][]=build(a,x,l,mid-);
son[x][]=build(a,x,mid+,r);
push_up(x);
return x;
}
inline void put(int x)
{
push_down(x);
if(son[x][])
{
put(son[x][]);
}
if(key[x]>-1e9&&key[x]<1e9)
{
printf("%d ",key[x]);
}
if(son[x][])
{
put(son[x][]);
}
}
} splay1; int main()
{
int n,m,l,r,a[]; scanf("%d%d",&n,&m);
for(int i=; i<=n+; i++)
{
a[i]=i-;
}
a[]=-1e9;
a[n+]=1e9;
splay1.rt=splay1.build(a,,,n+);
for(int i=; i<=m; i++)
{
scanf("%d%d",&l,&r);
splay1.reverse(l,r);
}
splay1.put(splay1.rt);
}

BZOJ3223 文艺平衡树(splay)的更多相关文章

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

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

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

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

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

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

  4. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

  5. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

  6. BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3595  Solved: 2029[Submit][Sta ...

  7. bzoj3223 文艺平衡树 (treap or splay分裂+合并)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 3313  Solved: 1883 [Submit][S ...

  8. Tyvj P1729 文艺平衡树 Splay

    题目: http://tyvj.cn/p/1729 P1729 文艺平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第二道:文艺平衡树 ...

  9. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

随机推荐

  1. GitFlow在命令行的使用

    gitflow安装 在命令行直接使用yum安装 yum install gitflow 如果本地的yum源中不存在gitflow,可以尝试添加EPEL源 CentOS6.5: # 下载 wget ht ...

  2. Linux Shell 1>/dev/null 2>&1 含义

    shell中可能经常能看到:echo log > /dev/null 2>&1 命令的结果可以通过%>的形式来定义输出 /dev/null :代表空设备文件>  :代表 ...

  3. Navicat for MySQL使用手记(下)--实现自动备份数据库

    五.备份和还原MySQL数据库 在数据库的管理中,备份和还原是必须做认真做的事情,如果疏忽或者做粗糙了,那么一旦数据库故障后果不堪设想,所以Navicat同样也有备份和还原的功能,相比较创建功能,其备 ...

  4. SQL Server 2008怎么自动备份数据库

    在SQL Server 2008数据库中.为了防止数据的丢失我们就需要按时的来备份数据库了.要是每天都要备份的话,人工备份会很麻烦的,自动备份的话就不需要那么麻烦了,只 要设置好了,数据库就会自动在你 ...

  5. windows Server 2008各版本有何区别?

    windows Server 2008有几个版本,先一一列出来把: Windows Server 2008 Standard Edition     (标准版) Windows Server 2008 ...

  6. Tuple、list的区别以及dict和set

    元组(Tuple): 定义方法:使用小括号() 使用方法: count:可以统计某个元组段在整个元组中出现的次数 index:可以查询某个元组段在整个元组中的元组号 name_tuple = ('xi ...

  7. leetcode454

    public class Solution { public int FourSumCount(int[] A, int[] B, int[] C, int[] D) { var dic = new ...

  8. RAD 10 新控件 TSearchBox TSplitView

    Seattle新控件 1.TSearchBox Events OnInvokeSearch published Occurs when the search indicator button is c ...

  9. wpa_supplicant移植与使用(转)

    下载wpa_supplicant最新版和openssl(编译wpa_supplicant需要openssl的库) 我这里使用的是wpa_supplicant-0.7.3.tar.gz和openssl- ...

  10. swift之xib关联UIView

    有点坑爹,设置file owner 不行,搞了一早上,来说下怎么关联吧 自定义UIView要重写 required init(coder aDecoder: NSCoder) { super.init ...