P3721 [AH2017/HNOI2017]单旋
题目:https://www.luogu.org/problemnew/show/P3721
手玩一下即可AC此题。
结论:插入x后,x要么会成为x的前驱的右儿子,要么成为x的后继的左儿子,这取决于它的前驱和后继的深度。
证明:首先可以证明的是,x的前驱和后继一定存在祖先与后代的关系,因为如果不存在此关系,它们的LCA一定和双方更接近。
然后这个结论画画图就比较显然了。
结论:单旋删除最小值后,它连向根节点的这条路径不发生变化,手玩即可证明,改变的只有它的儿子。
那么这个题就显然可以用LCT来维护,查询深度也很简单。
#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#define N 330000
#define L 300000
#define eps 1e-7
#define inf 1e9+7
#define db double
#define ll long long
#define ldb long double
using namespace std;
inline int read()
{
char ch=0;
int x=0,flag=1;
while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*flag;
}
#define lson son[x][0]
#define rson son[x][1]
int root,v[N],f[N],st[N],flag[N],sumv[N],son[N][2];
void pushup(int x){sumv[x]=sumv[lson]+sumv[rson]+1;}
void update(int x){flag[x]^=1;swap(lson,rson);}
void pushdown(int x){if(!flag[x])return;if(lson)update(lson);if(rson)update(rson);flag[x]=0;}
bool get(int x){return son[f[x]][1]==x;}
bool isroot(int x){return (son[f[x]][0]!=x)&&(son[f[x]][1]!=x);}
void rotate(int x)
{
int y=f[x],z=f[y],tx=get(x),ty=get(y),p=son[x][!tx];
if(!isroot(y))son[z][ty]=x;son[x][!tx]=y;son[y][tx]=p;
if(p)f[p]=y;f[y]=x;f[x]=z;pushup(y);pushup(x);
}
void splay(int x)
{
int cnt=0,tmp=x;
st[++cnt]=x;
while(!isroot(x))st[++cnt]=f[x],x=f[x];
for(int i=cnt;i>=1;i--)pushdown(st[i]);
x=tmp;
while(!isroot(x))
{
int y=f[x];
if(!isroot(y))rotate(get(x)==get(y)?y:x);
rotate(x);
}
pushup(x);
}
void access(int x){for(int y=0;x;y=x,x=f[x])splay(x),son[x][1]=y,pushup(x);}
void makeroot(int x){access(x);splay(x);update(x);}
void link(int x,int y){if(!x||!y)return;makeroot(x);f[x]=y;}
void cut(int x,int y){if(!x||!y)return;makeroot(x);access(y);splay(y);f[x]=son[y][0]=0;pushup(y);}
int dep(int x){makeroot(root);access(x);splay(x);return sumv[x];}
set<int>S;
set<int>::iterator it;
int opt[N],w[N],W[N],fa[N],lc[N],rc[N];
int main()
{
int m=read(),num=0;
for(int i=1;i<=m;i++)
{
opt[i]=read();
if(opt[i]==1)w[i]=W[++num]=read();
}
sort(W+1,W+num+1);num=unique(W+1,W+num+1)-W-1;
for(int i=1;i<=m;i++)if(opt[i]==1)w[i]=lower_bound(W+1,W+num+1,w[i])-W;
for(int i=1;i<=m;i++)
{
lc[0]=rc[0]=fa[0]=fa[root]=0;
int flag=opt[i];
if(flag==1)
{
int x=w[i],p,q;
S.insert(x);it=S.find(x);
if(it==S.begin())p=-1;else p=*(--S.lower_bound(x));
if(it==(--S.end()))q=-1;else q=*(S.upper_bound(x));
if(p==-1&&q==-1){root=x;printf("1\n");continue;}
if(p==-1||dep(p)<dep(q))link(x,q),lc[q]=x,fa[x]=q;
else link(x,p),rc[p]=x,fa[x]=p;
printf("%d\n",dep(x));
}
if(flag==2||flag==4)
{
int x=*S.begin(),p=rc[x],q=fa[x];
printf("%d\n",dep(x));
if(x!=root)
{
cut(x,p);cut(x,q);link(p,q);link(x,root);
lc[q]=p;if(p)fa[p]=q;rc[x]=root;fa[x]=0;fa[root]=x;root=x;
}
}
if(flag==3||flag==5)
{
int x=*S.rbegin(),p=lc[x],q=fa[x];
printf("%d\n",dep(x));
if(x!=root)
{
cut(x,p);cut(x,q);link(p,q);link(x,root);
rc[q]=p;if(p)fa[p]=q;lc[x]=root;fa[x]=0;fa[root]=x;root=x;
}
}
if(flag==4)
{
int x=*S.begin(),p=rc[x];
if(p)cut(x,p),rc[x]=fa[p]=0;
root=p;S.erase(x);
}
if(flag==5)
{
int x=*S.rbegin(),p=lc[x];
if(p)cut(x,p),lc[x]=fa[p]=0;
root=p;S.erase(x);
}
}
return 0;
}
P3721 [AH2017/HNOI2017]单旋的更多相关文章
- 洛谷 P3721 - [AH2017/HNOI2017]单旋(LCT)
洛谷题面传送门 终于调出来这道题了,写篇题解( 首先碰到这样的题我们肯定要考虑每种操作会对树的形态产生怎样的影响: 插入操作:对于 BST 有一个性质是,当你插入一个节点时,其在 BST 上的父亲肯定 ...
- luogu P3721 [AH2017/HNOI2017]单旋
传送门 \(Spaly:\)??? 考虑在暴力模拟的基础上优化 如果要插入一个数,那么根据二叉查找树的性质,这个点一定插在他的前驱的右子树或者是后继的左子树,可以利用set维护当前树里面的数,方便查找 ...
- 洛谷P3721 [AH2017/HNOI2017]单旋(线段树 set spaly)
题意 题目链接 Sol 这题好毒瘤啊.. 首先要观察到几个性质: 将最小值旋转到根相当于把右子树变为祖先的左子树,然后将原来的根变为当前最小值 上述操作对深度的影响相当于右子树不变,其他的位置-1 然 ...
- [AH2017/HNOI2017]单旋
题目 \(\rm splay\)水平太差,于是得手玩一下才能发现规律 首先插入一个数,其肯定会成为其前驱的右儿子或者是后继的左儿子,进一步手玩发现前驱的右儿子或者是后继的左儿子一定只有一个是空的,我们 ...
- [AH2017/HNOI2017] 单旋 - Splay
Splay 暴力维护节点信息即可 #include<iostream> #include<cstdio> #include<cstring> #include< ...
- bzoj 4825: [Hnoi2017]单旋 [lct]
4825: [Hnoi2017]单旋 题意:有趣的spaly hnoi2017刚出来我就去做,当时这题作死用了ett,调了5节课没做出来然后发现好像直接用lct就行了然后弃掉了... md用lct不知 ...
- 【LG3721】[HNOI2017]单旋
[LG3721][HNOI2017]单旋 题面 洛谷 题解 20pts 直接模拟\(spaly\)的过程即可. 100pts 可以发现单旋最大.最小值到根,手玩是有显然规律的,发现只需要几次\(lin ...
- 4825: [Hnoi2017]单旋
4825: [Hnoi2017]单旋 链接 分析: 以后采取更保险的方式写代码!!!81行本来以为不特判也可以,然后就总是比答案大1,甚至出现负数,调啊调啊调啊调~~~ 只会旋转最大值和最小值,以最小 ...
- [BZOJ4825][HNOI2017]单旋(线段树+Splay)
4825: [Hnoi2017]单旋 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 667 Solved: 342[Submit][Status][ ...
随机推荐
- Super-palindrome 【可能是暴力】
Super-palindrome 时间限制: 1 Sec 内存限制: 128 MB 提交: 486 解决: 166 [提交] [状态] [命题人:admin] 题目描述 You are given ...
- Why there is two completely different version of Reverse for List and IEnumerable?
https://stackoverflow.com/questions/12390971/why-there-is-two-completely-different-version-of-revers ...
- StringTie用法详解
StringTie 参考链接: https://ccb.jhu.edu/software/stringtie/index.shtml?t=manual#input https://www.cnblog ...
- (zhuan) Building Convolutional Neural Networks with Tensorflow
Ahmet Taspinar Home About Contact Building Convolutional Neural Networks with Tensorflow Posted on a ...
- (转) How a Kalman filter works, in pictures
How a Kalman filter works, in pictures I have to tell you about the Kalman filter, because what it d ...
- ES6中对象
ES6允许把声明的变量直接赋值给对象,我们看下面的例子. let name="Zachary"; let skill= 'web'; let obj= {name,skill}; ...
- 使用phpmyadmin创建数据库
1,使用phpmyadmin也需要实现安装php环境,安装环境请参考:http://www.sitestar.cn/bbs/thread-164-1-1.html: 2,到phpmyadmin官方网站 ...
- VMware网络连接模式—桥接、NAT以及仅主机模式的详细介绍和区别
在使用VMware Workstation(以下简称:VMware)创建虚拟机的过程中,配置虚拟机的网络连接是非常重要的一环,当我们为虚拟机配置网络连接时,我们可以看到如下图所示的几种网络连接模式:桥 ...
- Systemd初始化进程/RHEL 6系统中System V init命令与RHEL 7系统中systemctl命令的对比
Linux操作系统的开机过程是这样的,即从BIOS开始,然后进入Boot Loader,再加载系统内核,然后内核进行初始化,最后启动初始化进程.初始化进程作为Linux系统的第一个进程,它需要完成Li ...
- 力扣(LeetCode) 1010. 总持续时间可被 60 整除的歌曲
在歌曲列表中,第 i 首歌曲的持续时间为 time[i] 秒. 返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量.形式上,我们希望索引的数字 i < j 且有 (time[i] + ...