【BZOJ 3223】文艺平衡树 模板题
就是打个翻转标记,下推标记时记得交换左右孩子指针,查询kth和中序遍历输出时也记得要下推标记同时交换指针,二者不可缺!←这是易错点
仿陈竞潇学长模板的代码:
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
node();
node *ch[2],*fa;
short reversal;
short pl(){return this==fa->ch[1];}
int d,sum;
void push(); void count();
}*null;
int N,M;
node::node(){ch[0]=ch[1]=fa=null;reversal=sum=d=0;}
void node::push(){
if (this==null) return;
if (reversal==1){
reversal=0;
ch[0]->reversal^=1;
ch[1]->reversal^=1;
node *k=ch[0];
ch[0]=ch[1];
ch[1]=k;
}
}
void node::count(){
sum=ch[0]->sum+ch[1]->sum+1;
}
namespace Splay{
node *ROOT;
node *build(int l=1,int r=N){
if (l>r) return null;
int mid=(l+r)>>1;
node *ro=new node;
ro->d=mid;
ro->ch[0]=build(l,mid-1);
ro->ch[1]=build(mid+1,r);
ro->ch[0]->fa=ro;
ro->ch[1]->fa=ro;
ro->count();
return ro;
}
void Build(){
null=new node;
*null=node();
ROOT=build();
ROOT->count();
}
void rotate(node *k){
node *r=k->fa; if (r==null||k==null) return;
r->push(); k->push();
int x=k->pl()^1;
r->ch[x^1]=k->ch[x];
r->ch[x^1]->fa=r;
if (r->fa==null) ROOT=k;
else r->fa->ch[r->pl()]=k;
k->fa=r->fa;
r->fa=k;
k->ch[x]=r;
r->count(); k->count();
}
void splay(node *r,node *tar=null){
for (;r->fa!=tar;rotate(r))
if (r->fa->fa!=tar)rotate(r->pl()==r->fa->pl()?r->fa:r);
r->push();
}
node *kth(int x){
node *r=ROOT;
while (r!=null){
r->push();
if (r->ch[0]->sum>=x) r=r->ch[0];
else if (r->ch[0]->sum+1>=x) return r;
else x-=r->ch[0]->sum+1,r=r->ch[1];
}return null;
}
void rollingover(int ll,int rr){
node *ln=kth(ll-1),*rn=kth(rr+1),*r;
if ((ln==null)&&(rn==null)) r=ROOT;
else if (ln==null){
splay(rn); r=ROOT->ch[0];
}else if (rn==null){
splay(ln); r=ROOT->ch[1];
}else{
splay(ln); splay(rn,ROOT);
r=ROOT->ch[1]->ch[0];
}r->reversal=r->reversal^1;
}
void AC(node *r=ROOT){
if (r==null) return;
r->push();
AC(r->ch[0]);
printf("%d ",r->d);
AC(r->ch[1]);
}
}
int getint()
{
char c;
while (!isdigit(c=getchar()));
int a=c-'0';
while (isdigit(c=getchar()))
a=a*10+c-'0';
return a;
}
int main()
{
N=getint();M=getint();
Splay::Build();
while (M--){
int l=getint(),r=getint();
Splay::rollingover(l,r);
}
Splay::AC();
return 0;
}
自己写的62行简洁代码:
#include<cstdio>
#include<algorithm>
#define read(x) x=getint()
using namespace std;
inline int getint(){char c;int ret=0;for(c=getchar();c<'0'||c>'9';c=getchar());for(;c>='0'&&c<='9';c=getchar())ret=ret*10+c-'0';return ret;}
struct node{
node();
node *fa,*ch[2];
int d,sum;
bool rev;
bool pl() {return this->fa->ch[1]==this;}
void setc(node *r,bool c) {r->fa=this; this->ch[c]=r;}
void push() {if (rev){swap(ch[0],ch[1]);ch[0]->rev^=1;ch[1]->rev^=1;rev=0;}}
void count() {sum=ch[0]->sum+ch[1]->sum+1;}
}*ROOT,*null;
node::node(){fa=ch[0]=ch[1]=null;d=sum=rev=0;}
int n,m;
inline node *build(int l,int r){
if (l>r) return null; int mid=(l+r)>>1; node *k=new node;
k->ch[0]=build(l,mid-1); k->ch[1]=build(mid+1,r);
if (k->ch[0]!=null) k->ch[0]->fa=k; if (k->ch[1]!=null) k->ch[1]->fa=k;
k->d=mid; k->count(); return k;
}
inline void Build() {null=new node;*null=node();ROOT=build(1,n);}
inline void rotate(node *r){
node *f=r->fa; bool c=r->pl();
if (f!=ROOT) f->fa->setc(r,f->pl());
else r->fa=null,ROOT=r;
f->setc(r->ch[!c],c); r->setc(f,!c);
f->count();
}
inline void update(node *r) {if (r!=null) update(r->fa); r->push();}
inline void splay(node *r,node *tar=null){
update(r);
for(;r->fa!=tar;rotate(r)) if (r->fa->fa!=tar) rotate(r->fa->pl()==r->pl()?r->fa:r);
r->count();
}
inline node *kth(int x){
if ((x==0)||(x==n+1)) return null;
node *r=ROOT;
while (1){
r->push();
if (r->ch[0]->sum>=x) r=r->ch[0];
else if (r->ch[0]->sum+1>=x) return r;
else {x-=r->ch[0]->sum+1; r=r->ch[1];}
}
}
inline void reversal(int l,int r){
node *ll=kth(l-1),*rr=kth(r+1);
if ((ll==null)&&(rr==null)) ROOT->rev^=1;
else if (ll==null) splay(rr),ROOT->ch[0]->rev^=1;
else if (rr==null) splay(ll),ROOT->ch[1]->rev^=1;
else splay(ll),splay(rr,ROOT),rr->ch[0]->rev^=1;
}
inline void AC(node *r) {if (r==null) return; r->push(); AC(r->ch[0]); printf("%d ",r->d); AC(r->ch[1]);}
int main(){
read(n); read(m); Build();
int l,r;
while (m--) {read(l); read(r); reversal(l,r);}
AC(ROOT);
return 0;
}
然后就可以了
【BZOJ 3223】文艺平衡树 模板题的更多相关文章
- [题解]bzoj 3223 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- bzoj 3223 文艺平衡树 - Splay
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- BZOJ 3223 文艺平衡树 [codevs3303翻转区间]
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 通道2:http://codevs.cn/problem/3303/ 题目分析: 我 ...
- BZOJ 3223 文艺平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 ...
- bzoj 3223 文艺平衡树 splay 区间翻转
Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17715 Solved: 7769[Submit][Status][ ...
- bzoj 3223 文艺平衡树 Splay 打标志
是NOI2003Editor的一个子任务 #include <cstdio> #include <vector> #define maxn 100010 using names ...
- HDU 4006 The kth great number 优先队列、平衡树模板题(SBT)
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- [bzoj3224]普通平衡树/3223文艺平衡树
这是一道很普通的题.. 最近花了很多时间来想要去干什么,感觉自己还是太拿衣服 做这道题是因为偶尔看到了lavender的blog和她的bzoj早期AC记录,就被题目深深地吸引到了,原因有二: 自己sp ...
- BZOJ 2724 蒲公英 | 分块模板题
题意 给出一个序列,在线询问区间众数.如果众数有多个,输出最小的那个. 题解 这是一道分块模板题. 一个询问的区间的众数,可能是中间"整块"区间的众数,也可能是左右两侧零散的数中的 ...
随机推荐
- PL/0 词法分析器
PL/0 词法分析器 #include<stdio.h> #include <ctype.h> #include <stdlib.h> #include <s ...
- HDU 3584 Cube --三维树状数组
题意:给一个三维数组n*n*n,初始都为0,每次有两个操作: 1. 翻转(x1,y1,z1) -> (x2,y2,z2) 0. 查询A[x][y][z] (A为该数组) 解法:树状数组维护操作次 ...
- 转: 使用virtualenv搭建独立的Python环境
转自: http://qicheng0211.blog.51cto.com/3958621/1561685 谢谢作者的辛苦付出 virtualenv可以搭建虚拟且独立的python环境,可以使每个项 ...
- 通过Nethogs查看服务器网卡流量情况
在日常运维工作中,会碰到服务器带宽飙升致使网站异常情况.作为运维人员,我们要能非常清楚地了解到服务器网卡的流量情况,观察到网卡的流量是由哪些程序在占用着. 今天介绍一款linux下查看服务器网卡流量占 ...
- 理解SQL Server中的权限体系(下)----安全对象和权限
原文:http://www.cnblogs.com/CareySon/archive/2012/04/12/SQL-Security-SecurableAndPermission.html 在开始阅读 ...
- VSFTPD配置TLS/SSL
今天在OSX上配置Coda2 + Xampp的时候,发现FTP老是不连接到服务器上面,导致每次更改了文件都需要使用scp命令上传到服务器.如果一个文件还好,文件和文件夹一多就得使用rp参数全部提交,再 ...
- [tools]camtasia studio8.6
实际情况: 装了汉化补丁包后坏事,最后还是没装汉化补丁包.直接用英文版的.安装过程中输入序列号即可激活. ):输入注册码安装 用户名:大眼仔~旭(Anan) 注册码:GCABC-CPCCE-BPMMB ...
- 使用Uploadify实现上传图片生成缩略图例子,实时显示进度条
不了解Uploadify的,先看看前一篇详细说明 http://www.cnblogs.com/XuebinDing/archive/2012/04/26/2470995.html Uploadify ...
- 2015年新版C#从入门到精通(第2版)视频教学录像【无水印版】
<c#从入门到精通(第2版)>以零基础讲解为宗旨,用实例引导读者学习,深入浅出地介绍了c#的相关知识和实战技能.<c#从入门到精通(第2版)>第1篇[c#语言基础]主要讲解c# ...
- 集中式版本控制系统:从svn到tfs
06年进公司的时候,我们没有使用版本控制系统,所有代码都在部门经理的电脑上放着,谁要改什么代码就过去要一份最新的,改好之后再放回去,感觉好原始.据说之前是用过一段时间微软的vss(visual.sou ...