题目大意:给定一个$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)的更多相关文章

  1. 洛谷.3391.[模板]文艺平衡树(Splay)

    题目链接 //注意建树 #include<cstdio> #include<algorithm> const int N=1e5+5; //using std::swap; i ...

  2. 【洛谷P3391】文艺平衡树——Splay学习笔记(二)

    题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...

  3. 洛谷.3369.[模板]普通平衡树(Splay)

    题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...

  4. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

  5. 洛谷 P3391 模板Splay

    #include<bits/stdc++.h> using namespace std; #define maxn 200000 int read() { ,w=; ;ch=getchar ...

  6. 【洛谷P3369】普通平衡树——Splay学习笔记(一)

    二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...

  7. 洛谷.3369.[模板]普通平衡树(fhq Treap)

    题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...

  8. 洛谷 P3391 【模板】文艺平衡树(Splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  9. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

  10. [洛谷P3391] 文艺平衡树 (Splay模板)

    初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...

随机推荐

  1. 为什么在JavaScript中0.1+0.2不等于0.3?

    0.1+0.2不等于0.3?是不是有点颠覆你的认知,但是,在js中,是真实存在的! console.log(0.1+0.2); // 0.30000000000000004 其实这都是因为浮点数运算的 ...

  2. HDU-6217 BBP Formula 脑洞

    题目链接:https://cn.vjudge.net/problem/HDU-6217 题意 已知: \[ \pi = \sum_{k=0}^{\infty }\frac{1}{16^{k}}(\fr ...

  3. nmcli connection modify eth1 ipv4.addr "192.168.31.23" ipv4.method manual

    nmcli connection modify eth1 ipv4.addr "192.168.31.23/24" ipv4.method manual 修改IP地址

  4. [转载]解决/usr/bin/ld: cannot find -lxxx

    在linux环境编译应用程式或lib的source code时常常会出现如下的错误讯息: /usr/bin/ld: cannot find -lxxx 这些讯息会随着编译不同类型的source cod ...

  5. --without-v4l ,make clean, 重新make即可。

    --without-v4l ,make clean, 重新make 2011-02-27 17:38 Error: X11 support required for GUI compilation

  6. appium运行from appium import webdriver 提示most recent call last

    这是因为首次启动提示没有APPIUM 模块,CMD 执行 :pip3 install Appium-Python-Client

  7. SVN中各种符号箭头含义

    黄色感叹号(有冲突): -- 这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许 你提交,防止你的提交覆盖 ...

  8. windows系统中软件开发常用的软件

    1.windwos快速打开控制面板:热键+r打开运行框,输入control就打开windows的控制面板了 2.windows自带的远程桌面控制系统:mstsc -Microsoft terminal ...

  9. HDU 4357

    这道题写起来没难度,但这种题确实很难,这种字符串的题难在证明.以后也要注意. 奇偶性不同的字符串肯定不能转换,因为每一次交换都是字符串的和增加2. 当字符串长度为2时,可以模拟交换,最多26次. 否则 ...

  10. MYSQL Training: MySQL I

    让以admin身份登录.源代码: 非常easy的注入 在username输入 admin' OR '1'='1 OK.