其他分享
首页 > 其他分享> > 左偏树【待施工】

左偏树【待施工】

作者:互联网

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int fa[N],ls[N],rs[N],dist[N],val[N],id[N];
bool del[N];
int n,m,cnt;

int get(int x)
{
  if(x == fa[x])return x;
  return fa[x] = get(fa[x]);
}

struct leftist
{
  int id,val;
  bool operator<(leftist x)const{return val == x.val?id<x.id:val < x.val;}
}t[N];




int mer(int x,int y)
{
  if(!x||!y)return x|y;
  if(t[y]<t[x])swap(x,y);
  rs[x] = mer(rs[x],y);
  if(dist[ls[x]] < dist[rs[x]])swap(ls[x],rs[x]);
  dist[x] = dist[rs[x]] + 1; 
  return x;
}





int main()
{
  
  cin >> n >> m;
  dist[0] = -1;
  for(int i = 1 ; i <= n ; i ++ )
  {
    scanf("%d",&t[i].val);
    fa[i] = i;
    t[i].id = i;
  }

  for(int i = 1 ; i <= m ; i ++ )
  {
    int x,y,op;
    scanf("%d%d",&op,&x);
    if(op == 1)
    {
      scanf("%d",&y);
      if(del[x]||del[y])continue;
      x = get(x),y = get(y);
      if(x != y)fa[x] = fa[y] = mer(x,y);
    }
    else {
      if(del[x]){puts("-1");continue;}
      x = get(x);
      printf("%d\n",t[x].val);
      del[x] = 1;
      fa[ls[x]] = fa[rs[x]] = fa[x] = mer(ls[x],rs[x]);
      ls[x] = rs[x] = dist[x] = 0;
    }
  }
}

标签:dist,val,int,施工,fa,bool,id,左偏
来源: https://www.cnblogs.com/ErFu/p/16512399.html