poj3481(Treap)

来源:互联网 发布:写简历的软件 编辑:程序博客网 时间:2024/05/10 19:40
Double Queue
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13241 Accepted: 5966

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:

0The system needs to stop serving1 K PAdd client K to the waiting list with priority P2Serve the client with the highest priority and drop him or her from the waiting list3Serve 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

21 20 141 30 321 10 993220

Sample Output

02030100
这道题我是拿Treap做的,大致思路想的是区间第K大,然后就用的这个,最近一直在用Treap弄,所以这这么写了
#include <iostream>#include <stdio.h>#include <string.h>#include <time.h>#include <algorithm>const int M=1000000+5;using namespace std;struct node{    int size;    int key;    int rank;    node *son[2];    node (int key):key(key)    {        size=1;        rank=rand();        son[0]=son[1]=NULL;    }    bool operator < (const node &a) const    {        return rank<a.rank;    }    int cmp(int x) const    {        if(key==x) return -1;        return x<key?0:1;    }    void update()    {        size=1;        if(son[0]!=NULL) size+=son[0]->size;        if(son[1]!=NULL) size+=son[1]->size;    }};struct Treap{    inline void rotate(node* &o,int d)    {        node *k=o->son[d^1];        o->son[d^1]=k->son[d];        k->son[d]=o;        o->update();        k->update();        o=k;    }    inline void insert(node* &o,int x)    {        if(o==NULL)            o=new node(x);        else        {            int d=x<o->key?0:1;            insert(o->son[d],x);            o->update();            if(o->rank<o->son[d]->rank)                rotate(o,d^1);        }        o->update();    }    inline void del(node* &o,int x)    {        int d=o->cmp(x);        if(d==-1)        {            node *u=o;            if(o->son[0]!=NULL && o->son[1]!=NULL)            {                int d2=(o->son[0]->rank>o->son[1]->rank?1:0);                rotate(o,d2);                del(o->son[d2],x);            }            else            {                if(o->son[0]==NULL)                    o=o->son[1];                else                    o=o->son[0];                delete u;            }        }        else            del(o->son[d],x);        if(o!=NULL) o->update();    }    inline int kth(node* o,int k)    {        if(o==NULL||k<=0||k>o->size) return 0;        int s=o->son[1]==NULL?0:o->son[1]->size;        if(k==s+1) return o->key;        else if(k<=s) return kth(o->son[1],k);        else return kth(o->son[0],k-s-1);    }} treap;int sum;int query[10000000+5];int main(){    int f,k,id;    sum=0;    node *root=NULL;    while(scanf("%d",&f)&&f)    {        if(f==1)        {            sum++;            scanf("%d%d",&k,&id);            query[id]=k;            treap.insert(root,id);        }        else if(f==2)        {            if(sum==0)                printf("0\n");            else            {                int ans=treap.kth(root,1);                printf("%d\n",query[ans]);                treap.del(root,ans);                sum--;            }        }        else if(f==3)        {            if(sum==0)                printf("0\n");            else            {                int ans=treap.kth(root,sum);                printf("%d\n",query[ans]);                treap.del(root,ans);                sum--;            }        }        //cout<<"sum: "<<sum<<endl;    }    return 0;}


0 0
原创粉丝点击