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的口味可是真不一般啊 咳,其实在我最近只是浅浅地学了一部分的基础 ...
随机推荐
- day95
Linux基本部署配置及常见扩展应用 Linux软件包安装方法 1. 安装: 整个安装过程可以分为以下几步: 1) 取得应用软件:通过下载.购买光盘的方法获得: 2)解压缩文件:一般tar包,都会再做 ...
- c# 利用百度图像处理【人像分割】一键抠图
百度AI开放平台-人像分割: http://ai.baidu.com/tech/body/seg 注意本文后面的话,百度这个技术效果太差劲了,国外这 https://www.remove.bg/ 个比 ...
- JavaEE学习之Spring Security3.x——模拟数据库实现用户,权限,资源的管理
一.引言 因项目需要最近研究了下Spring Security3.x,并模拟数据库实现用户,权限,资源的管理. 二.准备 1.了解一些Spring MVC相关知识: 2.了解一些AOP相关知识: 3. ...
- ML.NET 示例:推荐之场感知分解机
写在前面 准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正. 如果有朋友对此感兴趣,可以加入我:https://github.com/fei ...
- 如何利用snmp协议发现大型复杂环境的网络拓扑
参考文献:http://blog.51cto.com/13769225/2121431 获取指标参考下图: 1.取接口描述(指定VLAN号) 命令:snmpwalk -v 2c -c Cvicse12 ...
- python-Requests + 正则表达式爬取猫眼电影
github: https://github.com/LXL-YAN/Requests_Regular-Expressions-Crawl-CatEye-Movies
- Feel Good POJ - 2796 (前缀和+单调栈)(详解)
Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...
- 逻辑回归为什么用sigmoid函数
Logistic回归目的是从特征学习出一个0/1分类模型,而这个模型是将特性的线性组合作为自变量,由于自变量的取值范围是负无穷到正无穷. 因此,使用logistic函数(或称作sigmoid函数)将自 ...
- 【转】Restful是什么
REST的概念是什么 维基百科 表现层状态转换(REST,英文:Representational State Transfer)是Roy Thomas Fielding博士于2000年在他的博士论文 ...
- node-cookie-parserDemo
let express = require('express'); let app = new express(); let cookieParser = require('cookie-parser ...