POJ - 3481 Double Queue(Splay板子题)

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 serving
K PAdd client K to the waiting list with priority P
2Serve the client with the highest priority and drop him or her from the waiting list
3Serve 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
#include<iostream>
#include<algorithm>
#include<cstring>
#define ls(x) T[x].ch[0]
#define rs(x) T[x].ch[1]
#define fa(x) T[x].fa
#define root T[0].ch[1]
using namespace std;
const int MAXN = 1e5 + 10, mod = 10007, INF = 1e9 + 10;
struct node
{
	int fa, ch[2], val, rec, sum, id;
}T[MAXN];
int tot = 0, pointnum = 0;
void update(int x) { T[x].sum = T[ls(x)].sum + T[rs(x)].sum + T[x].rec; }
int ident(int x) { return T[fa(x)].ch[0] == x ? 0 : 1; }
void connect(int x, int fa, int how) { T[fa].ch[how] = x; T[x].fa = fa; }
void rotate(int x){
	int Y = fa(x), R = fa(Y);
	int Yson = ident(x), Rson = ident(Y);
	connect(T[x].ch[Yson ^ 1], Y, Yson);
	connect(Y, x, Yson ^ 1);
	connect(x, R, Rson);
	update(Y); update(x);
}
void splay(int x, int to){
	to = fa(to);
	while (fa(x) != to){
		int y = fa(x);
		if (T[y].fa == to) rotate(x);
		else if (ident(x) == ident(y)) rotate(y), rotate(x);
		else rotate(x), rotate(x);
	}
}
int newnode(int v, int f,int id){
	T[++tot].fa = f;
	T[tot].rec = T[tot].sum = 1;
	T[tot].val = v;
	T[tot].id = id;
	return tot;
}
void Insert(int x,int id){
	int now = root;
	if (root == 0) { newnode(x, 0, id); root = tot; }//
	else{
		while (1){
			T[now].sum++;
			if (T[now].val == x) { T[now].rec++; splay(now, root); return; }
			int nxt = x < T[now].val ? 0 : 1;
			if (!T[now].ch[nxt]){
				int p = newnode(x, now, id);
				T[now].ch[nxt] = p;
				splay(p, root); return;
			}
			now = T[now].ch[nxt];
		}
	}
}
int find(int x){
	int now = root;
	while (1){
		if (!now) return 0;
		if (T[now].val == x) { splay(now, root); return now; }
		int nxt = x < T[now].val ? 0 : 1;
		now = T[now].ch[nxt];
	}
}
void delet(int x){
	int pos = find(x);
	if (!pos) return;
	if (T[pos].rec > 1) { T[pos].rec--, T[pos].sum--; return; }
	else{
		if (!T[pos].ch[0] && !T[pos].ch[1]) { root = 0; return; }
		else if (!T[pos].ch[0]) { root = T[pos].ch[1]; T[root].fa = 0; return; }
		else{
			int left = T[pos].ch[0];
			while (T[left].ch[1]) left = T[left].ch[1];
			splay(left, T[pos].ch[0]);
			connect(T[pos].ch[1], left, 1);
			connect(left, 0, 1);//
			update(left);
		}
	}
}
int rak(int x){
	int now = root, ans = 0;
	while (1){
		if (T[now].val == x) return ans + T[T[now].ch[0]].sum + 1;
		int nxt = x < T[now].val ? 0 : 1;
		if (nxt == 1) ans = ans + T[T[now].ch[0]].sum + T[now].rec;
		now = T[now].ch[nxt];
	}
}
int kth(int x)//排名为x的数 
{
	int now = root;
	while (1){
		int used = T[now].sum - T[T[now].ch[1]].sum;
		if (T[T[now].ch[0]].sum < x&&x <= used) { splay(now, root); return T[now].val; }
		if (x < used) now = T[now].ch[0];
		else now = T[now].ch[1], x -= used;
	}
}
int lower(int x){
	int now = root, ans = -INF;
	while (now){
		if (T[now].val < x) ans = max(ans, T[now].val);
		int nxt = x <= T[now].val ? 0 : 1;//这里需要特别注意 
		now = T[now].ch[nxt];
	}
	return ans;
}
int upper(int x){
	int now = root, ans = INF;
	while (now){
		if (T[now].val > x) ans = min(ans, T[now].val);
		int nxt = x < T[now].val ? 0 : 1;
		now = T[now].ch[nxt];
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int opt, x, sz = 0;
	while (cin>>opt){
		if (opt == 0)break;
		if (opt == 1) {
			int a, b;
			cin >> a >> b;
			Insert(b, a);
			sz++;
		}
		else if(opt == 2){
			if (sz == 0) {
				cout << "0\n";
			}
			else {
				int pos = find(kth(sz));
				cout << T[pos].id << "\n";
				sz--; delet(T[pos].val);
			}
		}
		else {
			if (sz == 0) {
				cout << "0\n";
			}
			else {
				int pos = find(kth(1));
				cout << T[pos].id << "\n";
				sz--; delet(T[pos].val);
			}
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值