分析:
好吧,其实没什么好分析的,左偏树裸题。
Code:
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<iomanip> #include<algorithm> using namespace std; const int N=1e6+7; int n,m; struct Node{ int ls,rs,val; int dist,fa; }t[N]; inline int merge(int x,int y) { if(!x||!y)return x+y; if(t[x].val>t[y].val||(t[x].val==t[y].val&&x>y)) swap(x,y); int &ur=t[x].rs,&ul=t[x].ls; ur=merge(ur,y); t[ur].fa=x; if(t[ur].dist>t[ul].dist)swap(ur,ul); t[x].dist=t[ur].dist+1; return x; } inline void delet(int x) { int ur=t[x].rs,ul=t[x].ls; t[x].val=0;t[ur].fa=0,t[ul].fa=0; merge(ur,ul); } inline int find(int x) {return t[x].fa?find(t[x].fa):x;} int main() { ios::sync_with_stdio(false); cin>>n;t[0].dist=-1; for(int i=1;i<=n;i++) cin>>t[i].val; cin>>m; char opt; int x,y; for(int i=1;i<=m;i++){ cin>>opt; if(opt=='M'){ cin>>x>>y; if(t[x].val*t[y].val==0)continue; x=find(x),y=find(y); if(x!=y) merge(x,y); } else{ cin>>x; if(t[x].val==0)cout<<0<<endl; else{ x=find(x); cout<<t[x].val<<endl; delet(x);} } } return 0; }