[洛谷P3391]【模板】文艺平衡树(Splay)
题目大意:给定一个$1\sim n$的序列,每次翻转一个区间,输出最后的序列。
解题思路:Splay的区间翻转操作。我借此打了个Splay的模板(运用内存池,但有些功能不确定正确,例如单点插入)。
大致思路就是,每次找到$l−1$和$r+1$两个节点,把$l−1$旋转到根,$r+1$旋转到根的右子树,则根的右子树的左子树就是$l,r$的区间。
对于翻转一个区间,直接打上标记,访问到这个节点时,下传标记并交换两个儿子节点。
注意访问$l−1$,$r+1$时可能访问到$0$和$n+1$,所以要多开两个节点。
最后输出即可。
C++ Code:
#include<bits/stdc++.h>
inline int readint(){
char c=getchar();
for(;!isdigit(c);c=getchar());
int d=0;
for(;isdigit(c);c=getchar())
d=(d<<3)+(d<<1)+(c^'0');
return d;
}
int n;
template<typename T>
class Splay{
private:
struct node{
T value;
int sz;
bool rotag;
node *fa,*ch[2];
};
public:
node* rt;
int n;
private:
node a[1000000],*stk[1000000];
int top;
inline void pushdown(node* t){
if(t->rotag){
node* x=t->ch[0];
t->ch[0]=t->ch[1];
t->ch[1]=x;
if(t->ch[0]!=NULL)
t->ch[0]->rotag^=1;
if(t->ch[1]!=NULL)
t->ch[1]->rotag^=1;
t->rotag=0;
}
}
inline node* newnode(T v,node* f){
node* now=stk[--top];
now->sz=1;
now->value=v;
now->fa=f;
now->ch[0]=now->ch[1]=NULL;
now->rotag=false;
return now;
}
inline void dispose(node*&t){stk[top++]=t;t=NULL;}
inline int findson(node* f,node* s){
return(f->ch[1]==s)?1:0;
}
inline void update(node* t){
t->sz=1;
if(t->ch[0]!=NULL)t->sz+=t->ch[0]->sz;
if(t->ch[1]!=NULL)t->sz+=t->ch[1]->sz;
}
inline void rotate(node* t){
node* f=t->fa;
node* g=f->fa;
int a=findson(f,t),b=a^1;
if(t->ch[b]!=NULL)t->ch[b]->fa=f;
f->ch[a]=t->ch[b];
t->ch[b]=f;
f->fa=t;
t->fa=g;
if(g!=NULL)g->ch[findson(g,f)]=t;else
rt=t;
update(f);
update(t);
}
public:
Splay(){
for(int i=0;i<1000000;++i)stk[i]=&a[i];
top=1000000;
}
inline void splay(node* t,node* p){
if(t==NULL)return;
pushdown(t);
while(t->fa!=p){
node* f=t->fa;
node* g=f->fa;
if(g==p)rotate(t);else
if(findson(g,f)!=findson(f,t))
rotate(t),rotate(t);else
rotate(f),rotate(t);
}
}
inline node* find(node* now,int y){
while(now!=NULL){
pushdown(now);
int t;
if(now->ch[0]==NULL)t=1;else
t=now->ch[0]->sz+1;
if(t==y)return now;
if(t<y)y-=t,now=now->ch[1];else
now=now->ch[0];
}
}
inline void build(node*& p,int l,int r,node* f=NULL){
if(l>r)return;
int mid=l+r>>1;
p=newnode(mid,f);
build(p->ch[0],l,mid-1,p);
build(p->ch[1],mid+1,r,p);
update(p);
}
inline void insert(T val){
if(rt==NULL)rt=newnode(val,NULL);
int x;
for(node* t=rt;t;t=t->son[x]){
x=val>=t->value?1:0;
if(t->ch[x]==NULL){t->son[x]=newnode(val,t);splay(t->son[x],rt);break;}
}
}
inline void rev(node* p,int l,int r){
node* pre=find(p,l),*suc=find(p,r);
splay(pre,NULL);
splay(suc,rt);
if(rt->ch[1]->ch[0]!=NULL)
rt->ch[1]->ch[0]->rotag^=1;
}
inline void print(node* p){
if(p==NULL)return;
pushdown(p);
print(p->ch[0]);
if(p->value>1&&p->value<n+2)printf("%d ",p->value-1);
print(p->ch[1]);
}
inline void build(int nn){
n=nn;
build(rt,1,n+2);
}
};
Splay<int>Tree;
int main(){
n=readint();int m=readint();
Tree.build(n);
while(m--){
int l=readint(),r=readint();
Tree.rev(Tree.rt,l,r+2);
}
Tree.print(Tree.rt);
return 0;
}
[洛谷P3391]【模板】文艺平衡树(Splay)的更多相关文章
- 洛谷.3391.[模板]文艺平衡树(Splay)
题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...
- 【洛谷P3391】文艺平衡树——Splay学习笔记(二)
题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...
- 洛谷.3369.[模板]普通平衡树(Splay)
题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- 洛谷 P3391 模板Splay
#include<bits/stdc++.h> using namespace std; #define maxn 200000 int read() { ,w=; ;ch=getchar ...
- 【洛谷P3369】普通平衡树——Splay学习笔记(一)
二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...
- 洛谷.3369.[模板]普通平衡树(fhq Treap)
题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...
- 洛谷 P3391 【模板】文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
随机推荐
- Rmq Problem mex
求区间mex.莫队可做. 但如果强制在线,就可以用主席树做. 建立权值线段树,找每个数最后一次出现的位置.查询的时候找第r棵线段树最近出现位置在l之前的最小数即可.update的时候可以update这 ...
- liunx 里面安装phpstudy环境s
ngixwget -c http://lamp.phpstudy.net/phpstudy.bin chmod +x phpstudy.bin #权限设置 ./phpstudy.bin #运行 ...
- prim求最小生成树
一直以来只会Kruskal prim和dijkstra很像 只不过prim维护的是最短的边,而dijkstra维护的是最短的从起点到一个点的路径 同时prim要注意当前拓展的边是没有拓展过的 可以用堆 ...
- CAD 二次开发----- 块
/// <summary> /// 插入一个块参照到CAD图形中 /// </summary> /// <param name="spaceId"&g ...
- docker 下载镜像 ( 以 mysql为例 )
一.官方镜像仓库 https://hub.docker.com/explore/ 二.常用操作 三.使用命令查看 mysql [root@localhost fw]# docker search my ...
- 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!
Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: ...
- hdu 2577 模拟
#include<stdio.h> #define N 200 char s[N]; int judgeup(char c) { if(c>='A'&&c<=' ...
- PHP 防xss攻击
PHP直接输出html的,可以采用以下的方法进行过滤: 1.htmlspecialchars函数 2.htmlentities函数 3.HTMLPurifier.auto.php插件 4.Remove ...
- JeeSite(2):导入数据,进入系统
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/50954485 未经博主同意不得转载. 博主地址是:http://blog.csd ...
- 有关计数问题的DP 划分数
有n个无差别的物品,将它们划分成不超过m组.求出划分方法数模M的余数. 输入: 3 4 10000 输出: 4(1+1+2=1+3=2+2=4) 定义:dp[i][j] = j的i划分的总数 #inc ...