luogu3391
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
1 3
1 3
1 4
Sample Output
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的更多相关文章
- [luogu3391][bzoj3223]文艺平衡树【splay】
题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 分析 ...
- [luogu3391][文艺平衡树]
题目链接 思路 splay区间操作的裸题. 假如要对l-r这段区间操作,那么就先把l-1伸展到根节点,然后把r +1伸展为根的儿子.这样r + 1的左儿子就是要操作的区间了.只要在上面打上标记,以后每 ...
- [luogu3391] 【模板】文艺平衡树(fhq-treap反转区间)
解题关键:无旋treap模板. #include<iostream> #include<cstdio> #include<cstring> #include< ...
- splay模板三合一 luogu2042 [NOI2005]维护数列/bzoj1500 [NOI2005]维修数列 | poj3580 SuperMemo | luogu3391 【模板】文艺平衡树(Splay)
先是维修数列 题解看这里,但是我写的跑得很慢 #include <iostream> #include <cstdio> using namespace std; int n, ...
- $LCT$初步
\(\rm{0x01}\) 闲话 · \(LCT\)的用途以及具体思路 LCT是啥?百度一下的话--貌似是一种检查妇科病的东西?Oier的口味可是真不一般啊 咳,其实在我最近只是浅浅地学了一部分的基础 ...
随机推荐
- 使用webstrom开发react-native时react-native代码会出现红色下划线的解决方法
问题:使用webstrom开发react-native时react-native代码会出现红色下划线的解决方法 解决方法:webstrom ->preferences->Laugrange ...
- 长期招收linux驱动工程师
公司:宝存科技 工作内容: 1.负责企业级ssd的feature设计和开发工作 2.负责ftl算法的设计及开发 3.排查客户问题 任职要求: 1.精通C语言 2.熟练掌握linux操作系统使用 3.熟 ...
- git 忽略无效解决办法
有时候发现git提交了一些我们不需要提交的内容,这时候第一反应是加个忽略: https://github.com/github/gitignore 宇宙神器VS: https://github.com ...
- 深入理解Spring Boot数据源与连接池原理
Create by yster@foxmail.com 2018-8-2 一:开始 在使用Spring Boot数据源之前,我们一般会导入相关依赖.其中数据源核心依赖就是spring‐boot‐s ...
- 分布式架构的基石.简单的 RPC 框架实现(JAVA)
前言 RPC 的全称是 Remote Procedure Call,它是一种进程间通信方式.允许像调用本地服务一样调用远程服务. 学习来源:<分布式系统架构:原理与实践> - 李林锋 1. ...
- golang中的context包
标准库的context包 从设计角度上来讲, golang的context包提供了一种父routine对子routine的管理功能. 我的这种理解虽然和网上各种文章中讲的不太一样, 但我认为基本上还是 ...
- NanoFabric-ServiceFabric 操作手册
service-fabric-52abp-ocelot A Service Fabric sample with a Frontend, one API Gateway and 52abp Micro ...
- OSGI嵌入tomcat应用服务器(gem-web)——资源下载
Gem-Web官网介绍: 官网地址:https://www.eclipse.org/gemini/web/download/milestones.php 1.1. 官方正式发布版 https://ww ...
- Entity Framework Core系列之什么是Entity Framework Core
前言 Entity Framework Core (EF Core)是微软推荐的基于.NET Core framework的应用程序数据访问技术.它是轻量级,可扩展并且支持跨平台开发.EF Core是 ...
- Python练习-2
#1.使用while循环输入 1 2 3 4 5 6 8 9 10 count = 0 while count < 10: count += 1 # count = count + 1 if c ...