POJ 3481 Double Queue

Double Queue
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6090   Accepted: 2866

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

//第一道SBT题目,

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <queue>
#define N 1000003
using namespace std;
struct node
{
    int l,r,s,k,ta;
    void emp()
    {
        l=r=k=ta=0;
        s=1;
    }
};
node st[N<<1];
int nu;
void rl(int &t)//左转
{
   int k=st[t].r;
   st[t].r=st[k].l;
   st[k].l=t;
   st[k].s=st[t].s;
   st[t].s=st[st[t].l].s+st[st[t].r].s+1;
   t=k;
}
void rr(int &t)//右转
{
    int k=st[t].l;
    st[t].l=st[k].r;
    st[k].r=t;
    st[k].s=st[t].s;
    st[t].s=st[st[t].l].s+st[st[t].r].s+1;
    t=k;
}
void Maintain(int &t,bool flag)//这个是核心了、还不是很理解
{
    if(flag==false)
    {
        if(st[st[st[t].l].l].s>st[st[t].r].s)
            rr(t);
        else if(st[st[st[t].l].r].s>st[st[t].r].s)
                 rl(st[t].l),rr(t);
             else return ;
    }
    else
    {
        if(st[st[st[t].r].r].s>st[st[t].l].s)
              rl(t);
        else if(st[st[st[t].r].l].s>st[st[t].l].s)
             rr(st[t].r),rl(t);
             else return ;
    }
    Maintain(st[t].l,false);
    Maintain(st[t].r,true);
    Maintain(t,false);
    Maintain(t,true);
}
void insert(int &t,int &v,int &ta)//这个不错,插入
{
    if(t==0)
    {    t=++nu;
        st[nu].emp();
        st[nu].k=v;
        st[nu].ta=ta;
        return;
    }
    st[t].s++;
    if(v>st[t].k)
      insert(st[t].r,v,ta);
    else
      insert(st[t].l,v,ta);
    Maintain(t,v>st[t].k);
}
int del(int &t,int v)//这个怎么说呢、精辟呀、膜拜,仰慕
{   if(t==0) {printf("0\n");return 0;}
    st[t].s--;
    if(v==st[t].k||(v>st[t].k&&!st[t].r)||(v<st[t].k&&!st[t].l))
    {
        if(v==st[t].k)
         printf("%d\n",st[t].ta);
        if(!st[t].l||!st[t].r)
        {
            int key=st[t].k;
            t=st[t].l+st[t].r;
            return key;
        }
        else
        {
            return st[t].k=del(st[t].l,v+1);
        }
    }
    else
    {
       if(v>st[t].k) return del(st[t].r,v);
       else return del(st[t].l,v);
    }
}
int getmin(int &t)
{
    if(st[t].l==0)
      return st[t].k;
    return getmin(st[t].l);
}
int getmax(int &t)
{
    if(st[t].r==0)
      return st[t].k;
    return getmax(st[t].r);
}
int main()
{
    int f,k,p,root=0;//这里也是经典
    int t;
    while(scanf("%d",&f),f)
    {
        if(f==1)
        {
           scanf("%d%d",&k,&p);
           insert(root,p,k);
        }else if(f==2)
              {
                  t=getmax(root);
                  del(root,t);
              }
              else
              {
                  t=getmin(root);
                  del(root,t);
              }
    }
    return 0;
}

posted on 2012-07-16 18:26  江财小子  阅读(286)  评论(0编辑  收藏  举报