POJ 3481 - Double Queue (SplayTree 템 플 릿)

5103 단어 데이터 구조
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
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 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
#include
#include
#include
#include
#include
#include
#include
#include
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;at[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
",st.t[id].k); st.Remove(id); } else { int id=st.findMin(); printf("%d
",st.t[id].k); st.Remove(id); } } return 0; }

좋은 웹페이지 즐겨찾기