POJ - 3481 Double Queue —— splay插入删除模板

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
Source

Southeastern Europe 2007
在newnode找到它位置的时候赋值就好了,然后更新一下父节点和子节点的关系

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define keytree ch[ch[root][1]][0]
#define L(x) ch[x][0]
#define R(x) ch[x][1]
#define N 100010
int ch[N][2],pre[N],cnt[N],size[N],key[N];
int tot,root;
int a[N],n,ans[N];
void newnode(int &u,int fa,int KEY,int val)
{
    u=++tot;
    ch[u][0]=ch[u][1]=0;
    pre[u]=fa;cnt[u]=size[u]=1;
    key[u]=KEY,ans[u]=val;
}
void up(int x)
{
     if(x)
     {
          size[x]=cnt[x];
          if(ch[x][0])size[x]+=size[ch[x][0]];
          if(ch[x][1])size[x]+=size[ch[x][1]];
     }
}
void rotate(int u,int kind)//kind表示u在fa的哪一边
{
    int fa=pre[u];
    ch[fa][kind]=ch[u][!kind];
    pre[ch[u][!kind]]=fa;
    if(pre[fa])ch[pre[fa]][ch[pre[fa]][1]==fa]=u;
    pre[u]=pre[fa];
    ch[u][!kind]=fa;
    pre[fa]=u;
    up(fa);up(u);
}
void splay(int u,int goal)
{
    int fa,kind;
    while(pre[u]!=goal)
    {
        if(pre[pre[u]]==goal)
        {
            rotate(u,R(pre[u])==u);
        }
        else
        {
            fa=pre[u];
            kind=R(pre[fa])==fa;
            if(ch[fa][kind]!=u)//不在同一侧
            {
                rotate(u,!kind);
                rotate(u,kind);
            }
            else
            {
                rotate(fa,kind);
                rotate(u,kind);
            }
        }
    }
    up(u);
    if(goal==0)root=u;
}
void insert(int v,int val)
{
    if(root==0)
    {
        newnode(root,0,v,val);
        return;
    }
    int now=root,fa=0;
    while(1)
    {
        if(key[now]==v)
        {
            cnt[now]++;up(now);up(fa);splay(now,0);
            //ans[now]=val;
            break;
        }
        fa=now;
        now=ch[now][key[now]<v];
        if(now==0)
        {
            newnode(ch[fa][key[fa]<v],fa,v,val);
            up(fa);splay(ch[fa][key[fa]<v],0);
            break;
        }
    }
}


int getkth(int u,int k)//第k个键值的点的编号
{
    int s=size[L(u)]+cnt[u];
    if(size[L(u)]<k&&k<=s) return u;
    if(s-cnt[u]>=k) return getkth(L(u),k);
    else return getkth(R(u),k-s);
}
int find(int u,int x)//查找键值为x的点的编号
{
    if(key[u]==x)return u;
    if(key[u]>x)
    {
        if(!L(u))return -1;
        return find(L(u),x);
    }
    if(key[u]<x)
    {
        if(!R(u))return -1;
        return find(R(u),x);
    }
}
int getpre(int u)
{
    if(cnt[u]>=2)return u;
    u=L(u);
    while(R(u))u=R(u);
    return u;
}
int getnext(int u)
{
    if(cnt[u]>=2)return u;
    u=R(u);
    while(L(u))u=L(u);
    return u;
}
void del(int x)//删除编号为x的节点
{
    if(size[root]==1)
    {
        root=0;
        return ;
    }
    if(cnt[x]>1)
    {
        cnt[x]--;
        return ;
    }
    splay(x,0);
    if(L(root))
    {
        if(!R(root))
        {
            pre[L(root)]=0;
            root=L(root);
            return ;
        }
        int p=getpre(x);
        splay(p,root);
        R(p)=R(root);
        pre[R(root)]=p;
        root=p;
        pre[p]=0;
        up(root);
    }
    else
    {
        root=R(root);
        pre[root]=0;
    }
}
//--------------------------------------------------基本操作
int getmin(int x)
{
    while(L(x))
        x=L(x);
    if(x==root)
        root=R(x);
    L(pre[x])=R(x);
    pre[R(x)]=pre[x];
    return ans[x];
}
int getmax(int x)
{
    while(R(x))
        x=R(x);
    if(x==root)
        root=L(x);
    R(pre[x])=L(x);
    pre[L(x)]=pre[x];
    return ans[x];
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        int val,pri;
        if(n==1)
        {
            scanf("%d%d",&val,&pri);
            insert(pri,val);
        }
        else if(n==2)
            printf("%d\n",getmax(root));
        else
            printf("%d\n",getmin(root));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/82021502