现在的位置: 首页 > 综合 > 正文

poj 3481 Double Queue(Size Balance Tree)

2014年09月29日 ⁄ 综合 ⁄ 共 4639字 ⁄ 字号 评论关闭

Double Queue
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9309   Accepted: 4260

Description

The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by
a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed
to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:

0 The system needs to stop serving
K P Add client K to the waiting list with priority P
2 Serve the client with the highest priority and drop him or her from the waiting list
3 Serve the client with the lowest priority and drop him or her from the waiting list

Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

Input

Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same
client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.

Output

For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

Sample Input

2
1 20 14
1 30 3
2
1 10 99
3
2
2
0

Sample Output

0
20
30
10
0

Source

题意:有3种操作,1是添加用户名为k,优先级为p的用户,2是优先级最高的出队,3是优先级最低的出队,0结束操作
题解:可以用STL的priority queue,map等各种STL都可以过,不过本人试码了一下SB tree~
Size Balance tree
#include<stdio.h>
#define MAXN 100008
struct sbtree{
    int key,left,right,siz;
    int data;
    void init(){
        left=right=key=0;
        siz=1;
    }
}tree[MAXN];
int top,root=0;
void left_rot(int &x)
{
    int y=tree[x].right;
    tree[x].right=tree[y].left;
    tree[y].left=x;
    tree[y].siz=tree[x].siz;
    tree[x].siz=tree[tree[x].left].siz+tree[tree[x].right].siz+1;
    x=y;
}
void right_rot(int &x)
{
    int y=tree[x].left;
    tree[x].left=tree[y].right;
    tree[y].right=x;
    tree[y].siz=tree[x].siz;
    tree[x].siz=tree[tree[x].left].siz+tree[tree[x].right].siz+1;
    x=y;
}
void maintain(int &x,bool flag)
{
    if(flag==false)
    {
        if(tree[tree[tree[x].left].left].siz>tree[tree[x].right].siz)
        {
            right_rot(x);
        }
        else if(tree[tree[tree[x].left].right].siz>tree[tree[x].right].siz)
        {
            left_rot(tree[x].left);
            right_rot(x);
        }
        else return;
    }
    else
    {
        if(tree[tree[tree[x].right].right].siz>tree[tree[x].left].siz)
        {
            left_rot(x);
        }
        else if(tree[tree[tree[x].right].left].siz>tree[tree[x].left].siz)
        {
            right_rot(tree[x].right);
            left_rot(x);
        }
        else return;
    }
    maintain(tree[x].left,false);
    maintain(tree[x].right,true);
    maintain(x,false);
    maintain(x,true);
}
void myinsert(int &x,int key,int data)
{
    if(x==0)
    {
        x=++top;
        tree[x].init();
        tree[x].data=data;
        tree[x].key=key;
    }
    else
    {
        tree[x].siz++;
        if(key<tree[x].key) myinsert(tree[x].left,key,data);
        else myinsert(tree[x].right,key,data);
        maintain(x,key>=tree[x].key);
    }
}
int myremove(int &x,int key)
{
    int d_key;

    tree[x].siz--;
    if((key==tree[x].key)
       ||(key<tree[x].key&&tree[x].left==0)
       ||(key>tree[x].key&&tree[x].right==0))
    {
        d_key=tree[x].key;
        if(tree[x].left&&tree[x].right)
        {
            tree[x].key=myremove(tree[x].left,tree[x].key+1);
        }
        else
        {
            x=tree[x].left+tree[x].right;
        }
    }
    else if(key>tree[x].key) d_key=myremove(tree[x].right,key);
    else if(key<tree[x].key) d_key=myremove(tree[x].left,key);

    return d_key;
}
int get_min()
{
    int x;
    for(x=root;tree[x].left;x=tree[x].left);
    return x;
}
int get_max()
{
    int x;
    for(x=root;tree[x].right;x=tree[x].right);
    return x;
}
int get_pre(int &x,int y,int key)
{
    if(x==0) return y;
    if(tree[x].key<key) return get_pre(tree[x].right,x,key);
    else return get_pre(tree[x].left,y,key);
}
int get_next(int &x,int y,int key)
{
    if(x==0) return y;
    if(tree[x].key>key) return get_next(tree[x].left,x,key);
    else return get_next(tree[x].right,y,key);
}
int get_kth(int &x,int k)
{
    int r=tree[tree[x].left].siz+1;
    if(r==k) return tree[x].key;
    else if(r<k) return get_kth(tree[x].right,k-r);
    else return get_kth(tree[x].left,k);
}
int get_rank(int &x,int key)
{
    if(key<tree[x].key) return get_rank(tree[x].left,key);
    else if(key<tree[x].key) return get_rank(tree[x].right,key)+tree[tree[x].left].siz+1;
    return tree[tree[x].left].siz+1;
}
void inorder(int &x)
{
    if(x==0) return;
    inorder(tree[x].left);
    printf("%d\n",tree[x].key);
    inorder(tree[x].right);
}
/*--------------------我是万恶的模板分割线-----------------------*/
int main()
{
    int x,k,p;

    root=top=0;
    while(scanf("%d",&x),x)
    {
        if(x==1)
        {
            scanf("%d%d",&k,&p);
            myinsert(root,p,k);
        }
        else if(x==2)
        {
            x=get_max();
            printf("%d\n",tree[x].data);
            myremove(root,tree[x].key);
        }
        else
        {
            x=get_min();
            printf("%d\n",tree[x].data);
            myremove(root,tree[x].key);
        }
    }

    return 0;
}

抱歉!评论已关闭.