[BZOJ]3223文艺平衡树做题笔记
2016-02-15 16:24
162 查看
题目来源:http://www.lydsy.com/JudgeOnline/problem.php?id=3223
这题是splay区间翻转。
个人感觉splay区间翻转最有意思的地方就是可以在翻转时破坏二叉排序树的性质,因为splay的旋转操作和查找第K大时不涉及data值的大小比较,所以可以放心的翻转,而不会破坏序列值的rank。这里的rank等价于某个值在序列中的位置编号。
这题是splay区间翻转。
个人感觉splay区间翻转最有意思的地方就是可以在翻转时破坏二叉排序树的性质,因为splay的旋转操作和查找第K大时不涉及data值的大小比较,所以可以放心的翻转,而不会破坏序列值的rank。这里的rank等价于某个值在序列中的位置编号。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 100010 #define lch ch[x][0] #define rch ch[x][1] int n,m; struct Splay { int ch [2],f ,s ,a ,cnt,root,val; bool b ; void filp (int x) { swap(ch[x][0],ch[x][1]); b[x]^=1; } void push (int x) { if (b[x]) { filp(ch[x][0]);filp(ch[x][1]); b[x]=0; } } void update (int x) { s[x]=s[ch[x][0]]+s[ch[x][1]]+1; } void rotate (int x) { int y=f[x],opt; if (ch[f[x]][0]==x) opt=0; else opt=1; ch[y][opt]=ch[x][!opt]; if (ch[x][!opt]) f[ch[x][!opt]]=y; f[x]=f[y]; if (root==y) root=x; else if (ch[f[y]][0]==y) ch[f[y]][0]=x; else ch[f[y]][1]=x; f[y]=x,ch[x][!opt]=y; update(y),update(x); } void splay (int x,int to=0) { while (f[x]!=to) { if (f[f[x]]==to) rotate(x); else if ((ch[f[f[x]]][0]==f[x]) ==(ch[f[x]][0]==x)) rotate(f[x]),rotate(x); else rotate(x),rotate(x); } } int find (int k) { int x=root;push(x); while (s[ch[x][0]]+1!=k) { if (k<s[ch[x][0]]+1) x=ch[x][0]; else k-=s[ch[x][0]]+1,x=ch[x][1]; push(x); } return x; } int build (int L,int R,int fa=0) { if (L>R) return 0; int x=++cnt,mid=(L+R)>>1; f[x]=fa; ch[x][0]=build(L,mid-1,x); a[x]=++val; ch[x][1]=build(mid+1,R,x); update(x); return x; } void init (int n) { ch[1][1]=2,f[2]=1,s[1]=2,s[2]=1,cnt=2,root=1; ch[2][0]=build(1,n,2); } void op (int L,int R) { splay(find(L)),splay(find(R+2),root); filp(ch[ch[root][1]][0]); } void print (int x) { if (!x) return; push(x); print(ch[x][0]); if (x>2) printf("%d ",a[x]); print(ch[x][1]); } }Sp; int main () { int l,r; scanf("%d%d",&n,&m); Sp.init(n); for (int i=0;i<m;i++) { scanf("%d%d",&l,&r); Sp.op(l,r); } Sp.print(Sp.root); }
相关文章推荐
- Android应用程序权限机制的源代码分析(2)
- Android中保存网络图片到SD卡
- myaudio.duration为null的解决办法
- 删除嵌套表类型列的元素值
- POJ3686 KM最大权匹配+高难度建图
- js基础篇——call/apply、arguments、undefined/null
- 2016.2.15 16年第一天上班 ZooKeeper集群的安装、配置
- Android应用程序权限机制的源代码分析(1)
- 重写前奏~继承和多态
- iOS 利用CoreLocation和MapKit开发搜索附近的商场功能
- js基础篇——call/apply、arguments、undefined/null
- mongodb java driver3.0初探
- Java 高并发缓存与Guava Cache
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- _exit 和 exit的区别
- 开机需要重新设置默认网关
- DB2操作流程
- 获取日期Date年月日
- 深入浅出Python装饰器
- Android的四大组件——Activity