POJ 3481 SBT做法
第三次做此题。。
不解释啦。
不过变成用SBT来做啦!
SBT好处在于能够保证树的高度为lgn,真真正正的平衡二叉树。
因此删除,插入操作与普通二叉树几乎相同。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define INF 0x3f3f3f3f
#define MAXN 100005 using namespace std; int cnt, rt; struct SBT
{
int key, size, son[], num;
}T[MAXN]; inline void PushUp(int x)
{
T[x].size=T[T[x].son[]].size+T[T[x].son[]].size+;
} inline int Newnode(int key, int num)
{
++cnt;
T[cnt].num=num;
T[cnt].key=key;
T[cnt].size=;
T[cnt].son[]=T[cnt].son[]=;
return cnt;
} void Rotate(int p, int &x)
{
int y=T[x].son[!p];
T[x].son[!p]=T[y].son[p];
T[y].son[p]=x;
PushUp(x);
PushUp(y);
x=y;
} void Maintain(int &x, int p) //维护SBT的!p子树
{
if(T[T[T[x].son[p]].son[p]].size > T[T[x].son[!p]].size)
Rotate(!p, x);
else if(T[T[T[x].son[p]].son[!p]].size > T[T[x].son[!p]].size)
Rotate(p, T[x].son[p]), Rotate(!p, x);
else return;
Maintain(T[x].son[], );
Maintain(T[x].son[], );
Maintain(x, );
Maintain(x, );
} void Insert(int key, int &x, int num)
{
if(!x) x=Newnode(key, num);
else
{
T[x].size++;
Insert(key, T[x].son[key > T[x].key], num);
Maintain(x, key > T[x].key);
}
} bool Delete(int key, int &x) //删除值为key的节点 key可以不存在
{
if(!x) return ;
if(T[x].key == key)
{
if(!T[x].son[])
{
x=T[x].son[];
return ;
}
if(!T[x].son[])
{
x=T[x].son[];
return ;
}
int y=T[x].son[];
while(T[y].son[])
y=T[y].son[];
T[x].key=T[y].key;
T[x].size--;
return Delete(T[x].key, T[x].son[]);
}
else
if(Delete(key, T[x].son[key > T[x].key]))
{
T[x].size--;
return ;
}
} int GetPth(int p, int &x)
{
if(!x) return ;
if(p == T[T[x].son[]].size+)
return x;
if(p > T[T[x].son[]].size+)
return GetPth(p-T[T[x].son[]].size-, T[x].son[]);
else
return GetPth(p, T[x].son[]);
} int main ()
{
int p, key, num, x;
while(scanf("%d", &p) && p)
{
switch (p)
{
case :
scanf("%d%d", &num, &key);
Insert(key, rt, num);
break;
case :
x=GetPth(T[rt].size, rt);
if(x)
{
printf("%d\n",T[x].num);
Delete(T[x].key, rt);
}
else
printf("0\n");
break;
case :
x=GetPth(, rt);
if(x)
{
printf("%d\n",T[x].num);
Delete(T[x].key, rt);
}
else
printf("0\n");
}
}
return ;
}
POJ 3481 SBT做法的更多相关文章
- POJ 3481 Double Queue STLmap和set新学到的一点用法
2013-08-08 POJ 3481 Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...
- POJ 3481 Double Queue(Treap模板题)
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15786 Accepted: 6998 Des ...
- POJ 3481 & HDU 1908 Double Queue (map运用)
题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Descript ...
- POJ 3481 splay模板
最后撸一发splay. 之前用treap撸的,现在splay也找到感觉了,果然不同凡响,两者之间差别与精妙之处各有其精髓! 真心赞一个! POJ平衡树的题目还是比较少,只能挑之前做过的捏一捏.但是收获 ...
- POJ 3481 Double Queue
平衡树.. 熟悉些fhq-Treap,为啥我在poj读入优化不能用啊 #include <iostream> #include <cstdio> #include <ct ...
- POJ 3481 set水过
题意:1表示插入客户K,他的优先级是P(相当于大小),2表示输出当前优先级最高的客户(即找出最大值),并且删除.3同理输出最低级的. 这题可以用splay treap AVL SBT -- (可是我并 ...
- POJ 3481 Double Queue(STL)
题意 模拟银行的排队系统 有三种操作 1-加入优先级为p 编号为k的人到队列 2-服务当前优先级最大的 3-服务当前优先级最小的 0-退出系统 能够用stl中的map 由于map本身 ...
- POJ 3481 treap
这是利用treap写的二叉排序树,只要理解其中旋转能够改变树的左右子树平衡度,即高度之差,差不多就能掌握treap树的要领了. 相对于其他高级BST,treap树实现应该算最简单了,利用的是随机树产生 ...
- POJ 3481 Double Queue(set实现)
Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...
随机推荐
- cursor图标自定义
cursor: url(./images/favicon.ico), auto; 首先auto必须加上,其次必须使用ico文件,目前来说ico文件没有兼容性问题,ico格式怎么转? 传送门:http: ...
- bzoj 2427 软件安装 - Tarjan - 树形动态规划
题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大). 但是 ...
- libcurl HTTP POST请求向服务器发送json数据
转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...
- FTP-IIS Web
快速搭建一个本地的FTP服务器 如果需要开发FTP文件上传下载功能,那么需要在本机上搭建一个本地FTP服务器,方便调试. 第一步:配置IIS Web服务器 1.1 控制面板中找到“程序”并打开 1 ...
- 托管C++调用C#
拿到了一个第三方demo,有dll,有.cpp..h,打开解决方案,如下图: 网上资料貌似很少,根据猜测: 这是使用托管C++来调用C#的方式. 过程: 1.先使用C#代码实现界面和功能,其实就是一个 ...
- Python3基础 file for+list 读取txt文本 并 一行一行的输出(低效率)
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- MacBook PRO蓝牙无法搜索设备
背景 经常把MacBook合上盖子就塞进包里,用时打开盖子就继续操作,偶尔会出现刚刚还在用的罗技蓝牙鼠标,重新打开笔记本后就连接不上了,而且也无法搜索到周边的蓝牙设备. 解决方案 快捷键:Option ...
- 【第一章】 第一个spring boot程序
环境: jdk:1.8.0_73 maven:3.3.9 spring-boot:1.2.5.RELEASE(在pom.xml中指定了) 注意:关于spring-boot的支持, 最少使用jdk7(j ...
- C# Byte[] 数组操作
byte[] Strbyte = Encoding.GetEncoding("big5").GetBytes(str); if (Strbyte.Length ...
- C# 测算代码运行时间 Stopwatch
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); stop ...