题目: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]单旋的更多相关文章

  1. 洛谷 P3721 - [AH2017/HNOI2017]单旋(LCT)

    洛谷题面传送门 终于调出来这道题了,写篇题解( 首先碰到这样的题我们肯定要考虑每种操作会对树的形态产生怎样的影响: 插入操作:对于 BST 有一个性质是,当你插入一个节点时,其在 BST 上的父亲肯定 ...

  2. luogu P3721 [AH2017/HNOI2017]单旋

    传送门 \(Spaly:\)??? 考虑在暴力模拟的基础上优化 如果要插入一个数,那么根据二叉查找树的性质,这个点一定插在他的前驱的右子树或者是后继的左子树,可以利用set维护当前树里面的数,方便查找 ...

  3. 洛谷P3721 [AH2017/HNOI2017]单旋(线段树 set spaly)

    题意 题目链接 Sol 这题好毒瘤啊.. 首先要观察到几个性质: 将最小值旋转到根相当于把右子树变为祖先的左子树,然后将原来的根变为当前最小值 上述操作对深度的影响相当于右子树不变,其他的位置-1 然 ...

  4. [AH2017/HNOI2017]单旋

    题目 \(\rm splay\)水平太差,于是得手玩一下才能发现规律 首先插入一个数,其肯定会成为其前驱的右儿子或者是后继的左儿子,进一步手玩发现前驱的右儿子或者是后继的左儿子一定只有一个是空的,我们 ...

  5. [AH2017/HNOI2017] 单旋 - Splay

    Splay 暴力维护节点信息即可 #include<iostream> #include<cstdio> #include<cstring> #include< ...

  6. bzoj 4825: [Hnoi2017]单旋 [lct]

    4825: [Hnoi2017]单旋 题意:有趣的spaly hnoi2017刚出来我就去做,当时这题作死用了ett,调了5节课没做出来然后发现好像直接用lct就行了然后弃掉了... md用lct不知 ...

  7. 【LG3721】[HNOI2017]单旋

    [LG3721][HNOI2017]单旋 题面 洛谷 题解 20pts 直接模拟\(spaly\)的过程即可. 100pts 可以发现单旋最大.最小值到根,手玩是有显然规律的,发现只需要几次\(lin ...

  8. 4825: [Hnoi2017]单旋

    4825: [Hnoi2017]单旋 链接 分析: 以后采取更保险的方式写代码!!!81行本来以为不特判也可以,然后就总是比答案大1,甚至出现负数,调啊调啊调啊调~~~ 只会旋转最大值和最小值,以最小 ...

  9. [BZOJ4825][HNOI2017]单旋(线段树+Splay)

    4825: [Hnoi2017]单旋 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 667  Solved: 342[Submit][Status][ ...

随机推荐

  1. SQL 基础语法笔记教程整理

    最近从图书馆借了本介绍 SQL 的书,打算复习一下基本语法,记录一下笔记,整理一下思路,以备日后复习之用. PS:本文适用 SQL Server2008 语法. 首先,附一个发现的 MySQL 读书笔 ...

  2. Linux lvm 分区知识笔记

    盘面上可以细分出扇区(Sector)与柱面(Cylinder)两种单位,其中扇区每个为512bytes那么大. 通常所说的"硬盘分区"就是指修改磁盘分区表,它定义了"第n ...

  3. 给ThinkPad E470C 换个高分屏(1080P)

  4. 关于#ifdef #ifndef

    https://www.cnblogs.com/agnily/p/5848768.html 1.先看#ifdef的用法: #define KEY1_PA0 #ifdef KEY1_PA0 ------ ...

  5. try finally 执行顺序问题

    有return的情况下try catch finally的执行顺序 在遇到Exception 并且没有catch的情况下finally语句块没有执行 System.exit(0),终止了 Java 虚 ...

  6. 题解——loj6281 数列分块入门5 (分块)

    分块 若块内最大值为0或1,则不用再开方 然后暴力修改 可以证明,如果开方后向下取整,则最多开方4次一个数就会变成0或1 #include <cstdio> #include <cm ...

  7. 2.在Jenkins中配置及执行 maven + selenium + testng项目

    1. 在Jenkins中配置Maven与Git 1)在系统管理>管理插件>可选插件 页面分别下载Git plugin 与 Maven Integration plugin插件,安装完成后再 ...

  8. 解决github网站打开慢的问题

    一.前言 作为一名合格的程序员,github打开速度太慢怎么能容忍.但是可以通过修改hosts文件信息来解决这个问题.现在chrome访问github速度杠杠的! 二.macOS解决方法 打开host ...

  9. linux 打开一个文件现swap文件

    转自:http://blog.csdn.net/eckelwei/article/details/17078187 有时候在用vim打开文件时提示类似以下的信息: 发现交换文件 ".expo ...

  10. $(document).ready和window.onload,细微小区别,ready是jQuery的方法,onload是window的方法

    $(document).ready和window.onload的区别 $(document).ready和window.onload都是在都是在页面加载完执行的函数,大多数情况下差别不大,但也是有区别 ...