Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17422 | Accepted: 7516 |
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 lowestpriority 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 Kis 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
#include <stdio.h>
#define maxn 1000005
int pre[maxn], key[maxn], id[maxn], ch[maxn][2], rt, tot;
void newNode(int& r, int fa, int v, int i){
r = ++tot;
pre[r] = fa;
key[r] = v;
id[r] = i;
ch[r][0] = ch[r][1] = 0;
}
void rotate(int r, int kind){
int y = pre[r];
ch[y][!kind] = ch[r][kind];
pre[ch[r][kind]] = y;
if(pre[y]){
ch[pre[y]][ch[pre[y]][1] == y] = r;
}
ch[r][kind] = y;
pre[r] = pre[y];
pre[y] = r;
}
void splay(int r, int goal){
while(pre[r] != goal){
if(pre[pre[r]] == goal){
rotate(r, ch[pre[r]][0] == r);
}
else{
int y = pre[r];
int kind = ch[pre[y]][0] == y;
if(ch[pre[r]][!kind] == r){
rotate(y, kind);
rotate(r, kind);
}
else{
rotate(r, !kind);
rotate(r, kind);
}
}
}
if(goal == 0) rt = r;
}
void insert(int x, int val){
int r = rt;
while(1){
if(val < key[r]){
if(ch[r][0]) r = ch[r][0];
else break;
}
else{
if(ch[r][1]) r = ch[r][1];
else break;
}
}
newNode(ch[r][val > key[r]], r, val, x);
splay(ch[r][val > key[r]], 0);
}
void remove(){
if(!rt) return;
int y;
if(ch[rt][1] == 0){
rt = ch[rt][0];
}
else{
y = ch[rt][1];
while(ch[y][0]){
y = ch[y][0];
}
splay(y, rt);
ch[y][0] = ch[rt][0];
pre[ch[rt][0]] = y;
rt = y;
}
pre[rt] = 0;
}
int getmax(){
int r = rt;
if(!r) return 0;
while(ch[r][1]){
r = ch[r][1];
}
splay(r, 0);
return id[r];
}
int getmin(){
int r = rt;
if(!r) return 0;
while(ch[r][0]){
r = ch[r][0];
}
splay(r, 0);
return id[r];
}
int main(){
rt = tot = 0;
int op, x, y;
while(scanf("%d", &op) != EOF){
if(op == 0) return 0;
if(op == 2){
printf("%d\n", getmax());
remove();
}
else if(op == 3){
printf("%d\n", getmin());
remove();
}
else{
scanf("%d %d", &x, &y);
if(rt == 0){
newNode(rt, 0, y, x);
}
else{
insert(x, y);
}
}
}
}
/*
题意:有一个客户等待列表,每个客户都有一个ID和优先权,1e6个客户,每次要么插入一个新客户,
要么拿走一个优先权最大或最小的客户,输出它的ID。
思路:平衡树入门题、模板题。这里用的splay。按优先权维护平衡树,完成插入,查找,删除操作。
*/