【Luogu】P3391文艺平衡树(Splay)
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)的更多相关文章
- [luogu P3391] 文艺平衡树
[luogu P3391] 文艺平衡树 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区 ...
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- Luogu P3391 文艺平衡树(Splay or FHQ Treap)
这道题要求区间反转...好东西.. 对于Splay:把l-1旋到根,把r+1旋到根的右儿子,这样r+1的左儿子就是整个区间了,然后对这个区间打个tg 注意要插-Inf和Inf到树里面,防止越界,坐标要 ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- P3391 文艺平衡树(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标记表示是否翻转 ------------------------------------------------- ...
- BZOJ3223/洛谷P3391 - 文艺平衡树
BZOJ链接 洛谷链接 题意 模板题啦~2 代码 //文艺平衡树 #include <cstdio> #include <algorithm> using namespace ...
随机推荐
- Lucene全文检索技术学习
---------------------------------------------------------------------------------------------------- ...
- Exoplanet: The hunt is on
原文 How many planets are out there? Today scientists believe that planets could outnumber the stars.F ...
- 洛谷 P2617 Dynamic Ranking
题目描述 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是多少(1≤k≤ ...
- mvc的model验证,ajaxhelper,验证机制语法
ajaxhelper: onsuccess是调用成功后显示方法,还有一个方法是调用前显示 model验证: 控件前端验证: 需要引入的JS 其中第二个是ajaxhelper的必须验证 后台的两个同名不 ...
- vue for循环中常见问题 之 求和(合计)
例:求后台返回数据this.dataInfo 中某个字段(item.totalSum)的和,只需添加computed,然后模板中直接可以使用totalSumAll (不需要再data中声明) comp ...
- apache 报413
http://www.hostlift.com/apache/modsecurity-request-body-content-length-is-larger-than-the-configured ...
- iOS 设计模式
很赞的总结 iOS Design Patterns 中文版 IOS设计模式之一(MVC模式,单例模式) IOS设计模式之二(门面模式,装饰器模式) IOS设计模式之三(适配器模式,观察者模式) IOS ...
- sha1、base64、ase加密
<!DOCTYPE html><html><head><title>sha1.base64.ase加密</title><meta ch ...
- 【单调栈 动态规划】bzoj1057: [ZJOI2007]棋盘制作
好像还有个名字叫做“极大化”? Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源 于易经的思想,棋盘是一个8*8大小的黑白相间的 ...
- WINDOWS下使用Mysql 中碰到的问题记录
问题:在cmd中输入net stop mysql反馈“服务名无效” win+R打开运行窗口,输入 services.msc 查看其中mysql的服务名,比如我的是叫做MySQL80 让我们继续回到最开 ...