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的口味可是真不一般啊 咳,其实在我最近只是浅浅地学了一部分的基础 ...
随机推荐
- 【vue】iView-admin后台管理系统
1.目录手册解析 2.跨域解决(axios访问服务器由于前端地址和后端地址不同源会出现跨域问题) 实例2 3.动态菜单导航+权限 4.webstorm 配置 Eslint 代码检查和自动修复
- dubbo源码阅读
http://seekheap.com/posts/dubbo/dubbo-src-01-overview-and-debug-environment.html 先占坑
- Java使用Future设置方法超时
1.使用线程包 java.util.concurrent.Future 2.Future代表一个异步计算的结果. 它提供了方法来检查是否计算已经完成,还是正在计算而处于等待状态,并且也提供了获取计算结 ...
- redis底层设计(一)——内部数据结构
redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...
- [原创]Sharding-Sphere之Proxy初探
大家好,拓海(https://github.com/tuohai666)今天为大家分享Sharding-Sphere推出的重磅产品:Sharding-Proxy!在之前闪亮登场的Sharding-Sp ...
- AtCoder Beginner Contest 116 D - Various Sushi (贪心+栈)
D - Various Sushi Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement ...
- stark组件配置,二层URL
1.django的admin配置 2 stark组件开发 3.2层url分发 4.小结 1.django的admin配置 model.py from django.db import models # ...
- PHP开发编码规范
(转载:https://blog.csdn.net/alexdream/article/details/2213313) 这些年来多从事Linux下PHP和C相关的开发,带过很多项目和团队,下面是根据 ...
- 什么是arp协议?
转自:https://blog.csdn.net/tigerjibo/article/details/7351992 ARP (Address Resolution Protocol) 是个地址解析协 ...
- React Native之微信分享(iOS Android)
React Native之微信分享(iOS Android) 在使用React Native开发项目的时候,基本都会使用到微信好友或者微信朋友圈分享功能吧,那么今天我就带大家实现以下RN微信好友以及朋 ...