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<iterator>
#include<stack>

using namespace std;

#define ll   __int64
#define MAXN  500010
#define inf  2000000007

struct node
{
    struct node *ch[2];
    int sz,key,num,pri;
};
typedef node * tree;
node base[MAXN],nil;
tree root,null,top;

void Init()
{
    top=base;
    root=null=&nil;
    null->ch[0]=null->ch[1]=null;
    null->key=null->pri=null->num=2147483647;
    null->sz=0;
}
inline int random()
{
    static int seed=703;
    return seed=int(seed *48271ll %2147483647);
}
inline tree newnode(int num,int key)
{
    top->num=num;
    top->key=key;
    top->pri=random();
    top->sz=1;
    top->ch[0]=top->ch[1]=null;
    return top++;
}
void Rotate(tree &x,bool d)
{
    tree y = x->ch[!d];
    x->ch[!d]=y->ch[d];
    y->ch[d]=x;
    x->sz=x->ch[0]->sz+1+x->ch[1]->sz;
    y->sz=y->ch[0]->sz+1+y->ch[1]->sz;
    x=y;
}
void Insert(tree &t,int num,int key)
{
    if(t==null)
        t=newnode(num,key);
    else
    {
        bool d=key>t->key;
        Insert(t->ch[d],num,key);
        (t->sz)++;
        if(t->pri<t->ch[d]->pri)
            Rotate(t,!d);
    }
}
void Delete(tree &t,int key)
{
    if(t->key!=key)
    {
        Delete(t->ch[key>t->key],key);
        (t->sz)--;
    }
    else if(t->ch[0]==null)
        t=t->ch[1];
    else if(t->ch[1]==null)
        t=t->ch[0];
    else
    {

        bool d=t->ch[0]->pri<t->ch[1]->pri;
        Rotate(t,d);
        Delete(t->ch[d],key);
    }
}
tree select(int k) //第k个
{
    tree t=root;
    int tmp;
    for(;;)
    {
        tmp=t->ch[0]->sz+1;
        if(k==tmp)
            return t;
        else if(k>tmp)
        {
            k-=tmp;
            t=t->ch[1];
        }
        else
            t=t->ch[0];
    }
}
int cnt;
int main()
{
    Init();
    int ty;
    cnt=0;

    while(scanf("%d",&ty)!=EOF)
    {
        if(ty==0)
            break;
        if(ty==1)
        {
            int num,key;
            scanf("%d%d",&num,&key);
            Insert(root,num,key);
            cnt++;
        }
        else if(ty==2)
        {
            if(cnt>0)
            {
                tree a=select(cnt);
                int b=a->key;
                //printf("%d\n",b);
                Delete(root,b);
                printf("%d\n",a->num);
                cnt--;
            }
            else
                printf("0\n");
        }
        else
        {
            if(cnt>0)
            {
                tree a=select(1);
                int b=a->key;
                Delete(root,b);
                printf("%d\n",a->num);
                cnt--;
            }
            else
                printf("0\n");
        }
    }
    return 0;
}

时间: 2024-10-13 16:05:40

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 splay模板

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

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

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 堆修改标记

Enemy Double Queue! 题目大意:维护一种数据结构,支持以下操作: 1.插入一个值 2.查询最大值并删除 3.查询最小值并删除 元素的值<=1000W 这数据结构一看就是堆...不过堆结构不能同时维护最大值和最小值,于是我们开两个堆,一个大根堆,一个小根堆 其中一堆删除时,另一堆也要删除相应元素 于是删除的话有两种方法 1.映射 1000W开数组映射妥妥MLE 于是我们在两个堆之间互相映射 不太好写 pair里开自己的指针会报错,于是只能开了void* 不过速度还是很可观的 #i

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写的二叉排序树,只要理解其中旋转能够改变树的左右子树平衡度,即高度之差,差不多就能掌握treap树的要领了. 相对于其他高级BST,treap树实现应该算最简单了,利用的是随机树产生的理论的二叉排序树时间复杂度为O(nlgn)来实现,具体证明 算法导论 中有. 推荐NOCOW中的讲解,关于二叉排序树相当完整! treap动画展示:http://www.ibr.cs.tu-bs.de/courses/ss98/audii/applets/BST/Treap-Example.htm