【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 蒲公英 | 分块模板题
题意 给出一个序列,在线询问区间众数.如果众数有多个,输出最小的那个. 题解 这是一道分块模板题. 一个询问的区间的众数,可能是中间"整块"区间的众数,也可能是左右两侧零散的数中的 ...
随机推荐
- 【Android UI设计与开发】4.底部菜单栏(一)Fragment介绍和简单实现
TabActivity在Android4.0以后已经被完全弃用,取而代之的是Fragment.Fragment是Android3.0新增的概念,Fragment翻译成中文是碎片的意思,不过却和Acti ...
- 『转载』使用TortoiseSVN客户端
原文地址:https://www.sinacloud.com/doc/sae/tutorial/code-deploy.html#shi-yong-git-ke-hu-duan TortoiseSVN ...
- JavaWeb学习之Servlet(四)----ServletConfig获取配置信息、ServletContext的应用
[声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140877.html [正文] 一.ServletConfig:代表当前 ...
- 第20章 DLL高级技术(2)
20.3 延迟载入DLL 20.3.1延迟载入的目的 (1)如果应用程序使用了多个DLL,那么它的初始化可能比慢,因为加载程序要将所有必需的DLL映射到进程的地址空间.→利用延迟加载可将载入过程延伸到 ...
- R之字符串连接函数paste
函数paste的一般使用格式为: paste(..., sep = " ", collapse = NULL) 其中...表示一个或多个R可以被转化为字符型的对象:参数sep表示分 ...
- Window.open()方法参数详解
Window.open()方法参数详解 1, 最基本的弹出窗口代码 window.open('page.html'); 2, 经过设置后的弹出窗口 window.open('page.html ...
- result默认返回action中的所有数据,要想返回指定的数据怎么做呢
result默认返回action中的所有数据,要想返回指定的数据怎么做呢?
- WPF使用cefsharp
最近在公司项目上会用到cefsharp.wpf,不知道为什么按照网上的配置一直无法运行成功,怎么配置可以参考以下这篇博文: http://www.cnblogs.com/TianFang/p/4573 ...
- shell案例
调用同目录下的ip.txt内容: 路径 [root@lanny ~]# pwd /root txt文件 [root@lanny ~]# cat ip.txt 10.1.1.1 10.1.1.2 10. ...
- Silverlight自定义控件开发:仪表盘
在项目中,由于使用到了活动积温运算,也就是指当日平均气温稳定上升到10℃以上时,大多数农作物才能活跃生长.把大于等于10℃持续期内的日平均气温累加起来,得到的气温总和,叫做活动积温.所以我决定采用do ...