POJ 3481

这是利用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=1, rt=0;

struct Tree{
    int key, num, pri, son[2];
    void set(int _x, int _y, int _z){
        key=_x;
        num=_y;
        pri=_z;
        son[0]=son[1]=0;
    }
}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 == 0)
        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[0] && T[x].son[1])
        {
            int p=T[T[x].son[0]].pri > T[T[x].son[1]].pri;
            rotate(x, p);
            del(T[x].son[p], key);
        }
        else
        {
            if(!T[x].son[0]) x=T[x].son[1];
            else x=T[x].son[0];
        }
    }
    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 1:
                scanf("%d%d", &num, &key);
                ins(rt, key, num);
                break;
            case 2:
                x=get(rt, 1);
                if(x)
                {
                    printf("%d\n",T[x].num);
                    del(rt, T[x].key);
                }
                else
                    printf("0\n");
                break;
            case 3:
                x=get(rt, 0);
                if(x)
                {
                    printf("%d\n",T[x].num);
                    del(rt, T[x].key);
                }
                else
                    printf("0\n");
        }
    }
    return 0;
}
时间: 2024-12-15 01:43:32

POJ 3481的相关文章

poj 3481 Double Queue STL中map的运用

题意: 维护一个集合,操作有1:加入一个元素,2:删除最大元素,3:删除最小元素. 分析: map本质是个容器,且具有第一个关键字有序的性质,所以用它来水水就好啦~ 代码: //poj 3481 //sep9 #include <iostream> #include <map> using namespace std; map<int,int> mymap; map<int,int>::iterator iter; int main() { int x,su

POJ 3481 Double Queue(Treap模板题)

Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15786   Accepted: 6998 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provid

POJ 3481 &amp; HDU 1908 Double Queue (map运用)

题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by

POJ 3481 splay模板

最后撸一发splay. 之前用treap撸的,现在splay也找到感觉了,果然不同凡响,两者之间差别与精妙之处各有其精髓! 真心赞一个! POJ平衡树的题目还是比较少,只能挑之前做过的捏一捏.但是收获很多,这一天做的题都是有一定普遍性的. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cstdlib> #include

AVL树(模板题)—— POJ 3481 Double Queue

对应POJ题目:点击打开链接 Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11741   Accepted: 5335 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing env

跳跃表基础——POJ 3481 Double Queue

对应POJ 题目:点击打开链接 Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11768   Accepted: 5349 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing en

POJ - 3481 - Double Queue (STL)

Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11133   Accepted: 5056 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provid

poj 3481 平衡树

裸的treap #include<stdio.h> #include<algorithm> #include<stdlib.h> #include<cstring> #include<iostream> #include<string> #include<cmath> #include<vector> #include<queue> #include<map> #include<i

POJ 3481 Double Queue splay

题意:3个炒作,1 插入一个(值,优先级) 2  找优先级最大的输出值并删除  3,找优先值最小的输出值并删除. 解题思路:splay 解题代码: 1 // File Name: poj3481.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月09日 星期四 14时41分43秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<