左偏树

BZOJ1455

#include <cstdio>

  int fa[1000001],a[1000001],n,root[1000001],die[1000001];
  
  struct treenode{
      int lc,rc,po,dis;
  }tr[1000001];

  int getfa(int po){
      int t=po;
      while (fa[t]!=t) t=fa[t];
      
      int root=t;
      while (fa[po]!=po){
        t=fa[po];
        fa[po]=root;
        po=t;
    }
    return(root);
  }
 
  int merge(int x,int y){
    if (x==0) return(y);
    if (y==0) return(x);
    if (a[tr[x].po]>a[tr[y].po]){
      int t=x;x=y;y=t;
    }
    tr[x].rc=merge(tr[x].rc,y);
    if (tr[tr[x].lc].dis<tr[tr[x].rc].dis){
      int t=tr[x].lc;tr[x].lc=tr[x].rc;tr[x].rc=t;
    }    
    tr[x].dis=tr[tr[x].rc].dis+1;
    return(x);
  }
  
  int main(){
      scanf("%d",&n);tr[0].dis=-1;
      for (int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        tr[i].po=i;
      fa[i]=i;root[i]=i;
    }
    int m;
    scanf("%d",&m);
    for (int i=1;i<=m;i++){
      char st[2];int t1,t2;
      scanf("%s",&st);
      if (st[0]=='M'){
          scanf("%d%d",&t1,&t2);
          if (die[t1]||die[t2]) continue;
          t1=getfa(t1),t2=getfa(t2);
          if (t1==t2) continue;
          fa[t1]=t2;
          root[t2]=merge(root[t1],root[t2]);
      }else{
          scanf("%d",&t1);
          if (die[t1]){printf("0\n");continue;}
          t1=getfa(t1);
          printf("%d\n",a[tr[root[t1]].po]);
          die[tr[root[t1]].po]=1;
          root[t1]=merge(tr[root[t1]].lc,tr[root[t1]].rc);
      }
    }
  }

 

posted @ 2016-12-25 10:34  z1j1n1  阅读(136)  评论(0编辑  收藏  举报