加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

P3377 【模板】左偏树(可并堆)

发布时间:2020-12-16 09:20:07 所属栏目:百科 来源:网络整理
导读:? ? ? ? ? ? 因为路径压缩的原因 ?pop出根的时候记得要将根的父亲连向合并后新树的根 #includebits/stdc++.h using namespace std; #define rep(i,a,b) for(int i=(a);i=(b);i++) #define repp(i,b) for(int i=(a);i=(b);--i) #define ll long long #define

?

?

?

?

?

  

?

因为路径压缩的原因 ?pop出根的时候记得要将根的父亲连向合并后新树的根

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define see(x) (cerr<<(#x)<<‘=‘<<(x)<<endl)
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
typedef pair<int,int>pii;
//////////////////////////////////
const int N=2e6+10;

int val[N],lson[N],rson[N],f[N],dis[N];

int find1(int x){return f[x]==x?x:f[x]=find1(f[x]);};

int Merge(int x,int y)
{
    if(!x||!y)return x+y;
    if(val[x]>val[y]||val[x]==val[y]&&x>y)swap(x,y);
    rson[x]=Merge(rson[x],y);
    if(dis[lson[x]]<dis[rson[x]])swap(lson[x],rson[x]);
    f[lson[x]]=f[rson[x]]=f[x]=x;
    dis[x]=dis[lson[x]]+1;
    return x;
}
void pop(int x){val[x]=-1;f[lson[x]]=lson[x];f[rson[x]]=rson[x];f[x]=Merge(lson[x],rson[x]);}

int n,m,b,c;
int main()
{
    cin>>n>>m;
    dis[0]=-1;
    rep(i,1,n)scanf("%d",&val[i]),f[i]=i;

    rep(i,1,m)
    {
        cin>>a>>b;
        if(a==1)
        {
            cin>>c;
            if(val[b]==-1||val[c]==-1)continue;
            int x=find1(b),y=find1(c);
            if(x!=y)f[x]=f[y]=Merge(x,y);
        }
        else
        {
            if(val[b]==-1)printf("-1n");
            else printf("%dn",val[find1(b)]),pop(find1(b));
        }
    }
    return 0;
}
View Code

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读