【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 ...
随机推荐
- Spring下读取properties文件
由于在spring的xml文件中配置了 <bean id="validator" class="org.springframework.validation.bea ...
- 【MYSQL】mysql-5.6.19-win32免安装版本配置方法
[MYSQL]mysql-5.6.19-win32免安装版本配置方法 1.文件下载网站(http://dev.mysql.com/downloads/): 具体下载地址:http://211.136. ...
- vijos 1190 繁忙的都市
描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路 ...
- Servlet和JSP之有关Servlet和JSP的梳理(二)
JSP JSP页面本质上是一个Servlet,JSP页面在JSP容器中运行,一个Servlet容器通常也是JSP容器. 当一个JSP页面第一次被请求时,Servlet/JSP容器主要做一下两件事情: ...
- 欧拉函数求在1-n-1与n互质的个数
long long phi(long long x) { long long res=x,a=x,i; ;i*i<=a;i++) { ) { res=res/i*(i-); ) a=a/i; } ...
- WPF中引入外部资源
有时候需要在WPF中引入外部资源,比如图片.音频.视频等,所以这个常见的技能还是需要GET到. 第一步:在VS中创建一个WPF窗口程序 第二步:从外部引入资源,这里以引入图片资源为例 1)新建Reso ...
- AppCrawler自动化遍历使用详解(版本2.1.0 )(转)
AppCrawle是自动遍历的app爬虫工具,最大的特点是灵活性,实现:对整个APP的所有可点击元素进行遍历点击. 优点: 1.支持android和iOS, 支持真机和模拟器 2.可通过配置来设定 ...
- Codeforces Round #272 (Div. 2)-C. Dreamoon and Sums
http://codeforces.com/contest/476/problem/C C. Dreamoon and Sums time limit per test 1.5 seconds mem ...
- 寄存器变量 extern 外部变量 外部函数
寄存器变量 这个可以不理睬 register 关键字定义的变量直接放在寄存器当中 寄存器是放在CPU内部的存储单元,它的速度比内存快的多,所以当程序中有10000多次调用同一个变量的时候声明成寄存器变 ...
- iOS7.1企业版发布后用户通过sarafi浏览器安装无效的解决方案
关于iOS7.1企业版发布后,用户通过sarafi浏览器安装无效的解决方案: 通过测试,已经完美解决. 方案一: iOS7.1企业应用无法安装应用程序 因为证书无效的解决方案 http://blog. ...