HNOI2017 单旋
题目描述
网址:https://www.luogu.org/problemnew/show/3721
大意:
有一颗单旋Splay(Spaly),以key值为优先度,总共有5个操作。
- [1] 插入一个节点,需返还插入后此节点的深度。
- [2] 把最小点单旋到根,需要返还旋转前此点深度。
- [3] 把最大点单旋到根,需要返还旋转前此点深度。
- [4] 把最小点单旋到根,然后删除根,需要返还旋转前此点深度。
- [5] 把最大点单旋到根,然后删除根,需要返还旋转前此点深度。
总共有M个操作,数据范围:M <= 100000
题目解法
题目提出的为splay算法,那么正解肯定不是splay。
观察到所有旋转操作都是对极值进行操作的,又是单旋,手玩一下就会发现树的形态基本不改变。
所以,我们可以建立一棵普通二叉树,由于每次修改的点都为常数个。
所以对于修改操作,手动修改即可。
然后考虑查询深度的问题,可以用LCT维护,修改普通二叉树对应的Link、Cut即可维持树的形态。
用LCT查询点u的深度,只需要把u到root的路径变为重路径,查询这棵辅助树的大小即可
最后是插入操作。用set可快速查询前驱与后继。
可以发现一定是插在两者中深度较大的下面,与前面一样直接修改即可。
实现代码
#include<bits/stdc++.h>
#define ll long long
#define RG register
#define IL inline
#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
#define maxn 210000
using namespace std;
IL int gi(){ RG int date = 0, m = 1; RG char ch = 0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch = getchar();
if(ch == '-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{date=date*10+ch-'0'; ch = getchar();}
return date*m;
}
int root,cnt,M;
int Stk[maxn],ch[maxn][2],fa[maxn],rev[maxn],sz[maxn],fth[maxn];
int tr[maxn][2];
IL bool Son(int x){ return rs(fa[x]) == x; }
IL bool Isroot(int x){return (ls(fa[x])!=x && rs(fa[x])!=x) ; }
IL void PushUp(int x){ sz[x] = 1+sz[ls(x)]+sz[rs(x)]; }
IL void PushDown(int x){
if(!rev[x])return; rev[x] = 0;
rev[ls(x)]^=1; rev[rs(x)]^=1; swap(ls(x) , rs(x));
}
IL void Rot(RG int x){
RG int y = fa[x],z = fa[y],c = Son(x);
if(!Isroot(y))ch[z][Son(y)] = x; fa[x] = z;
ch[y][c] = ch[x][!c]; fa[ch[y][c]] = y;
ch[x][!c] = y; fa[y] = x; PushUp(y);
}
IL void Splay(RG int x){
RG int top = 0; Stk[++top] = x;
for(RG int i = x; !Isroot(i); i = fa[i])Stk[++top] = fa[i];
while(top)PushDown(Stk[top--]);
for(RG int y = fa[x]; !Isroot(x); Rot(x),y = fa[x])
if(!Isroot(y))Son(x) ^ Son(y) ? Rot(x) : Rot(y);
PushUp(x);
}
IL void Access(int x){
for(RG int y = 0; x; y = x,x = fa[x])
Splay(x),ch[x][1] = y,PushUp(x);
}
IL void Makeroot(int x){Access(x);Splay(x); rev[x]^=1; }
IL void Split(int x,int y){ Makeroot(x); Access(y); Splay(y); }
IL void Link(int x,int y){ if(!x||!y)return; Makeroot(x); fa[x] = y; }
IL void Cut(int x,int y){
if(!x||!y)return;
Split(x,y); rs(y) = fa[x] = 0;
PushUp(x); PushUp(y);
}
IL int Query(RG int x){Makeroot(root); Access(x); Splay(x); return sz[x];}
set<int>S; set<int>::iterator bf,aft,it;
map<int,int>mp;
IL void NewNode(){cnt++; fa[cnt] = 0; sz[cnt] = 1;}
IL int Insert(){
RG int data = gi(); S.insert(data);
RG int dep,cs,dep1=0,dep2=0,v1,v2;
bf = S.find(data); aft = bf; aft++;
NewNode(); mp[data] = cnt;
if(!root){root = cnt; return 1;}
if(aft != S.end()){ v1 = mp[*aft]; dep1 = Query(v1); }
if(bf !=S.begin()){bf--; v2 = mp[*bf]; dep2 = Query(v2);}
cs = (dep1>dep2) ? v1:v2; dep = max(dep1,dep2);
if(cs==v1)tr[cs][0] = cnt,fth[cnt] = cs;
if(cs==v2)tr[cs][1] = cnt,fth[cnt] = cs;
Link(cs,cnt); return dep+1;
}
IL int Findmin1(){
it = S.begin(); RG int p = mp[*it],dep;
dep = Query(p);
RG int x = p,sn = tr[x][1],ft = fth[x];
if(x == root)return 1;
Cut(x,sn); Cut(ft,x); Link(x,root); Link(ft,sn);
fth[x] = 0; tr[x][1] = root; fth[root] = x; tr[ft][0] = sn; fth[sn] = ft;
root = x; return dep;
}
IL int Findmax1(){
it = S.end(); it--; RG int p = mp[*it],dep;
dep = Query(p);
RG int x = p,sn = tr[x][0],ft = fth[x];
if(x == root)return 1;
Cut(x,sn); Cut(ft,x); Link(x,root); Link(ft,sn);
fth[x] = 0; tr[x][0] = root; fth[root] = x; tr[ft][1] = sn; fth[sn] = ft;
root = x; return dep;
}
IL int Findmin2(){
RG int dep = Findmin1(),rt = root,sn = tr[root][1];
fth[sn] = 0; Cut(root,sn);
root = sn;
tr[rt][1] = tr[rt][0] = 0;
it = S.begin(); S.erase(it);
return dep;
}
IL int Findmax2(){
RG int dep = Findmax1(),rt = root,sn = tr[root][0];
fth[sn] = 0; Cut(root,sn);
root = sn;
tr[rt][1] = tr[rt][0] = 0;
it = S.end(); it--; S.erase(it);
return dep;
}
IL void Work(){
RG int c = gi();
if(c == 1)printf("%d\n",Insert());
else if(c == 2)printf("%d\n",Findmin1());
else if(c == 3)printf("%d\n",Findmax1());
else if(c == 4)printf("%d\n",Findmin2());
else if(c == 5)printf("%d\n",Findmax2());
}
int main()
{
freopen("testdate.in","r",stdin);
M = gi();
root = 0; while(M--)Work();
return 0;
}
HNOI2017 单旋的更多相关文章
- 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][ ...
- 【BZOJ4825】[Hnoi2017]单旋 线段树+set
[BZOJ4825][Hnoi2017]单旋 Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能 ...
- bzoj4825 [Hnoi2017]单旋
Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的必 ...
- BZOJ:4825: [Hnoi2017]单旋
Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的必 ...
- HNOI2017单旋
单旋 这道题做法贼多,LCT,splay,线段树什么的貌似都行. 像我这种渣渣只会线段树了(高级数据结构学了也不会用). 首先离线所有操作,因为不会有两个点值重复,所以直接离散. 一颗线段树来维护所有 ...
- P3721 [AH2017/HNOI2017]单旋
题目:https://www.luogu.org/problemnew/show/P3721 手玩一下即可AC此题. 结论:插入x后,x要么会成为x的前驱的右儿子,要么成为x的后继的左儿子,这取决于它 ...
随机推荐
- PHP判断SQL语句是否合法:mysqli_error()
假设现在有条update语句,有时候update语句正确,但是受影响的行数是0. 那么怎么判断这条SQL语句到底是否正确?使用 mysqli_error($Conn); create table us ...
- iOS7控制中心会覆盖由下向上的手势
Expect users to swipe up from the bottom of the screen to reveal Control Center. If iOS determines t ...
- JetBrains Rider 破解 (ideaIU等等开发工具都通用)2018-02-27
贴一下Rider下载地址:(下载不了可以用百度云离线下载) Win:https://download.jetbrains.com/resharper/JetBrains.Rider-2017.3.1. ...
- Oracle创建表时Storage参数具体含义
本文通过图表和实例的阐述在Oracle数据库创建新表时Storage的参数具体含义. 可用于:表空间.回滚段.表.索引.分区.快照.快照日志 参数名称 缺省值 最小值 最大值 说明 INITIAL 5 ...
- 手把手教你树莓派实现简易室内监控系统(C)之BOA服务器的搭建
本篇主要讲利用BOA服务器做室内监控系统的服务器端. 古人云:万事开头靠百度,实在不行就Google.小编也是一步一步的,亲自搭建成功,不能说是万全之策,仅仅是给大家一个参考就满足了. 第一步: 1. ...
- C++学习笔记第三天:类、虚函数、双冒号
类 class Box { public: double length; // 盒子的长度 double breadth; // 盒子的宽度 double height; // 盒子的高度 }; 类成 ...
- basler 相机拍照简单类综合Emgu.CV---得到图档
在网上找了半天都是下载要钱,自己试做了,经测试能ok,一起分享吧.给初学的人一点鼓励. using System;using System.Collections.Generic;using Syst ...
- nyoj 1022 合纵连横 经典并查集
思路:关键在于并查集的删点操作. 给每个诸侯国一个另外的编号,比如box[i]表示诸侯国i现在处于第box[i]个联盟,可以随时改变它的联盟编号,并且让box[i] = k, 实现删除操作.以前联盟中 ...
- linux pxe网络装机无人值守
项目分析远程装机的实现:配置DHCP+HTTP+TFTP提供通过vesamenu.c32模块实现图形PXE菜单为不同系统分别提供ks应答文件将第三方rpm包以yum源的方式提供:集中提供ntfs-3g ...
- html、text、val、attr、prop区别。this.value和$(this).val()区别以及return用法
html(): html() 方法返回或设置被选元素的内容 (inner HTML). 当使用该方法读取多个值时,它会返回第一个匹配元素的内容. 当使用该方法设置一个值时,它会覆盖所有匹配元素的内容. ...