题目链接

  ddosvoid和自为风月马前卒教了我这道题

  他们好强啊

  如果我们要反转区间[l,r]

  我们首先把l的前驱旋转到根节点

  再把r的后继旋转到根节点的右儿子

  那么此时根节点的右儿子的左儿子所代表的就是区间l,r

  具体为啥不知道

  然后可以给splay的节点打标记,就像线段树一样

inline void pushdown(int x){
if(!tree[x].tag) return;
swap(tree[x].e[],tree[x].e[]);
tree[tree[x].e[]].tag^=;
tree[tree[x].e[]].tag^=;
tree[x].tag=;
}

  这就是标记下传

  

#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using std::swap; inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} int root;int n;int m; struct Splay{
struct node{
int e[],size,fa;
bool tag;
}tree[];
inline int iden(int x){ return x==tree[tree[x].fa].e[]; }
inline void update(int x){ tree[x].size=tree[tree[x].e[]].size+tree[tree[x].e[]].size+; }
inline void connect(int x,int fa,int how){ tree[x].fa=fa; tree[fa].e[how]=x; }
void rotate(int x){
int y=tree[x].fa; if(y==root) root=x;
int r=tree[y].fa;
//pushdown(r); pushdown(y); pushdown(x);
int sony=iden(x); int sonr=iden(y);
int b=tree[x].e[sony^];
connect(b,y,sony);
connect(y,x,sony^);
connect(x,r,sonr);
update(y); update(x);
}
inline void pushdown(int x){
if(!tree[x].tag) return;
swap(tree[x].e[],tree[x].e[]);
tree[tree[x].e[]].tag^=;
tree[tree[x].e[]].tag^=;
tree[x].tag=;
}
void splay(int pos,int to){
while(tree[pos].fa!=to){
if(tree[tree[pos].fa].fa==to) rotate(pos);
else
if(iden(pos)==iden(tree[pos].fa)){ rotate(tree[pos].fa); rotate(pos); }
else { rotate(pos); rotate(pos); }
}
update(pos);
}
int build(int l,int r){
if(l>r) return ;
int mid=(l+r)>>;
int lson=build(l,mid-);
connect(lson,mid,);
int rson=build(mid+,r);
connect(rson,mid,);
tree[mid].tag=;
update(mid);
return mid;
}
int find(int val){
int now=root;val--;
pushdown(now);
while(val!=tree[tree[now].e[]].size){
if(tree[tree[now].e[]].size<val){
val-=tree[tree[now].e[]].size+;
now=tree[now].e[];
}
else now=tree[now].e[];
pushdown(now);
}
return now;
}
void print(int now){
if(!now) return;
pushdown(now);
print(tree[now].e[]);
if(now!=&&now!=n+) printf("%d ",now-);
print(tree[now].e[]);
}
}s; int main(){
n=read(); m=read(); root=s.build(,n+);
while(m--){
int l=read(),r=read();
int x=s.find(l); s.splay(x,);
int y=s.find(r+); s.splay(y,root);
s.tree[s.tree[y].e[]].tag^=;
}
s.print(root);
return ;
}

【Luogu】P3391文艺平衡树(Splay)的更多相关文章

  1. [luogu P3391] 文艺平衡树

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

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

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

  3. Luogu P3391 文艺平衡树(Splay or FHQ Treap)

    这道题要求区间反转...好东西.. 对于Splay:把l-1旋到根,把r+1旋到根的右儿子,这样r+1的左儿子就是整个区间了,然后对这个区间打个tg 注意要插-Inf和Inf到树里面,防止越界,坐标要 ...

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

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

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

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

  6. P3391 文艺平衡树(Splay)

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

  7. BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3595  Solved: 2029[Submit][Sta ...

  8. Tyvj P1729 文艺平衡树 Splay

    题目: http://tyvj.cn/p/1729 P1729 文艺平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第二道:文艺平衡树 ...

  9. BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

    速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...

  10. BZOJ3223/洛谷P3391 - 文艺平衡树

    BZOJ链接 洛谷链接 题意 模板题啦~2 代码 //文艺平衡树 #include <cstdio> #include <algorithm> using namespace ...

随机推荐

  1. https增加临时证书,tomcat配置

    1Windows下: 1.1 生成keystore文件及导出证书 打开控制台: 运行: %JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RS ...

  2. SnowKiting

    原文 Let's go fly a kite...in the snow Reach into your closet,find that dusty kite and clean it off - ...

  3. HDU 3377 Plan (插头DP,变形)

    题意:有一个n*m的矩阵,每个格子中有一个值(可能负值),要从左上角走到右下角,求路径的最大花费. 思路: 除了起点和终点外,其他的点可以走,也可以不走. (2)我用的是括号表示法,所以起始状态为') ...

  4. decompressedResponseImageOfSize:completionHandler:]_block_invoke

    原因:   It turns out the linker error was caused by the CGImageSourceCreateWithData call. And the root ...

  5. BC div2补题以及 复习模除 逆元__BestCoder Round #78 (div.2)

    第一题没话说 智商欠费 加老柴辅导终于过了 需要在意的是数据范围为2的63次方-1 三个数相加肯定爆了 四边形的定义 任意边小于其余三边之和 换句话说就是 最长边小于其余三边之和 这样的话问题转化为 ...

  6. POJ1077 八数码 BFS

    BFS 几天的超时... A*算法不会,哪天再看去了. /* 倒搜超时, 改成顺序搜超时 然后把记录路径改成只记录当前点的操作,把上次的位置记录下AC..不完整的人生啊 */ #include < ...

  7. 2018 北京区域赛 I - Palindromes (找规律)

    题目 HihoCoder - 1878 题目大意 给出k,让求出第k个回文数(k的“长度”不超过1e5) 题解 之前做过类似的题,是统计各阶段的数找到第K个回文数,但这里K太大,需要寻找新的方法. 打 ...

  8. iview table里面 插入下拉列表组件(自定义组件)一定要加key,不加key,table开始会加载所有数据,然后再从第2页点回第一页,就会走onChange事件,混乱的逻辑,切记加:key

    iview table里面 插入下拉列表组件(自定义组件)一定要加key,不加key,table开始会加载所有数据,然后再从第2页点回第一页,就会走onChange事件,混乱的逻辑,切记加:key 关 ...

  9. 如何在Mac OS X中开启或关闭显示隐藏文件命令

    打开终端,输入:defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.app ...

  10. How to debug add-ins for arcgis

    Debugging add-ins To debug an add-in, follow these steps: Confirm that the add-in is deployed to the ...