BZOJ

Luogu

题目太长了,就不放了。

题解

首先声明一点,无论是splay还是spaly,插入一个新的元素,都要rotate到根!所以说题目也算是给了一个错误示范吧。

我们发现把最值旋转到根并不会给这棵spaly造成多大的形态改变,所以考虑用LCT维护这个spaly的形态。

每次插入的时候我们用一个Set来找出插入元素的前驱后继。可以证明,一定会插在前驱或者是后继的对应左右儿子上。判断一下插入即可。除了LCT里的splay外,还要维护一下原树中(spaly中)的父亲、左右儿子关系。

code

细节。。还好吧

我tm又双叒叕把一个l打成r了

#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
const int N = 100005;
int fa[N],ls[N],rs[N],rev[N],sz[N],Stack[N],top;
int gi()
{
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
bool isroot(int x){return ls[fa[x]]!=x&&rs[fa[x]]!=x;}
void pushup(int x){sz[x]=sz[ls[x]]+sz[rs[x]]+1;}
void pushdown(int x){if (!rev[x]) return;swap(ls[x],rs[x]);rev[ls[x]]^=1;rev[rs[x]]^=1;rev[x]=0;}
void R_rotate(int x)
{
int y=fa[x],z=fa[y];
ls[y]=rs[x];
if (rs[x]) fa[rs[x]]=y;
fa[x]=z;
if (!isroot(y)) if (y==ls[z]) ls[z]=x;else rs[z]=x;
rs[x]=y;fa[y]=x;
pushup(y);
}
void L_rotate(int x)
{
int y=fa[x],z=fa[y];
rs[y]=ls[x];
if (ls[x]) fa[ls[x]]=y;
fa[x]=z;
if (!isroot(y)) if (y==ls[z]) ls[z]=x;else rs[z]=x;
ls[x]=y;fa[y]=x;
pushup(y);
}
void splay(int x)
{
Stack[top=1]=x;
for (int i=x;!isroot(i);i=fa[i])
Stack[++top]=fa[i];
while (top) pushdown(Stack[top--]);
while (!isroot(x))
{
int y=fa[x],z=fa[y];
if (isroot(y))
if (x==ls[y]) R_rotate(x);
else L_rotate(x);
else
if (y==ls[z])
if (x==ls[y]) R_rotate(y),R_rotate(x);
else L_rotate(x),R_rotate(x);
else
if (x==ls[y]) R_rotate(x),L_rotate(x);
else L_rotate(y),L_rotate(x);
}
pushup(x);
}
void access(int x){for (int y=0;x;y=x,x=fa[x]) splay(x),rs[x]=y,pushup(x);}
void makeroot(int x){access(x);splay(x);rev[x]^=1;}
void split(int x,int y){makeroot(x);access(y);splay(y);}
void link(int x,int y){makeroot(x);fa[x]=y;}
void cut(int x,int y){split(x,y);fa[x]=ls[y]=0;}
map<int,int>M;
set<int>S;
int m,tot,root,ff[N],lson[N],rson[N];
void Insert(int x)
{
int now=++tot;
M[x]=now;
if (S.empty())
{
S.insert(x);
root=now;
puts("1");
return;
}
set<int>::iterator t=S.upper_bound(x);
if (t==S.end()||lson[M[*t]])
{
--t;
int father=M[*t];
link(now,father);rson[father]=now;ff[now]=father;
}
else
{
int father=M[*t];
link(now,father);lson[father]=now;ff[now]=father;
}
S.insert(x);
split(now,root);
printf("%d\n",sz[root]);
}
void Spl_Min()
{
int x=M[*S.begin()];
if (x==root) {puts("1");return;}
split(x,root);printf("%d\n",sz[root]);
cut(x,ff[x]);if (rson[x]) cut(x,rson[x]);
link(x,root);if (rson[x]) link(ff[x],rson[x]);
lson[ff[x]]=rson[x];if (rson[x]) ff[rson[x]]=ff[x];
ff[x]=0;ff[root]=x;rson[x]=root;
root=x;
}
void Spl_Max()
{
int x=M[*--S.end()];
if (x==root) {puts("1");return;}
split(x,root);printf("%d\n",sz[root]);
cut(x,ff[x]);if (lson[x]) cut(x,lson[x]);
link(x,root);if (lson[x]) link(ff[x],lson[x]);
rson[ff[x]]=lson[x];if (lson[x]) ff[lson[x]]=ff[x];
ff[x]=0;ff[root]=x;lson[x]=root;
root=x;
}
void Del_Min()
{
int x=M[*S.begin()];
if (x==root)
{
puts("1");
if (rson[x]) cut(x,rson[x]);
ff[root=rson[x]]=0;
S.erase(S.begin());
return;
}
split(x,root);printf("%d\n",sz[root]);
cut(x,ff[x]);if (rson[x]) cut(x,rson[x]);
if (rson[x]) link(ff[x],rson[x]);
lson[ff[x]]=rson[x];if (rson[x]) ff[rson[x]]=ff[x];
S.erase(S.begin());
}
void Del_Max()
{
int x=M[*--S.end()];
if (x==root)
{
puts("1");
if (lson[x]) cut(x,lson[x]);
ff[root=lson[x]]=0;
S.erase(--S.end());
return;
}
split(x,root);printf("%d\n",sz[root]);
cut(x,ff[x]);if (lson[x]) cut(x,lson[x]);
if (lson[x]) link(ff[x],lson[x]);
rson[ff[x]]=lson[x];if (lson[x]) ff[lson[x]]=ff[x];
S.erase(--S.end());
}
int main()
{
m=gi();
while (m--)
{
int opt=gi();
if (opt==1) Insert(gi());
if (opt==2) Spl_Min();
if (opt==3) Spl_Max();
if (opt==4) Del_Min();
if (opt==5) Del_Max();
}
return 0;
}

[BZOJ4825][HNOI2017]单旋spaly的更多相关文章

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

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

  2. bzoj4825 [Hnoi2017]单旋

    Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的必 ...

  3. BZOJ4825: [Hnoi2017]单旋(Splay)

    题面 传送门 题解 调了好几个小时--指针太难写了-- 因为只单旋最值,我们以单旋\(\min\)为例,那么\(\min\)是没有左子树的,而它旋到根之后,它的深度变为\(1\),它的右子树里所有节点 ...

  4. BZOJ4825 [Hnoi2017]单旋 【线段树】

    题目链接 BZOJ4825 题解 手模一下操作,会发现一些很优美的性质: 每次旋到根,只有其子树深度不变,剩余点深度\(+1\) 每次旋到根,[最小值为例]右儿子接到其父亲的左儿子,其余点形态不改变, ...

  5. 【BZOJ4825】[Hnoi2017]单旋 线段树+set

    [BZOJ4825][Hnoi2017]单旋 Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能 ...

  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]单旋 线段树+STL-set

    题目描述 H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的必修技能.有一天 ...

