bzoj3223 文艺平衡树
2016-07-26 21:29
316 查看
传送门 :http://www.lydsy.com/JudgeOnline/problem.php?id=3223
splay区间翻转的基础题,然而我还是调了一晚上(蒟蒻的悲哀)
splay区间翻转的基础题,然而我还是调了一晚上(蒟蒻的悲哀)
#include <cstdio> #include <cmath> #include <iostream> #include <cstring> #include <algorithm> using namespace std; int n,m,root,x,y; int ls[100005],rs[100005],s[100005],v[100005],lazy[100005],fa[100005],f[100005]; void pushup(int rt) { s[rt]=s[ls[rt]]+s[rs[rt]]+1; } void pushdown(int rt) { if (!lazy[rt]) return; swap(ls[rt],rs[rt]); lazy[ls[rt]]^=1; lazy[rs[rt]]^=1; lazy[rt]=0; } void build(int l,int r,int fa) { if (l>r) return; int now=v[l]; int last=v[fa]; if (l==r) { s[now]=1; f[now]=last; if (l<fa) ls[last]=now; else rs[last]=now; return; } int mid=(l+r)>>1; now=v[mid]; build(l,mid-1,mid); build(mid+1,r,mid); f[now]=last; if (mid<fa) ls[last]=now; else rs[last]=now; pushup(mid); } void zig(int x) { int y=f[x],z=f[y]; if (ls[z]==y) ls[z]=x; else rs[z]=x; f[x]=z; f[y]=x; ls[y]=rs[x]; f[rs[x]]=y; rs[x]=y; pushup(y); pushup(x); if (y==root) root=x; } void zag(int x) { int y=f[x],z=f[y]; if (ls[z]==y) ls[z]=x; else rs[z]=x; f[x]=z; f[y]=x; rs[y]=ls[x]; f[ls[x]]=y; ls[x]=y; pushup(y); pushup(x); if (y==root) root=x; } void splay(int x,int k) { while(f[x]!=k) { if (ls[f[x]]==x) zig(x); else zag(x); } } int find(int &k,int x) { pushdown(k); if(s[ls[k]]+1==x)return k; if(s[ls[k]]>=x)return find(ls[k],x); return find(rs[k],x-s[ls[k]]-1); } void lazye(int l,int r) { int x=find(root,l); int y=find(root,r); splay(x,0); splay(y,x); lazy[ls[y]]^=1; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n+2;i++) { v[i]=i; } root=(n+3)>>1; build(1,n+2,0); for (int i=1;i<=m;i++) { scanf("%d%d",&x,&y); lazye(x,y+2); } for (int i=2;i<=n+1;i++) { printf("%d ",find(root,i)-1); } }
相关文章推荐
- python简单爬虫爬取队列的实现
- 【学习】ie-css3.htc---让ie8以下支持css3
- sublime3最新版破解码 linux下也适用
- Java NIO学习
- Zabbix-3.0.3结合Grafana-3.1.0给你想要的绘图
- MyBatis的关于批量数据操作的测试
- 设计模式-builder模式
- 《邪气凛然》读后感
- Test3
- 模拟时间
- Android Studio 提交现有项目到osc
- C语言实验——整数位(用顺序表)
- SlidingMenu+Fragment组合
- 科学与艺术
- [leetcode]215. Kth Largest Element in an Array
- hdu1251-字符前缀查找问题 map容器
- Cable master<hdoj1551>
- 5-29 修理牧场
- java.nio系列二
- EJB系列(一)---简介