Double Queue POJ 3481

//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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值