bzoj1455-罗马游戏
可并堆板子题.
代码
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
#define rep(i,l,r) for(register int i=(l);i<=(r);++i)
#define repdo(i,l,r) for(register int i=(l);i>=(r);--i)
#define il inline
typedef double db;
typedef long long ll;
//---------------------------------------
const int nsz=1e6+50;
int n,m;
int val[nsz];
struct tnd{int v,l,r,d,f;}tree[nsz];
void init(){rep(i,1,n)tree[i].v=val[i];}
int merge(int a,int b){//b -> a
if(a==0||b==0)return a+b;
if(tree[a].v>tree[b].v||(tree[a].v==tree[b].v&&a>b))swap(a,b);
tree[a].r=merge(tree[a].r,b);
tree[tree[a].r].f=a;
if(tree[tree[a].l].d<tree[tree[a].r].d)swap(tree[a].l,tree[a].r);
tree[a].d=tree[tree[a].r].d+1;
return a;
}
int getf(int x){
while(tree[x].f)x=tree[x].f;
return x;
}
void del(int x){
val[x]=-1;
tree[tree[x].l].f=tree[tree[x].r].f=0;
merge(tree[x].l,tree[x].r);
}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin>>n;
rep(i,1,n)cin>>val[i];
init();
cin>>m;
int a,b;char s[5];
rep(i,1,m){
cin>>s>>a;
if(s[0]=='M'){
cin>>b;
if(val[a]==-1||val[b]==-1||a==b)continue;
a=getf(a),b=getf(b);
if(a==b)continue;
merge(a,b);
}
else{
if(val[a]==-1)cout<<"0\n";
else a=getf(a),cout<<val[a]<<'\n',del(a);
}
}
return 0;
}