POJ 3481 treap
这是利用treap写的二叉排序树,只要理解其中旋转能够改变树的左右子树平衡度,即高度之差,差不多就能掌握treap树的要领了。
相对于其他高级BST,treap树实现应该算最简单了,利用的是随机树产生的理论的二叉排序树时间复杂度为O(nlgn)来实现,具体证明 算法导论 中有。
推荐NOCOW中的讲解,关于二叉排序树相当完整!
treap动画展示:http://www.ibr.cs.tu-bs.de/courses/ss98/audii/applets/BST/Treap-Example.html
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <ctime>
#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 Tree{
int key, num, pri, son[];
void set(int _x, int _y, int _z){
key=_x;
num=_y;
pri=_z;
son[]=son[]=;
}
}T[MAXN]; void rotate(int &x, int p) //1 右旋 0 左旋
{
int y=T[x].son[!p];
T[x].son[!p]=T[y].son[p];
T[y].son[p]=x;
x=y;
} void ins(int &x, int key, int num)
{
if(x == )
T[x=cnt++].set(key, num, rand());
else
{
int p=key < T[x].key;
ins(T[x].son[!p], key, num);
if(T[x].pri < T[T[x].son[!p]].pri) rotate(x, p);
}
} void del(int &x, int key)
{
if(T[x].key == key)
{
if(T[x].son[] && T[x].son[])
{
int p=T[T[x].son[]].pri > T[T[x].son[]].pri;
rotate(x, p);
del(T[x].son[p], key);
}
else
{
if(!T[x].son[]) x=T[x].son[];
else x=T[x].son[];
}
}
else
{
int p=T[x].key > key;
del(T[x].son[!p], key);
}
} int get(int x, int p)
{
while(T[x].son[p])
x=T[x].son[p];
return x;
} int main ()
{
srand(time(NULL));
int p, key, num, x;
while(scanf("%d", &p) && p)
{
switch (p)
{
case :
scanf("%d%d", &num, &key);
ins(rt, key, num);
break;
case :
x=get(rt, );
if(x)
{
printf("%d\n",T[x].num);
del(rt, T[x].key);
}
else
printf("0\n");
break;
case :
x=get(rt, );
if(x)
{
printf("%d\n",T[x].num);
del(rt, T[x].key);
}
else
printf("0\n");
}
}
return ;
}
POJ 3481 treap的更多相关文章
- POJ 3481 Double Queue(Treap模板题)
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15786 Accepted: 6998 Des ...
- POJ 3481 Double Queue STLmap和set新学到的一点用法
2013-08-08 POJ 3481 Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...
- POJ 3481 Double Queue (treap模板)
Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...
- 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 1442 treap
裸treap. 只需增加一个size记录其儿子个数便可找到第k大数. #include <cstdio> #include <cstring> #include <cti ...
- POJ 2352 treap
当年经常遇到这种题,愣是没做出来,好像那时不会线段树,也不会平衡树. 凭借一身蛮力来搞,倒是和那群朋友搞得开开心心. 题意: y从小到大,若y相同,x从小到大,这样给出一些坐标,求每个点覆盖的点个数. ...
- POJ 2985 Treap平衡树(求第k大的元素)
这题也能够用树状数组做,并且树状数组姿势更加优美.代码更加少,只是这个Treap树就是求第K大元素的专家--所以速度比較快. 这个也是从那本红书上拿的模板--自己找了资料百度了好久,才理解这个Trea ...
随机推荐
- Unity中的 原生插件/平台交互 原理
http://blog.csdn.net/u010019717/article/details/78451660 声明: 内容摘录自: http://gad.qq.com/article/deta ...
- MySQL Crash Course #11# Chapter 20. Updating and Deleting Data
INDEX Updating Data The IGNORE Keyword Deleting Data Faster Deletes Guidelines for Updating and Dele ...
- Linux 环境 HTTP 服务器
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <netinet/in ...
- Codeforces Round#413 Problem A - C
Problem#A Carrot Cakes vjudge链接[here] (偷个懒,cf链接就不给了) 题目大意是说,烤面包,给出一段时间内可以考的面包数,建第二个炉子的时间,需要达到的面包数,问建 ...
- Python3基础 函数 可变参数,将传进来的参数转成列表
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 怎样用QtCreator编辑运行python脚本
QtCreator作为一款开发基于qt库的程序.以及开发C语言.c++语言项目都是一个利器,轻便好用.那么作为开发者来说,经常换着使用各种IDE是家常便饭,但是要是这些语言都能够集成到一个工具里岂不是 ...
- 不同ContentType的post请求
public static T Invoke<T>(string url, object input, bool requireJSON = true) { using (var clie ...
- poj 2481 Cows(树状数组)题解
Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...
- redis.conf 配置 详解 中文 2.8
# redis version 2.8.19 # 1k => 1000 bytes# 1kb => 1024 bytes# 1m => 1000000 bytes# 1m ...
- HDU 1863 畅通工程 (最小生成树
看卿学姐视频学到的题目 kruskal算法实现最小生成树 #include<bits/stdc++.h> using namespace std; ; typedef long long ...