【洛谷3377】 左偏樹(可並堆)
阿新 • • 發佈:2019-01-14
前言
其實我是不小心翻線性基的時候看見的。
Solution
左偏樹只會模板,挖坑待補
程式碼實現
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<algorithm> #include<queue> #include<set> #include<map> #include<iostream> using namespace std; #define ll long long #define re register #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout) inline int gi(){ int f=1,sum=0;char ch=getchar(); while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();} return f*sum; } const int N=100010; int f[N],dis[N],ch[N][2],v[N],n,m; int find(int x){ while(f[x])x=f[x]; return x; } int merge(int x,int y){ if(!x || !y)return x+y; if(v[x]>v[y] || (v[x]==v[y] && x>y))swap(x,y); ch[x][1]=merge(ch[x][1],y); f[ch[x][1]]=x; if(dis[ch[x][0]]<dis[ch[x][1]])swap(ch[x][0],ch[x][1]); dis[x]=dis[ch[x][1]]+1; return x; } int main(){ n=gi();m=gi();dis[0]=-1; for(int i=1;i<=n;i++)v[i]=gi(); while(m--){ int opt=gi(); if(opt==1){ int x=gi(),y=gi(); if(v[x]==-1 || v[y]==-1)continue; if(find(x)==find(y))continue; merge(find(x),find(y)); } else{ int x=gi(),F=find(x); printf("%d\n",v[F]); if(v[F]==-1)continue;v[F]=-1; f[ch[F][0]]=f[ch[F][1]]=0; merge(ch[F][0],ch[F][1]); } } return 0; }