题目背景

这是一道经典的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. (转)android - anim translate中 fromXDelta、toXDelta、fromYDelta、toXDelta属性

    2012-03-23 15:51 16144人阅读 评论(5) 收藏 举报 android <set xmlns:android="http://schemas.android.com ...

  2. mysql5.7不支持0000-00-00 00:00:00的默认时间设置

    方案一: 数据不多的话把原有的5.53的数据改一下符合要求(数据库时间字段里千万不能出现0000-00-00 00:00:00这样的值),然后导出.sql文件,导出的.sql文件里把 DEFAULT ...

  3. 求小球反弹高度,及落地过程中经过的路程~~~java代码

    总结:这种思想,不是一想就突然出现在脑海里的 package com.c2; //题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半: //再落下,求它在 第10次落地时,共经过多少米?第 ...

  4. Windows Media Player 打不开怎么办

    1. 右键VS工具箱的空白处; 2. 打开工具箱, 选择com组件→找到windows media player 3. 如果这里没有发现 windows Media Player怎么办? , 以win ...

  5. windows网络服务之配置网络负载均衡(NLB)群集

    O首页51CTO博客我的博客 搜索 每日博报 社区:学院论坛博客下载更多            登录注册 家园 学院 博客 论坛 下载 自测 门诊 周刊 读书 技术圈 曾垂鑫的技术专栏 http:// ...

  6. wordpress 学习笔记

    (1) __()函数 function __( $text, $domain = 'default' ) { return translate( $text, $domain ); } 返回一个字符串 ...

  7. Collision Detection

    [Collision Detection] Collision Detection是Rigidbody中的一个属性.所以显然Collision Detection指定的类型只在Rigidbody之间才 ...

  8. mfs实际操作教程

    9. 实际操作案例 9.1 默认的垃圾回收时间是86400,存在一种可能性是垃圾还没回收完,你的存储容量就暴掉了.(案例提供者shinelian) 方案1:设置垃圾回收时间,积极监控存储容量.     ...

  9. springBoot集成 quartz动态定时任务

    项目中需要用到定时任务,考虑了下java方面定时任务无非就三种: 用Java自带的timer类.稍微看了一下,可以实现大部分的指定频率的任务的调度(timer.schedule()),也可以实现关闭和 ...

  10. c++策略模式(Strategy Method)

    别人的博客再讲策略模式时都会讲三国,策略类就是赵云的锦囊,锦囊里装着若干妙计.在打仗时想要用什么妙计,直接从锦囊里去取. 锦囊类: class context { public: context(IS ...