随机推荐

  1. 购物车【JavaWeb小项目、简单版】

    前言 为了巩固MVC的开发模式,下面就写一个购物车的小案例.. ①构建开发环境 导入需要用到的开发包 建立程序开发包 ②设计实体 书籍实体 public class Book { private St ...

  2. 免费 Https 证书(Let's Encrypt)申请与配置

    之前要申请免费的 https 证书操作步骤相当麻烦,今天看到有人在讨论,就搜索了一下.发现现在申请步骤简单多了. 1. 下载 certbot git clone https://github.com/ ...

  3. css渲染(一) 字体和文本

    一.字体属性 1.默认字体系列 chrome/opera:"宋体" firefox:"微软雅黑" safari/IE:Times,"宋体" ...

  4. MySql全国省市区SQL语句

    CREATE TABLE IF NOT EXISTS `province` (   `id` int(11) NOT NULL auto_increment,   `provinceID` int(1 ...

  5. Centos下_MysqL5.7在使用mysqldump命令备份数据库报错:mysqldump: Got error: 1449: The user specified as a definer ('fk_system'@'localhost') does not exist when using LOCK TABLES

    在阿里云服务器增加一个shell脚本定时备份数据库脚本执行任务时,测试性的执行了备份命令,如下 [root@iZ2ze503xw2q1fftv5rhboZ mysql_bak]# /usr/local ...

  6. python str与bytes之间的转换

    # bytes object b = b"example" # str object s = "example" # str to bytes bytes(s, ...

  7. PAT1119. Pre- and Post-order Traversals

    思路:中序遍历–根结点,左子树,右子树:后序遍历–左子树,右子树,根结点. 那么在找到根结点之后就可以开始划分左右子树了.左子树的先序第一个节点是根,左子树的后序最后一个节点是根. 例如 1 2 3 ...

  8. CodeForces - 796C Bank Hacking

    思路:共有n-1条边连接n个点,即形成一棵树.一开始需要选择一个点hack--将这个点视为根结点,与它相邻的点防御值加1,与它相隔一个在线点的点的防御也加1.当根节点被hack,即这个点被删除,又变成 ...

  9. 常用的CSS框架

    常用的CSS框架 之前在写自己的个人网站的时候,由于自己Web前端不是特别好,于是就去找相关的CSS框架来搭建页面了. 找到以下这么一篇文章(列出了很多常用的CSS框架): http://w3scho ...

  10. 【SSH框架】系列之 Spring 整合 Hibernate 框架

    1.SSH 三大框架整合原理 Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建. Spring 与 Hibernate 的整合就是将 Session ...