LG3391 【模板】文艺平衡树(Splay)
题意
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
\(n,m \leq 1e5\)
分析
Splay Tree实现。
时间复杂度均摊\(O(\log n)\)
代码
#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read()
{
rg T data=0;
rg int w=1;
rg char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
{
data=data*10+ch-'0';
ch=getchar();
}
return data*w;
}
template<class T>il T read(rg T&x)
{
return x=read<T>();
}
typedef long long ll;
co int N=1e5+7;
namespace T
{
using std::swap;
int rt;
int fa[N],ch[N][2],siz[N];
bool rev[N];
void pushup(int x)
{
siz[x]=siz[ch[x][0]]+1+siz[ch[x][1]];
}
void pushdown(int x)
{
if(rev[x])
{
swap(ch[x][0],ch[x][1]);
rev[ch[x][0]]^=1;
rev[ch[x][1]]^=1;
rev[x]=0;
}
}
void rotate(int x,int&k)
{
int y=fa[x],z=fa[y],d=ch[y][0]==x;
if(y==k)
k=x;
else
{
if(ch[z][0]==y)
ch[z][0]=x;
else
ch[z][1]=x;
}
ch[y][d^1]=ch[x][d],fa[ch[y][d^1]]=y;
ch[x][d]=y,fa[y]=x,fa[x]=z;
pushup(x);
pushup(y);
}
void splay(int x,int&k)
{
while(x!=k)
{
int y=fa[x],z=fa[y];
if(y!=k)
{
if((ch[y][0]==x)^(ch[z][0]==y))
rotate(x,k);
else
rotate(y,k);
}
rotate(x,k);
}
}
void build(int l,int r,int f)
{
if(l>r)
return;
int mid=(l+r)/2;
if(mid<f)
ch[f][0]=mid;
else
ch[f][1]=mid;
fa[mid]=f,siz[mid]=1;
if(l==r)
return;
build(l,mid-1,mid);
build(mid+1,r,mid);
pushup(mid);
}
int find(int x,int k)
{
pushdown(x);
int s=siz[ch[x][0]];
if(k==s+1)
return x;
if(k<=s)
return find(ch[x][0],k);
else
return find(ch[x][1],k-s-1);
}
void rever(int l,int r)
{
int x=find(rt,l),y=find(rt,r+2);
splay(x,rt);
splay(y,ch[x][1]);
int z=ch[y][0];
rev[z]^=1;
}
}
using namespace T;
using namespace std;
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int n,m;
read(n),read(m);
rt=(n+3)/2;
build(1,n+2,rt);
while(m--)
{
int l,r;
read(l),read(r);
rever(l,r);
}
for(int i=2;i<=n+1;++i)
printf("%d ",find(rt,i)-1);
return 0;
}
LG3391 【模板】文艺平衡树(Splay)的更多相关文章
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- BZOJ3223 文艺平衡树(splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
- Tyvj P1729 文艺平衡树 Splay
题目: http://tyvj.cn/p/1729 P1729 文艺平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第二道:文艺平衡树 ...
- BZOJ 3223: Tyvj 1729 文艺平衡树(splay)
速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...
- bzoj3223Tyvj 1729 文艺平衡树 splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5644 Solved: 3362[Submit][Sta ...
随机推荐
- 定制kali linux
Kali Linux Ps: Kali发布撸~ 写了个如此装13的标题.这是一个Guide… 都是些基本操作撸.定制为王实推 ArchLinux. 各位看官继续………………………………………号外.L ...
- java基础(5)--流程控制结构
流程控制结构 if结构 当关系表达式为true时,执行语句 if(关系表达式){ //语句块 } if-else结构 当关系表达式为true时,执行语句块1,否则执行语句块2 if(关系表达式){ / ...
- Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
转自:https://blog.csdn.net/linxingliang/article/details/52263763 spring boot 自学笔记(三) Redis集成—RedisTemp ...
- Treflection01_Class对象_构造函数_创建对象
1. package reflectionZ; import java.lang.reflect.Constructor; import java.util.List; public class Tr ...
- 谈谈你对Glide和Picasso他们的对比的优缺点
1.Picasso和Glide的withi后面的参数不同 Picasso.with(这里只能传入上下文) . Glide.with,后面可以传入上下文,activity实例,FragmentA ...
- LeetCode第[2]题(Java):Add Two Numbers (链表相加)——Medium
题目难度:Medium 题目: You are given two non-empty linked lists representing two non-negative integers. The ...
- redis key 通配符 查询相应的key
keys pattern 查询相应的key 在redis里,允许模糊查询key 有3个通配符 *, ? ,[] *: 通配任意多个字符 ?: 通配单个字符 []: 通配括号内的某1个字符 redis ...
- spring3: 依赖和依赖注入-xml配置-DI的配置
3.1.1 依赖和依赖注入 传统应用程序设计中所说的依赖一般指“类之间的关系”,那先让我们复习一下类之间的关系: 泛化:表示类与类之间的继承关系.接口与接口之间的继承关系: 实现:表示类对接口的实现 ...
- ubuntu16.04 添加中文ibus输入法
ubuntu版本 16.04 在terminal 输入命令 sudo apt-get install ibus-pinyin sudo apt-get ibus-setup 设置 选择拼音,添加选择 ...
- Ajax-07 基于Ajax实现跨域请求
跨域 跨域名访问,如c1.com域名向c2.com域名发送请求 本质:浏览器存在同源策略机制,同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性. django服务端准备 url. ...