POJ 3481 Double Queue(Splay)

原题链接

Problem 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
1 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

题目大意

一共有4种操作,0表示结束,1 a b表示插入优先级为b的一个数a,2表示为输出优先级最高的数并移出队列,3表示输出优先级最低的数并移出队列。

解题思路

由于Splay具有二叉搜索树的性质,适用于这样要按照优先级大小排列的队列。通过Splay操作可以高效地完成插入操作。

AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define rep(i,n) for(int i = 0;i < n; i++)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define mp make_pair
#define exp 2.7182818
#define PI 3.141592653589793
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define eps 1e-8
#define mod 1000000007ll
#define sign(x) ((x)>eps?1:((x)<-eps?(-1):(0)))
using namespace std;
double mysqrt(double x) { return max(0.0, sqrt(x)); }

const int MAXN=100010;
int cnt=1, rt=0;
struct Tree
{
    int key, size, fa, son[2], num;
    void set(int _key, int _size, int _fa, int _num)
    {
        key=_key;
        size=_size;
        fa=_fa;
        son[0]=son[1]=0;
        num=_num;
    }
}T[MAXN];

inline void PushUp(int x)
{
    T[x].size=T[T[x].son[0]].size+T[T[x].son[1]].size+1;
}

inline void Rotate(int x, int p) 
{
    int y=T[x].fa;
    T[y].son[!p]=T[x].son[p];
    T[T[x].son[p]].fa=y;
    T[x].fa=T[y].fa;
    if(T[x].fa)
        T[T[x].fa].son[T[T[x].fa].son[1] == y]=x;
    T[x].son[p]=y;
    T[y].fa=x;
    PushUp(y);
    PushUp(x);
}

void Splay(int x, int To) 
{
    while(T[x].fa != To)
    {
        if(T[T[x].fa].fa == To)
            Rotate(x, T[T[x].fa].son[0] == x);
        else
        {
            int y=T[x].fa, z=T[y].fa;
            int p=(T[z].son[0] == y);
            if(T[y].son[p] == x)
                Rotate(x, !p), Rotate(x, p); 
            else
                Rotate(y, p), Rotate(x, p);    
        }
    }
    if(To == 0) rt=x;
}

int find(int key) 
{
    int x=rt;
    while(x && T[x].key != key)
        x=T[x].son[key > T[x].key];
    if(x) Splay(x, 0);
    return x;
}

int prev() 
{
    int x=T[rt].son[0];
    if(!x) return 0;
    while(T[x].son[1])
        x=T[x].son[1];
    Splay(x, 0);
    return x;
}

int succ() 
{
    int x=T[rt].son[1];
    if(!x) return 0;
    while(T[x].son[0])
        x=T[x].son[0];
    Splay(x, 0);
    return x;
}

void Insert(int key, int num) 
{
    if(!rt)
        T[rt = cnt++].set(key, 1, 0, num);
    else
    {
        int x=rt, y=0;
        while(x)
        {
            y=x;
            x=T[x].son[key > T[x].key];
        }
        T[x = cnt++].set(key, 1, y, num);
        T[y].son[key > T[y].key]=x;
        Splay(x, 0);
    }
}

void Delete(int key) 
{
    int x=find(key);
    if(!x) return;
    int y=T[x].son[0];
    while(T[y].son[1])
        y=T[y].son[1];
    int z=T[x].son[1];
    while(T[z].son[0])
        z=T[z].son[0];
    if(!y && !z)
    {
        rt=0;
        return;
    }
    if(!y)
    {
        Splay(z, 0);
        T[z].son[0]=0;
        PushUp(z);
        return;
    }
    if(!z)
    {
        Splay(y, 0);
        T[y].son[1]=0;
        PushUp(y);
        return;
    }
    Splay(y, 0);
    Splay(z, y);
    T[z].son[0]=0;
    PushUp(z);
    PushUp(y);
}

int GetPth(int p) 
{
    if(!rt) return 0;
    int x=rt, ret=0;
    while(x)
    {
        if(p == T[T[x].son[0]].size+1)
            break;
        if(p>T[T[x].son[0]].size+1)
        {
            p-=T[T[x].son[0]].size+1;
            x=T[x].son[1];
        }
        else
            x=T[x].son[0];
    }
    Splay(x, 0);
    return x;
}
int main(void)
{
    int p;
    while(scanf("%d",&p)!=EOF&&p)
    {
        switch (p)
        {
            case 1:{
                int key,num;
                scanf("%d%d",&num,&key);
                Insert(key,num);
            };break;
            case 2:{
                int x=GetPth(T[rt].size);
                if(x)
                {
                    printf("%d\n",T[x].num);
                    Delete(T[x].key);
                }
                else printf("0\n");
            };break;
            case 3:{
                int x=GetPth(1);
                if(x)
                {
                    printf("%d\n",T[x].num);
                    Delete(T[x].key);
                }
                else printf("0\n");
            };break;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值