[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 ...
随机推荐
- FireMonkey Premium Style Pack 2 for RAD Studio XE4
FireMonkey Premium Style Pack 2 for RAD Studio XE4 http://cc.embarcadero.com/item/29483 http://www.e ...
- Shell 参数(1)
shell 中参数相关: ./a.sh a b c d $# 是传给脚本的参数个数 $0 是脚本本身的名字 $1 是传递给该shell脚本的第一个参数 $2 是传递给该shell脚本的第二个参数 $@ ...
- Linux下Apache配置局域网访问出现的问题
在网站安装好之后,本机可以访问,但是局域网内无法访问,我查看了 /etc/httpd/conf/httpd.conf 看到我的配置如下 <Directory ......> Allow A ...
- Python基本特殊方法之__new__
__new__()和不可变对象 __new__方法的一个用途是初始化不可变对象,__new()__方法中允许创建未初始化的对象,这允许我们在__init__()方法被调用之前先设置对象的属性 例:为f ...
- 【JVM】JVM参数说明和分析
不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正确的选择不同的GC策略, 调整JVM.GC的参数,可以极大的减少由于GC工作,而导致的程序运行中断方面的问题,进而适当的提高Jav ...
- 好用的python第三方库
参考连接:http://python.jobbole.com/84464/ https://www.zhihu.com/question/20501628 python每日技术更新:https://g ...
- 十、python沉淀之路--高阶函数初识
一.高阶函数:分两种:一种是返回值中包含函数体:另一种是把一个函数体当作了参数传给了另一个函数 1.返回值中包含函数体 例1. def test(): print('这是一个测试') return t ...
- phpstorm2017.3.6的激活、样式设置和汉化
一:安装phpstorm2017.3.6,并激活.设置样式.(1)先在phstorm官网里www.jetbrains.com下载phpstorm2017.3.6,按照步骤安装即可.下面开始激活!(2) ...
- openfaas 架构介绍
此为官方介绍 Overview of OpenFaaS Function Watchdog You can make any Docker image into a serverless fun ...
- js+css 实现遮罩居中弹出层(随浏览器窗口滚动条滚动)
本文为大家详细介绍下使用js实现遮罩弹出层居中,且随浏览器窗口滚动条滚动,示例代码如下,感兴趣的朋友可以参考下, js+css 实现遮罩居中弹出层(随浏览器窗口滚动条滚动) 下面看看我的原始代码: & ...