Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11119 | Accepted: 5046 |
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 priorityP. 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 thelowest 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 priorityP 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.插入客人,k为id,p为优先级;2.输出优先级最大的客人id;3.输出优先级最小的客人id
map和SBT都可以做,SBT效率高一些
SBT代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1000100;
struct SBT
{
int left,right,size,key,id;
void init(int val,int idd)
{
left=right=0;
size=1;
key=val;
id=idd;
}
}tree[maxn];
int tot,root;
void left_rotate(int &x)
{
int y=tree[x].right;
tree[x].right=tree[y].left;
tree[y].left=x;
tree[y].size=tree[x].size;
tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;
x=y;
}
void right_rotate(int &x)
{
int y=tree[x].left;
tree[x].left=tree[y].right;
tree[y].right=x;
tree[y].size=tree[x].size;
tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;
x=y;
}
void maintain(int &x,int flag)
{
if(!flag)
{
if(tree[tree[tree[x].left].left].size>tree[tree[x].right].size)
right_rotate(x);
else if(tree[tree[tree[x].left].right].size>tree[tree[x].right].size)
left_rotate(tree[x].left),right_rotate(x);
else return;
}
else
{
if(tree[tree[tree[x].right].right].size>tree[tree[x].left].size)
left_rotate(x);
else if(tree[tree[tree[x].right].left].size>tree[tree[x].left].size)
right_rotate(tree[x].right),left_rotate(x);
else return;
}
maintain(tree[x].left,0);
maintain(tree[x].right,1);
maintain(x,0);
maintain(x,1);
}
//插入值为key的节点
void insert(int &x,int key,int id)
{
if(!x)
{
x=++tot;
tree[x].init(key,id);
}
else
{
tree[x].size++;
if(key<tree[x].key)insert(tree[x].left,key,id);
else insert(tree[x].right,key,id);
maintain(x,key>=tree[x].key);
}
}
int del(int &x,int key)
{
if(!x)return 0;
int res=0;
tree[x].size--;
if(key==tree[x].key||(key<tree[x].key&&tree[x].left==0)||
(key>tree[x].key&&tree[x].right==0))
{
if(tree[x].left&&tree[x].right)
{
int p=del(tree[x].left,key+1);
tree[x].key=tree[p].key;
res=p;
}
else
{
int p=x;
x=tree[x].left+tree[x].right;
res=p;
}
}
else
res=del(key<tree[x].key?tree[x].left:tree[x].right,key);
maintain(x,key<tree[x].key);
return res;
}
int get_max(int x)
{
while(tree[x].right)x=tree[x].right;
return x;
}
int get_min(int x)
{
while(tree[x].left)x=tree[x].left;
return x;
}
int N,K,P;
int main()
{
root=tot=0;
while(scanf("%d",&N)!=EOF,N)
{
if(N==1)
{
scanf("%d%d",&K,&P);
insert(root,P,K);
}
else if(N==2)
{
int x=get_max(root);
printf("%d\n",tree[x].id);
del(root,tree[x].key);
}
else
{
int x=get_min(root);
printf("%d\n",tree[x].id);
del(root,tree[x].key);
}
}
return 0;
}
map:
#pragma warning (disable:4786)
#include<cstdio>
#include<iostream>
#include<map>
using namespace std;
map<int,int> M;
int n,name,number;
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
if(n==1)
{
scanf("%d%d",&name,&number);
M.insert(pair<int,int>(number,name));
}
else if(n==2)
{
if(M.empty())
printf("0\n");
else
{
printf("%d\n",M.rbegin()->second );
M.erase (M.find (M.rbegin()->first ));
}
}
else if(n==3)
{
if(M.empty ())
printf("0\n");
else
{
printf("%d\n",M.begin ()->second );
M.erase (M.begin() );
}
}
}
return 0;
}
treap:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1000100;
int root,tot;
struct Node
{
int ch[2];
int r;//优先级
int v;//值
int s;
int cnt;//自身重复次数
int id;
void init(int val,int iid){v=val;ch[0]=ch[1]=0;s=cnt=1;r=rand();id=iid;}
int cmp(int x)const
{
if(x==v)return -1;
return x<v?0:1;
}
}tree[maxn];
void maintain(int x)
{
tree[x].s=tree[x].cnt;
tree[x].s+=tree[tree[x].ch[0]].s+tree[tree[x].ch[1]].s;
}
void rotate(int &o,int d)
{
int k=tree[o].ch[d^1];
tree[o].ch[d^1]=tree[k].ch[d];
tree[k].ch[d]=o;
maintain(o);
maintain(k);
o=k;
}
void insert(int &o,int x,int val)
{
if(!o)
{
o=++tot;
tree[o].init(x,val);
}
else
{
if(x==tree[o].v)tree[o].cnt++;
else
{
int d=(x<tree[o].v?0:1);
insert(tree[o].ch[d],x,val);
if(tree[tree[o].ch[d]].r>tree[o].r)
rotate(o,d^1);
}
}
maintain(o);
}
void remove(int &o,int x)
{
if(!o)return;
int d=tree[o].cmp(x);
if(d==-1)
{
int u=o;
if(tree[o].ch[0]&&tree[o].ch[1])
{
int d2=(tree[tree[o].ch[0]].r>tree[tree[o].ch[1]].r?1:0);
rotate(o,d2);
remove(tree[o].ch[d2],x);
}
else
{
if(!tree[o].ch[0])o=tree[o].ch[1];
else o=tree[o].ch[0];
}
}
else remove(tree[o].ch[d],x);
if(o)maintain(o);
}
//返回最大值
int get_max(int o)
{
while(tree[o].ch[0])o=tree[o].ch[0];
return o;
}
//返回最小值
int get_min(int o)
{
while(tree[o].ch[1])o=tree[o].ch[1];
return o;
}
//返回val的前驱,如果没有的话返回y
//y的初值可赋成0,表示没有前驱
int get_pred(int o,int val,int y)
{
if(!o)return y;
if(tree[o].v<=val)//注意大于等于号
return get_pred(tree[o].ch[1],val,tree[o].v);
else return get_pred(tree[o].ch[0],val,y);
}
//返回val的后继,如果没有的话返回y
//y的初值可赋成0,表示没有后继
int get_succ(int o,int val,int y)
{
if(!o)return y;
if(tree[o].v>=val)return get_succ(tree[o].ch[0],val,tree[o].v);
else return get_succ(tree[o].ch[1],val,y);
}
//返回第k大的元素的值
int get_kth(int o,int k)
{
if(!o)return 0;
if(k<=tree[tree[o].ch[0]].s)return get_kth(tree[o].ch[0],k);
else if(k>tree[tree[o].ch[0]].s+tree[o].cnt)
return get_kth(tree[o].ch[1],k-tree[tree[o].ch[0]].s-tree[o].cnt);
return tree[o].v;
}
//返回val的排名
int get_rank(int o,int val)
{
if(!o)return 0;
int lsize=tree[tree[o].ch[0]].s;
if(val<tree[o].v)
return get_rank(tree[o].ch[0],val);
else if(val>tree[o].v)
return get_rank(tree[o].ch[1],val)+lsize+tree[o].cnt;
return lsize+tree[o].cnt;
}
int N,K,P;
int main()
{
root=tot=0;
while(scanf("%d",&N)!=EOF,N)
{
if(N==1)
{
scanf("%d%d",&K,&P);
insert(root,P,K);
}
else if(N==2)
{
int x=get_min(root);
printf("%d\n",tree[x].id);
remove(root,tree[x].v);
}
else
{
int x=get_max(root);
printf("%d\n",tree[x].id);
remove(root,tree[x].v);
}
}
return 0;
}