[bzoj3223]文艺平衡树(splay区间反转模板)
解题关键:splay模板题。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
const int N = ; int ch[N][],par[N],val[N],cnt[N],size[N],rev[N],root,ncnt;
int n,m,x,y; bool chk(int x){
return ch[par[x]][]==x;
} void pushup(int x){
size[x]=size[ch[x][]]+size[ch[x][]]+cnt[x];
} void pushdown(int x){
if(rev[x]){
swap(ch[x][],ch[x][]);//反转就是交换左右子树即可成立。
rev[ch[x][]]^=;
rev[ch[x][]]^=;
rev[x]=;
}
} void rotate(int x){
int y=par[x],z=par[y],k=chk(x),w=ch[x][k^];
ch[y][k]=w;par[w]=y;
ch[z][chk(y)]=x;par[x]=z;
ch[x][k^]=y;par[y]=x;
pushup(y);pushup(x);
} void splay(int x,int goal=){
while(par[x]!=goal){
int y=par[x],z=par[y];
if(z!=goal){
if(chk(x)==chk(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
if(!goal) root=x;
} void insert(int x){
int cur=root,p=;
while(cur&&val[cur]!=x){
p=cur;
cur=ch[cur][x>val[cur]];
}
if(cur){
cnt[cur]++;
}else{
cur=++ncnt;
if(p) ch[p][x>val[p]]=cur;
ch[cur][]=ch[cur][]=;
par[cur]=p;val[cur]=x;
cnt[cur]=size[cur]=;
}
splay(cur);
} void find(int x){
int cur=root;
if(!cur) return;
while(ch[cur][x>val[cur]]&&x!=val[cur]){
cur=ch[cur][x>val[cur]];
}
splay(cur);
}
//从1开始计数
int kth(int k){
k++;
int cur=root;
while(){
pushdown(cur);
if(ch[cur][]&&k<=size[ch[cur][]]){
cur=ch[cur][];
}else if(k>size[ch[cur][]]+cnt[cur]){
k-=size[ch[cur][]]+cnt[cur];
cur=ch[cur][];
}else{
return cur;
}
}
} int rnk(int x){
find(x);
if(val[root]>=x) return size[ch[root][]];
else return size[ch[root][]]+cnt[root];
} void reverse(int l,int r){
int x=kth(l-),y=kth(r+);
splay(x);splay(y,x);
rev[ch[y][]]^=;
} int pre(int x){
find(x);
if(val[root]<x) return root;
int cur=ch[root][];
while(ch[cur][]) cur=ch[cur][];
return cur;
} int succ(int x) {
find(x);
if(val[root]>x) return root;
int cur=ch[root][];
while(ch[cur][]) cur=ch[cur][];
return cur;
} void output(int x){
pushdown(x);
if(ch[x][]) output(ch[x][]);
if(val[x]<=n&&val[x]>=) printf("%d ",val[x]);
if(ch[x][]) output(ch[x][]);
} void init(){
insert(-2e9);
insert(2e9);
} int main(){
init();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) insert(i);
while(m--){
scanf("%d%d",&x,&y);
reverse(x,y);
}
output(root);
return ;
}
[bzoj3223]文艺平衡树(splay区间反转模板)的更多相关文章
- BZOJ3223 文艺平衡树(splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- JZYZOJ1998 [bzoj3223] 文艺平衡树 splay 平衡树
http://172.20.6.3/Problem_Show.asp?id=1998 平衡树区间翻转的板子,重新写一遍,给自己码一个板子. #include<iostream> #incl ...
- [bzoj3223]文艺平衡树——splay
题意 你应当编写一个数据结构,支持以下操作: 反转一个区间 题解 我们把在数组中的位置当作权值,这样原序列就在这种权值意义下有序,我们考虑使用splay维护. 对于操作rev[l,r],我们首先把l- ...
- bzoj 3223 文艺平衡树 splay 区间翻转
Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17715 Solved: 7769[Submit][Status][ ...
- 算法模板——splay区间反转 2
实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
随机推荐
- Photon Cloud Networking: OnPhotonSerializeView Not Firing
Photon Cloud Networking: OnPhotonSerializeView Not Firing http://answers.unity3d.com/questions/31305 ...
- <tf-idf + 余弦相似度> 计算文章的相似度
背景知识: (1)tf-idf 按照词TF-IDF值来衡量该词在该文档中的重要性的指导思想:如果某个词比较少见,但是它在这篇文章中多次出现,那么它很可能就反映了这篇文章的特性,正是我们所需要的关键词. ...
- Go语言开发中MongoDB数据库
伴随着移动端的兴起,Nosql数据库以其分布式设计和高性能等特点得到了广泛的应该用,下面将介绍下Nosql中的mongoDB在Go语言中的应用,在开发前,有必要了解下基础知识 在开发前,导入开发需要用 ...
- 安装g++,在centos上执行yum -y install gcc gcc-c++ libstdc++-devel
Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: mirrors.1 ...
- JDBC 2 封装
1 封装 新建类 DBConnUtil ,新建database.properties 文件储存链接信息如下所示 jdbcDriver=com.mysql.jdbc.Driver jdbcUrl=jd ...
- Linq:Grouping Operators
[Category("Grouping Operators")] [Description("This sample uses group by to partition ...
- shell中把大写字母转换成小写字母
shell中把大写字母转换成小写字母 参考:http://www.jb51.net/article/40257.htm echo "AABBCC" | tr "[:upp ...
- HDOJ5876(补图的最短路)
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HTML简历
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 杂项:UN-APP
ylbtech-杂项:APP 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回顶部 10 ...