POJ 3481 Double Queue(Splay树)

Double Queue
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:17753   Accepted: 7636

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
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


【思路】
第一次写Splay,在此留念一下。简要地说,Splay是通过每一次对查询或操作元素进行旋转到根部来均摊时间复杂度的数据结构,这是一道练手题,思路很简单,在二叉搜索树中,左查最小,右查最大,那么将查询者转至根部再删除就符合题意了,但是读者可以留意一下手法。

【代码】
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 1e6 + 5;

int root;
int val[MAXN], fa[MAXN], son[MAXN][2]; 

void rotate(int x, int d)
{
	int y = fa[x], z = fa[y];
	son[y][!d] = son[x][d]; if (son[x][d] != 0) fa[son[x][d]] = y;
	if (z != 0) son[z][son[z][1] == y] = x; fa[x] = z;
	son[x][d] = y; fa[y] = x;
	if (root == y) root = x;
}

void splay(int x, int f)
{
	while (fa[x] != f) {
		if (fa[fa[x]] == f)
			if (son[fa[x]][0] == x) rotate(x, 1); else rotate(x, 0);
		else {
			int y = fa[x], z = fa[y];
			if (son[z][0] == y)
				if (son[y][0] == x) rotate(y, 1), rotate(x, 1); else rotate(x, 0), rotate(x, 1);
			else
				if (son[y][1] == x) rotate(y, 0), rotate(x, 0); else rotate(x, 1), rotate(x, 0);
		}
	}
}

void insert(int x, int k)
{
	val[x] = k;
	son[x][0] = son[x][1] = 0;
	if (root == 0) {
		root = x;
		fa[x] = 0;
	}
	else {
		int now = root, f = 0;
		while (now != 0) {
			f = now;
			now = son[now][k > val[now]];
			if (now == 0) {
				son[f][k > val[f]] = x;
				fa[x] = f;
			}
		}
		splay(x, 0);
	}
}

int find_min()
{
	int now = root;
	while (son[now][0] != 0) now = son[now][0];
	splay(now, 0);
	return now;
}

int find_max()
{
	int now = root;
	while (son[now][1] != 0) now = son[now][1];
	splay(now, 0);
	return now;
}

void remove_root()
{
	if (son[root][0] != 0) {
		int now = son[root][0], f = root;
		while (son[now][1] != 0) {
			f = now;
			now = son[now][1]; 
		}
		if (now == son[root][0]) {
			son[now][1] = son[root][1]; if (son[root][1] != 0) fa[son[root][1]] = now;
			root = now; fa[now] = 0;
		}
		else {
			son[f][1] = son[now][0]; fa[son[now][0]] = f;
			son[now][0] = son[root][0]; fa[son[root][0]] = now;
			son[now][1] = son[root][1]; if (son[root][1] != 0) fa[son[root][1]] = now;
			root = now; fa[now] = 0;
		}
	}
	else
	if (son[root][1] != 0) {
		root = son[root][1];
		fa[root] = 0;
	}
	else
		root = 0;
}

int main()
{
	root = 0;
	int op;
	while (scanf("%d", &op) == 1 && op != 0) {
		if (op == 1) {
			int k, p;
			scanf("%d %d", &k, &p);
			insert(k, p);
		}
		if (op == 2) {
			if (root != 0) {
				printf("%d\n", find_max());
				remove_root();
			}
			else
				printf("0\n");
		}
		if (op == 3) {
			if (root != 0) {
				printf("%d\n", find_min());
				remove_root();
			}
			else
				printf("0\n");
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/shili_xu/article/details/78952476