POJ topic 3481 Double Queue (SBT ro map)

Source: Internet
Author: User

Double Queue
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11824 Accepted: 5385

Description

The new founded Balkan Investment Group Bank (Big-bank) opened a new office in Bucharest, equipped with a modern comput ing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank was identified by a positive an integer K and, upon arriving to the bank for some s Ervices, 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 breaks the tradition by sometimes calling the serving desk with the lowest priority Instea D of that and the highest priority. Thus, the system would 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 she from the waiting list
3 Serve the client with the lowest priority and drop him or she 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; The last line contains the Stop-request (code 0). You may assume this when there are 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 was always less than 106, and a priorityP was less than 107. The client may arrive to being served multiple times, and each time could obtain a different priority.

Output

For each request with code 2 or 3, the program have 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 and then the program prints Zero (0) to the output.

Sample Input

21 20 141 30 321 10 993220

Sample Output

02030100

Source

Southeastern Europe 2007

Hangzhou Electric On the original problem, before using map to do, learned the SBT after the POJ with SBT did a bit

The comment section is the deletion of the precursor version, which looks slightly slower.

AC Code

#include <stdio.h> #include <string.h>//#pragma comment (linker, "/stack:102400000,102400000") struct S {in  T Key,left,right,size,num; }tree[1000100];int Top,root,keynum;      void Left_rot (int &x) {int y=tree[x].right;      Tree[x].right=tree[y].left;      Tree[y].left=x;      Tree[y].size=tree[x].size;      tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;  X=y;      } void Right_rot (int &x) {int y=tree[x].left;      Tree[x].left=tree[y].right;      Tree[y].right=x;      Tree[y].size=tree[x].size;      tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;  X=y; }void Maintain (int &x,bool flag)//maintain SBT status {if (Flag==false) {if (tree[tree[tree[x].left].left].siz          E>tree[tree[x].right].size) Right_rot (x); else if (tree[tree[tree[x].left].right].size>tree[tree[x].right].size) {Lef                  T_rot (Tree[x].left);     Right_rot (x);            } else return;          } else {if (tree[tree[tree[x].right].right].size>tree[tree[x].left].size) Left_rot (x);                  else if (tree[tree[tree[x].right].left].size>tree[tree[x].left].size) {                  Right_rot (Tree[x].right);              Left_rot (x);      } else return;      } maintain (Tree[x].left,false);      Maintain (tree[x].right,true);      Maintain (x,true);  Maintain (X,FALSE);          } void Insert (int &x,int key,int num) {if (x==0) {x=++top;          tree[x].left=0;          tree[x].right=0;          tree[x].size=1;    Tree[x].key=key;tree[x].num=num;          } else {tree[x].size++;          if (key<tree[x].key) insert (tree[x].left,key,num);          else insert (tree[x].right,key,num);      Maintain (X,key>=tree[x].key); }} int Getmin (int x) {while (tree[x].left) X=tree[x].left;//keynum=tree[x].key;  return x;    } int Getmax (int x) {while (tree[x].right) X=tree[x].right;keynum=tree[x].key;  return x;      }/*int Remove (int &x,int key) {if (!x) return 0;      int res=0;      tree[x].size--; if (key==tree[x].key| |         (key<tree[x].key&&tree[x].left==0) | |      (key>tree[x].key&&tree[x].right==0))              {if (tree[x].left&&tree[x].right) {int p=remove (tree[x].left,key+1);              Tree[x].key=tree[p].key;          Res=p;              } else {int p=x;              X=tree[x].left+tree[x].right;          Res=p;      }} else Res=remove (Key<tree[x].key?tree[x].left:tree[x].right,key);      Maintain (X,key<tree[x].key);  return res;      } */int Remove (int &x,int key) {tree[x].size--;      if (key>tree[x].key) remove (Tree[x].right,key); ELSE if (Key<tree[x].key) remove (Tree[x].left,key);                  else if (tree[x].left!=0&&tree[x].right==0) {int temp=x;                  X=tree[x].left;              return temp; } else if (!tree[x].left&&tree[x].right!=0) {i                      NT Temp=x;                      X=tree[x].right;                  return temp;                          } else if (!tree[x].left&&!tree[x].right) {                          int temp=x;                          x=0;                      return temp;                          } else {int temp=tree[x].right;                          while (Tree[temp].left) Temp=tree[temp].left;                        Tree[x].key=tree[temp].key;  Remove (Tree[x].right,tree[temp].key); }}int Main () {int n;root=top=0;while (scanf ("%d", &n)!=eof,n) {if (n==1) {int key,num;scanf ("%d%d",&num,& key); Insert (root,key,num);} ElseIf (n==2) {int X=getmax (root);p rintf ("%d\n", Tree[x].num); remove (Root,tree[x].key);} ElseIf (n==3) {int x=getmin (root);p rintf ("%d\n", Tree[x].num); remove (Root,tree[x].key);}}

Map version

AC Code

problem:3481 user:kxh1995 memory:196k time:297ms language:c++ result:accepted Source Code #include <stdio.h> #include <string.h> #include <map> #include <iostream>using namespace Std;map<int,int>m;int    Main () {int n;        while (scanf ("%d", &n)!=eof,n) {int k,p;            if (n==1) {scanf ("%d%d", &k,&p);        M.insert (pair<int,int> (p,k)); } else if (n==2) {if (M.empty ()) {printf ("0\                n ");                    } else {printf ("%d\n", M.rbegin ()->second);                M.erase (M.rbegin ()->first);                    }} else if (n==3) {if (M.empty ())                    {printf ("0\n"); } else {printf ("%d\n", M.BEgin ()->second);                    M.erase (M.begin ()->first); }                }    }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ topic 3481 Double Queue (SBT ro map)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.