P3391 【模板】文艺平衡树(Splay)

题目背景

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

题目描述

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是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

sol:闲的蛋疼打了一遍splay板子,还挂了一发。Ps:注意下传Rev标记

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,inf=0x3f3f3f3f;
int n,m;
namespace Pht
{
int Points=,Root;
int Child[N][],Parent[N];
int Size[N];
int Quanzhi[N];
bool Rev[N]; inline void Init();
inline int Check(int x);
inline void PushUp(int x);
inline void PushDown(int x);
inline void Rotate(int x);
inline void Splay(int At,int To);
inline void Insert(int Val);
inline int Ask_Kth(int Id);
inline void Reverse(int l,int r);
inline void Output(int Now);
inline void Solve(); inline void Init()
{
int i;
Insert(-inf);
for(i=;i<=n;i++) Insert(i);
Insert(inf);
}
inline int Check(int x)
{
return (Child[Parent[x]][]==x)?:;
}
inline void PushUp(int x)
{
Size[x]=Size[Child[x][]]+Size[Child[x][]]+;
}
inline void PushDown(int x)
{
if(!Rev[x]) return;
swap(Child[x][],Child[x][]);
Rev[x]=;
Rev[Child[x][]]^=;
Rev[Child[x][]]^=;
}
inline void Rotate(int x)
{
int y,z,oo;
y=Parent[x];
z=Parent[y];
oo=Check(x);
Child[y][oo]=Child[x][oo^]; Parent[Child[x][oo^]]=y;
Child[z][Check(y)]=x; Parent[x]=z;
Child[x][oo^]=y; Parent[y]=x;
PushUp(x); PushUp(y);
}
inline void Splay(int At,int To)
{
while(Parent[At]!=To)
{
int Father=Parent[At];
if(Parent[Father]==To)
{
Rotate(At);
}
else if(Check(At)==Check(Father))
{
Rotate(Father); Rotate(At);
}
else
{
Rotate(At); Rotate(At);
}
}
if(To==) Root=At;
}
inline void Insert(int Val)
{
int Now=Root,Par=;
while(Now)
{
Par=Now;
Now=Child[Now][(Val>Quanzhi[Now])?:];
}
Now=++Points;
if(Par)
{
Child[Par][(Val>Quanzhi[Par])?:]=Now;
}
Parent[Now]=Par;
Child[Now][]=Child[Now][]=;
Quanzhi[Now]=Val;
Size[Now]=;
Splay(Now,);
}
inline int Ask_Kth(int Id)
{
int Now=Root;
while(Now)
{
PushDown(Now);
if(Size[Child[Now][]]>=Id)
{
Now=Child[Now][];
}
else if(Size[Child[Now][]]+==Id)
{
return Now;
}
else
{
Id=Id-Size[Child[Now][]]-;
Now=Child[Now][];
}
}
}
inline void Reverse(int l,int r)
{
int ll=Ask_Kth(l),rr=Ask_Kth(r+);
Splay(ll,);
Splay(rr,ll);
int Pos=Child[rr][];
Rev[Pos]^=;
}
inline void Output(int Now)
{
PushDown(Now);
if(Child[Now][]) Output(Child[Now][]);
if(Quanzhi[Now]>=&&Quanzhi[Now]<=n) W(Quanzhi[Now]);
if(Child[Now][]) Output(Child[Now][]);
}
inline void Solve()
{
Init();
while(m--)
{
int l=read(),r=read();
Reverse(l,r);
}
Output(Root);
}
}
int main()
{
R(n); R(m);
Pht::Solve();
return ;
}
/*
input
5 3
1 3
1 3
1 4
output
4 3 2 1 5
*/

luogu3391的更多相关文章

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

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

  2. [luogu3391][文艺平衡树]

    题目链接 思路 splay区间操作的裸题. 假如要对l-r这段区间操作,那么就先把l-1伸展到根节点,然后把r +1伸展为根的儿子.这样r + 1的左儿子就是要操作的区间了.只要在上面打上标记,以后每 ...

  3. [luogu3391] 【模板】文艺平衡树(fhq-treap反转区间)

    解题关键:无旋treap模板. #include<iostream> #include<cstdio> #include<cstring> #include< ...

  4. splay模板三合一 luogu2042 [NOI2005]维护数列/bzoj1500 [NOI2005]维修数列 | poj3580 SuperMemo | luogu3391 【模板】文艺平衡树(Splay)

    先是维修数列 题解看这里,但是我写的跑得很慢 #include <iostream> #include <cstdio> using namespace std; int n, ...

  5. $LCT$初步

    \(\rm{0x01}\) 闲话 · \(LCT\)的用途以及具体思路 LCT是啥?百度一下的话--貌似是一种检查妇科病的东西?Oier的口味可是真不一般啊 咳,其实在我最近只是浅浅地学了一部分的基础 ...

随机推荐

  1. burnside+polya 整理

    先定义几个含义和符号:起始状态/方法/位置/元素/:以染色为例,起始状态是所有的染色方案,方法是以起始状态所有染色方案为基准转变为新的染色情景的操作(如旋转),位置则必须是没有任何染色效果的抽象空间, ...

  2. Ubuntu使用小技巧

    1. Ubuntu下自由截图 Ubuntu下使用PrintScreen按键可以截取整个屏幕,但是很多时候并不需要那么多内容,还需要对图片进行编辑. 这时候就需要截图时,有矩形选择,更符合要求. 进入S ...

  3. IDE安装Lombok插件提高开发效率

    Lombok官方api:https://projectlombok.org/features/index.html 使用lombok之后,省去了许多没必要的get,set,toString,equal ...

  4. ffmpeg源码安装

    官网下载地址 http://www.ffmpeg.org/download.html https://sourceforge.net/projects/opencore-amr/ 参考资料:官网及以下 ...

  5. 4月27号开学! 第6期《jmeter实战接口自动化+性能》课程,零基础也能学

    2019年 第6期<jmeter实战接口自动化+性能>课程,4月27号开学! 主讲老师:飞天小子 上课方式:QQ群视频在线教学 本期上课时间:4月27号-6月9号,每周六.周日晚上20:0 ...

  6. Mac 下编译安装 php-5.6

    1.安装 PHP 1.1 下载源码包 http://php.net/get/php-5.6.35.tar.bz2/from/a/mirror 1.2 编译&安装 ./configure --p ...

  7. 单链表的基本操作--c++

    #include <iostream> //实现单链表的建立,测长和打印 #include <string> using namespace std; struct node ...

  8. codeforces#1090 D. New Year and the Permutation Concatenation(打表找规律)

    题意:给出一个n,生成n的所有全排列,将他们按顺序前后拼接在一起组成一个新的序列,问有多少个长度为n的连续的子序列和为(n+1)*n/2 题解:由于只有一个输入,第一感觉就是打表找规律,虽然表打出来了 ...

  9. C#跨进程读取listview控件中的数据

    http://www.cnblogs.com/Charltsing/p/slv32.html 欢迎交流:QQ564955427 读取标准的32位listview控件中的数据,网上已经有很多代码了.今天 ...

  10. Vue基础(ES6)

      起步 1.扎实的HTML/CSS/Javascript基本功,这是前置条件. 2.不要用任何的构建项目工具,只用最简单的<script>,把教程里的例子模仿一遍,理解用法.不推荐上来就 ...