POJ3481-Double Queue(SplayTree模板)

版权声明:Dream_dog专属 https://blog.csdn.net/Dog_dream/article/details/83714515

Double Queue

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19029   Accepted: 8149

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

Southeastern Europe 2007


题解:该题就是一道纯模板题 不过菜菜的楼主敲了很多遍才敲过 感慨自己真的是弱到爆


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<map>
using namespace std;
#define clr(a,b)  memset(a,b,sizeof(a))
#define pb(a)     push_back(a)
#define reg       register
#define il        inline
#define rep(a,b,c) for(reg int a=b;a<c;a++)
typedef long long ll;
typedef double db;
const int maxn = 100100;
const int minn = 10000;
struct Splay_Tree
{
	struct Node
	{
		int size, k, p, f, child[2];
	}t[maxn];
	int root,tot;
	il void Init()
	{
		clr(t,0);
		root=tot=0;
		return ;
	}
	il void pushUp(int x)
	{
		if(!x){return ;}
		t[x].size=t[t[x].child[0]].size+t[t[x].child[1]].size+1;
		return ;
	}
	il void newNode(int k,int p,int f)
	{
		t[++tot].f = f;
		t[tot].k = k;
		t[tot].p = p;
		t[tot].child[0] = t[tot].child[1] = 0;
		if(f){t[f].child[p>t[f].p]=tot;}
		pushUp(tot);
		return ;
	}
	il void Rotate(int x,int k)
	{
		int fx=t[x].f;
		t[fx].child[!k]=t[x].child[k];
		if(t[x].child[k])
		{
			t[t[x].child[k]].f=fx;
		}
		t[x].f=t[fx].f;
		if(t[x].f)
		{
			t[t[x].f].child[t[t[x].f].child[1]==fx]=x;
		}
		t[x].child[k]=fx;
		t[fx].f=x;
		pushUp(x);
		pushUp(fx);
		return ;
	}
	il void Splay(int x,int goal)
	{
		while(t[x].f!=goal)
		{
			if(t[t[x].f].f==goal)
			{
				Rotate(x,t[t[x].f].child[0]==x);
			}
			else
			{
				int fx=t[x].f,ffx=t[fx].f;
				int k=(t[ffx].child[0]==fx);
				if(t[fx].child[k]==x)
				{
					Rotate(x,!k);Rotate(x,k);
				}
				else
				{
					Rotate(x,k);Rotate(x,k);
				}
			}
		}
		if(!goal)root=x;
		return;
	}
	il void Insert(int k,int p)
	{
		if(root==0)
		{
			newNode(k,p,root);
			root=tot;
			return ;
		}
		int cur=root;
		while(t[cur].child[p>t[cur].p])
		{
		   cur=t[cur].child[p>t[cur].p];
		}
		newNode(k,p,cur);
		Splay(t[cur].child[p>t[cur].p],0);
		return ;
	}
	il int findMin()
	{
		int cur=root;
		while(t[cur].child[0])
		{
			cur=t[cur].child[0];
		}
		Splay(cur,0);
		return cur;
	}
	il int findMax()
	{
		int cur=root;
		while(t[cur].child[1])
		{
			cur=t[cur].child[1];
		}
		Splay(cur,0);
		return cur;
	}
	il void Remove(int x)
	{
		if(!x)return ;
		int l=t[x].child[0];
		int r=t[x].child[1];
		if(!l&&!r){Init();return;}
		while(t[l].child[1]){l=t[l].child[1];}
		while(t[r].child[0]){r=t[r].child[0];}
		if(!l)
		{
			Splay(r,0);
			t[r].child[0]=0;
			pushUp(r);
		}
		else if(!r)
		{
			Splay(l,0);
			t[l].child[1]=0;
			pushUp(l);
		}
		else
		{
			Splay(l,0);Splay(r,l);
			t[r].child[0]=0;
			pushUp(l);pushUp(r);
		}
		return ;
	}
}st;
int main()
{
	//freopen("data.txt", "r", stdin);
	int op, k, p;
	st.Init();
	while (~scanf("%d", &op)&&op)
	{
		if (op == 1)
		{
			scanf("%d%d", &k, &p);
			st.Insert(k, p);
		}
		else if (op == 2)
		{
			int id=st.findMax();
			printf("%d\n",st.t[id].k);
			st.Remove(id);
		}
		else
		{
			int id=st.findMin();
			printf("%d\n",st.t[id].k);
			st.Remove(id);
		}

	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Dog_dream/article/details/83714515