Pku3418 Double Queue

Source: Internet
Author: User

The following operations are supported when such a data structure is required:

1: Insert a node with priority P and number k

2: query the node with the highest priority, output the number, and delete it.

3: query the node with the lowest priority, output the number, and delete it.

Using an SBT can solve the problem perfectly. There is nothing to say. It is useless to say more ~~~

View code

  1 program pku3481(input,output);  2 var  3     left,right,key,s,th:array[0..200000] of longint;  4     tot,root:longint;  5 procedure left_rotate(var t:longint);  6 var  7     k:longint;  8 begin  9     k:=right[t]; 10     right[t]:=left[k]; 11     left[k]:=t; 12     s[k]:=s[t]; 13     s[t]:=s[left[t]]+s[right[t]]+1; 14     t:=k; 15 end;{ left_rotate } 16 procedure right_rotate(var t:longint); 17 var 18     k:longint; 19 begin 20     k:=left[t]; 21     left[t]:=right[k]; 22     right[k]:=t; 23     s[k]:=s[t]; 24     s[t]:=s[left[t]]+s[right[t]]+1; 25     t:=k; 26 end;{ right_rotate } 27 procedure maintain(var t:longint;flag:boolean); 28 begin 29     if not flag then 30     begin 31         if s[left[left[t]]]>s[right[t]] then 32             right_rotate(t) 33         else 34             if s[right[left[t]]]>s[right[t]] then 35             begin 36                 left_rotate(left[t]); 37                 right_rotate(t); 38             end 39             else 40                 exit; 41     end 42     else 43     begin 44         if s[right[right[t]]]>s[left[t]] then 45             left_rotate(t) 46         else 47             if s[left[right[t]]]>s[left[t]] then 48             begin 49                 right_rotate(right[t]); 50                 left_rotate(t); 51             end 52             else 53                 exit; 54     end; 55     maintain(left[t],false); 56     maintain(right[t],true); 57     maintain(t,false); 58     maintain(t,true); 59 end;{ maintain } 60 procedure insert(var now,k,u:longint); 61 begin 62     if now=0 then 63     begin 64         inc(tot); 65         now:=tot; 66         s[now]:=1; 67         left[now]:=0; 68         right[now]:=0; 69         key[now]:=k; 70         th[now]:=u; 71     end 72     else 73     begin 74         inc(s[now]); 75         if k<key[now] then 76             insert(left[now],k,u) 77         else 78             insert(right[now],k,u); 79         maintain(now,k>=key[now]); 80     end; 81 end;{ insert } 82 function delete(var t:longint;k:longint):longint; 83 begin 84     dec(s[t]); 85     if (k=key[t])or((k<key[t])and(left[t]=0))or((k>key[t])and(right[t]=0)) then 86     begin 87         delete:=key[t]; 88         if left[t]*right[t]=0 then 89             t:=left[t]+right[t] 90         else 91             key[t]:=delete(left[t],k+1); 92     end 93     else 94         if k<key[t] then 95             delete:=delete(left[t],k) 96         else 97             delete:=delete(right[t],k); 98 end;{ delete } 99 function get_max_number(t:longint):longint;100 begin101     if t=0 then102         exit(19950714);103     get_max_number:=get_max_number(right[t]);104     if get_max_number=19950714 then105         get_max_number:=t;106 end;{ get_max_number }107 function get_min_number(t:longint):longint;108 begin109     if t=0 then110         exit(19950714);111     get_min_number:=get_min_number(left[t]);112     if get_min_number=19950714 then113         get_min_number:=t;114 end;{ get_min_number }115 procedure main;116 var117     x,y,z:longint;118 begin119     root:=0;120     tot:=0;121     read(x);122     while x<>0 do123     begin124         if x=1 then125         begin126             readln(y,z);127             insert(root,z,y);128         end;129         if x=2 then130             if s[root]=0 then131                 writeln(0)132             else133             begin134                 y:=get_max_number(root);135                 writeln(th[y]);136                 delete(root,key[y]);137             end;138         if x=3 then139             if s[root]=0 then140                 writeln(0)141             else142             begin143                 y:=get_min_number(root);144                 writeln(th[y]);145                 delete(root,key[y]);146             end;147         read(x);148     end;149 end;{ main }150 begin151     main;152 end.                

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.