博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ3223: Tyvj 1729 文艺平衡树 无旋Treap
阅读量:4673 次
发布时间:2019-06-09

本文共 2483 字,大约阅读时间需要 8 分钟。

一开始光知道pushdown却忘了pushup.........

#include
#include
#include
#include
#include
#define MAXN 100010using namespace std;inline int read(){ int sum=0; char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch>='0'&&ch<='9') { sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar(); } return sum;}struct Treap{ struct Node { Node *ch[2]; int size,v,key; bool rev; void pushup() { size=ch[0]->size+ch[1]->size+1; } }null[MAXN],*root,*q[MAXN]; void swap(Node *&x,Node *&y) { Node *temp=x; x=y; y=temp; } void pushdown(Node *p) { if(!p->rev)return; p->ch[0]->rev^=1; p->ch[1]->rev^=1; swap(p->ch[0],p->ch[1]); p->rev=0; } int head,tail; int sz; Node *build(int l,int r) { if(l>r)return null; int mid=(l+r)>>1; null[mid].ch[0]=build(l,mid-1); null[mid].ch[1]=build(mid+1,r); null[mid].pushup(); return &null[mid]; } void bfs() { q[1]=root; head=tail=1; while(head<=tail) { Node *x=q[head++]; x->v=++sz; if(x->ch[1]!=null)q[++tail]=x->ch[1]; if(x->ch[0]!=null)q[++tail]=x->ch[0]; } } void Init(int n) { null->ch[0]=null->ch[1]=null; for(int i=1;i<=n;i++) null[i].ch[0]=null[i].ch[1]=null,null[i].size=1,null[i].key=i; root=build(1,n); bfs(); } Node *Merge(Node *a,Node *b) { if(a==null)return b; if(b==null)return a; if(a->v
v) { pushdown(a); a->ch[1]=Merge(a->ch[1],b); a->pushup(); return a; } else { pushdown(b); b->ch[0]=Merge(a,b->ch[0]); b->pushup(); return b; } } pair
split(Node *p,int k) { if(p==null)return make_pair(null,null); pushdown(p); if(p->ch[0]->size>=k) { pair
y=split(p->ch[0],k); p->ch[0]=y.second; p->pushup(); y.second=p; return y; } pair
y=split(p->ch[1],k-p->ch[0]->size-1); p->ch[1]=y.first; p->pushup(); y.first=p; return y; } void Rev(int l,int r) { pair
x=split(root,l-1); pair
y=split(x.second,r-l+1); y.first->rev^=1; root=Merge(Merge(x.first,y.first),y.second); } void Print(Node *p) { pushdown(p); if(p==null)return; Print(p->ch[0]); printf("%d ",p->key); Print(p->ch[1]); }}YY;int main(){ int n=read(),m=read(); YY.Init(n); while(m--) { int l=read(),r=read(); YY.Rev(l,r); } YY.Print(YY.root); return 0;}

 

转载于:https://www.cnblogs.com/TSHugh/p/7155709.html

你可能感兴趣的文章
Access 中数据库操作时提示from子句语法错误
查看>>
【备战NOIP】[算法总结] 二分查找
查看>>
sort函数用于vector向量的排序
查看>>
《算法》-- 总结
查看>>
El表达式
查看>>
leetcode-题8-3sum
查看>>
SQL-Server使用点滴(二-系统表)
查看>>
Djiango django跨域 cookie session
查看>>
vue通过webpack打包后怎么运行
查看>>
MYSQL中的日期转换
查看>>
在线修改Schema
查看>>
【学术篇】SDOI2008 仪仗队
查看>>
5.递归实现,把M元用最少的硬币来凑。不同面值的硬币,有10元,5元,2元,1元。...
查看>>
第6章—渲染web视图—使用Thymeleaf
查看>>
Android动态添加Fragment
查看>>
OGRE粒子系统简介
查看>>
Windows 10 使用问题
查看>>
linux xargs命令
查看>>
用CSS3实现图像风格
查看>>
转载--黎曼
查看>>