Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16117 | Accepted: 7137 |
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
题意:有三种操作,1是将一个数赋给p的优先级家入队列,2是以优先级从高到低排序弹出最高的,3是优先级从低到高排弹出最低的
思路:用Treap来维护这个队列。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,size,root,ans;
struct data
{
int l,r,v,size,rnd,w,id;
}tr[100005];
void update(int k)
{
tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+tr[k].w;
}
void rturn(int &k)
{
int t=tr[k].l;tr[k].l=tr[t].r;tr[t].r=k;
tr[t].size=tr[k].size;update(k);k=t;
}
void lturn(int &k)
{
int t=tr[k].r;tr[k].r=tr[t].l;tr[t].l=k;
tr[t].size=tr[k].size;update(k);k=t;
}
void insert(int &k,int x,int id)
{
if(k==0)
{
size++;k=size;
tr[k].size=tr[k].w=1;tr[k].v=x;tr[k].rnd=rand();
tr[k].id=id;
return;
}
tr[k].size++;
if(tr[k].v==x) tr[k].w++;
else if(x>tr[k].v)
{
insert(tr[k].r,x,id);
if(tr[tr[k].r].rnd<tr[k].rnd) lturn(k);
}
else
{
insert(tr[k].l,x,id);
if(tr[tr[k].l].rnd<tr[k].rnd) rturn(k);
}
}
void del(int &k,int x)
{
if(k==0) return;
if(tr[k].v==x)
{
if(tr[k].w>1)
{
tr[k].w--;tr[k].size--;
return;
}
if(tr[k].l*tr[k].r==0) k=tr[k].l+tr[k].r;
else if(tr[tr[k].l].rnd<tr[tr[k].r].rnd)
rturn(k),del(k,x);
else lturn(k),del(k,x);
}
else if(x>tr[k].v)
tr[k].size--,del(tr[k].r,x);
else tr[k].size--,del(tr[k].l,x);
}
int query_id(int k,int x)
{
if(k==0) return 0;
if(x<=tr[tr[k].l].size)
return query_id(tr[k].l,x);
else if(x>tr[tr[k].l].size+tr[k].w)
return query_id(tr[k].r,x-tr[tr[k].l].size-tr[k].w);
else return tr[k].id;
}
int query_v(int k,int x)
{
if(k==0) return 0;
if(x<=tr[tr[k].l].size)
return query_v(tr[k].l,x);
else if(x>tr[tr[k].l].size+tr[k].w)
return query_v(tr[k].r,x-tr[tr[k].l].size-tr[k].w);
else return tr[k].v;
}
int main()
{
int n,cnt=0;
for(int i=0;i<100005;i++)
{
tr[i].l=0;
tr[i].r=0;
}
while(~scanf("%d",&n)&&n)
{
if(n==1)
{
int id,v;
scanf("%d%d",&id,&v);
insert(root,v,id);
cnt++;
}
else if(n==2)
{
if(cnt==0) printf("0\n");
else
{
printf("%d\n",query_id(root,cnt));
del(root,query_v(root,cnt));
cnt--;
}
}
else if(n==3)
{
if(cnt==0) printf("0\n");
else
{
printf("%d\n",query_id(root,1));
del(root,query_v(root,1));
cnt--;
}
}
}
return 0;
}