//http://blog.csdn.net/fp_hzq/article/details/6887058
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
using namespace std;
const int maxn=2000005;
int L[maxn],R[maxn],S[maxn],V[maxn],K[maxn];
int cnt_node,root;
void Right_Rotate(int &t){
int k=L[t];
L[t]=R[k];
R[k]=t;
S[k]=S[t];
S[t]=S[L[t]]+S[R[t]]+1;
t=k;
}
void Left_Rotate(int &t){
int k=R[t];
R[t]=L[k];
L[k]=t;
S[k]=S[t];
S[t]=S[L[t]]+S[R[t]]+1;
t=k;
}
void maintain(int &t,bool flag){
if(flag)
if(S[R[R[t]]]>S[L[t]])
Left_Rotate(t);
else if(S[L[R[t]]]>S[L[t]])
Right_Rotate(R[t]),Left_Rotate(t);
else
return ;
else
if(S[L[L[t]]]>S[R[t]])
Right_Rotate(t);
else if(S[R[L[t]]]>S[R[t]])
Left_Rotate(L[t]),Right_Rotate(t);
else
return ;
maintain(L[t],0);
maintain(R[t],1);
maintain(t,0);
maintain(t,1);
}
void Init(){
cnt_node=root=S[0]=0;
}
void Insert(int &t,int v,int k){
if(t){
S[t]++;
if(v<V[t])Insert(L[t],v,k);
else Insert(R[t],v,k);
maintain(t,v>=V[t]);
}else{
V[t=++cnt_node]=v;
K[t]=k;
S[t]=1;
L[t]=R[t]=0;
}
}
int Find_min(int rt){
int p=rt;
while(L[rt]){
p=rt;rt=L[rt];
}
if(rt!=p)L[p]=R[rt];
else root=R[rt];
return K[rt];
}
int Find_max(int rt){
int p=rt;
while(R[rt]){
p=rt;rt=R[rt];
}
if(rt!=p)R[p]=L[rt];
else root=L[p];
return K[rt];
}
int main(){
int i,a,x,y,ans;
Init();
int tot=0;
while(scanf("%d",&a)!=EOF && a){
if(a==2 ){
if(tot){
ans=Find_max(root);
printf("%d\n",ans);
tot--;
}else{
puts("0");
}
}else if(a==3){
if(tot){
ans=Find_min(root);
printf("%d\n",ans);
tot--;
}else{
puts("0");
}
}else if(a==1){
scanf("%d%d",&x,&y);
tot++;
Insert(root,y,x);
}
}
return 0;
}
************************************************
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
using namespace std;
const int maxn=2000005;
int L[maxn],R[maxn],S[maxn],V[maxn];
int cnt_node,root;
void Right_Rotate(int &t){
int k=L[t];
L[t]=R[k];
R[k]=t;
S[k]=S[t];
S[t]=S[L[t]]+S[R[t]]+1;
t=k;
}
void Left_Rotate(int &t){
int k=R[t];
R[t]=L[k];
L[k]=t;
S[k]=S[t];
S[t]=S[L[t]]+S[R[t]]+1;
t=k;
}
void maintain(int &t,bool flag){
if(flag)
if(S[R[R[t]]]>S[L[t]])
Left_Rotate(t);
else if(S[L[R[t]]]>S[L[t]])
Right_Rotate(R[t]),Left_Rotate(t);
else
return ;
else
if(S[L[L[t]]]>S[R[t]])
Right_Rotate(t);
else if(S[R[L[t]]]>S[R[t]])
Left_Rotate(L[t]),Right_Rotate(t);
else
return ;
maintain(L[t],0);
maintain(R[t],1);
maintain(t,0);
maintain(t,1);
}
void Init(){
cnt_node=root=S[0]=0;
}
void Insert(int &t,int v){
if(t){
S[t]++;
if(v<V[t])Insert(L[t],v);
else Insert(R[t],v);
maintain(t,v>=V[t]);
}else{
V[t=++cnt_node]=v;
S[t]=1;
L[t]=R[t]=0;
}
}
//????????????v????????????
int Delete(int &t ,int v){
--S[t];
if(v==V[t]|| v<V[t] && !L[t] ||v>V[t] && !R[t]){
int tmp=V[t];
if(!L[t] || !R[t])t=L[t]+R[t];
else V[t]=Delete(L[t],V[t]+1);
return tmp;
}
else if(v<V[t])return Delete(L[t],v);
else return Delete(R[t],v);
}
int Find_min(int rt){
while(L[rt])rt=L[rt];
return V[rt];
}
int Find_max(int rt){
while(R[rt])rt=R[rt];
return V[rt];
}
map<int,int>mp;
int main(){
char ch[5];
int k,ans;
int i,j,a,x,y;
Init();
mp.clear();
int tot=0;
while(scanf("%d",&a)!=EOF && a){
if(a==2 ){
if(tot){
ans=Find_max(root);
printf("%d\n",mp[ans]);
Delete(root,ans);
tot--;
}else{
puts("0");
}
}else if(a==3){
if(tot){
ans=Find_min(root);
printf("%d\n",mp[ans]);
Delete(root,ans);
tot--;
}else{
puts("0");
}
}else if(a==1){
scanf("%d%d",&x,&y);
tot++;
mp[y]=x;
Insert(root,y);
}
}
return 0;
}
Double Queue POJ 3481
最新推荐文章于 2023-02-22 11:13:58 发布