839.模拟堆
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/d9b5a2e3fc454ea1abec2b6500a26f0b.png)
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int n,m=0;
int h[N],ph[N],hp[N],cnt;
void heap_swap(int a,int b){
swap(ph[hp[a]],ph[hp[b]]);
swap(hp[a],hp[b]);
swap(h[a],h[b]);
}
void down(int u){
int t=u;
if(u*2<=cnt and h[u*2]<h[t])t=u*2;
if(u*2+1<=cnt and h[u*2+1]<h[t])t=u*2+1;
if(u!=t){
heap_swap(u,t);
down(t);
}
}
void up(int u){
while(u/2 and h[u/2]>h[u]){
heap_swap(u/2,u);
u/=2;
}
}
int main(){
cin>>n;
while(n--){
string op;
int k,x;
cin>>op;
if(op=="I"){
cin>>x;
cnt++,m++;
ph[m]=cnt;
hp[cnt]=m;
h[cnt]=x;
up(cnt);
}else if(op=="PM"){
cout<<h[1]<<endl;
}else if(op=="DM"){
heap_swap(1,cnt);
cnt--;
down(1);
}else if(op=="D"){
cin>>k;
k=ph[k];
heap_swap(k,cnt);
cnt--;
down(k);
up(k);
}else{
cin>>k>>x;
k=ph[k];
h[k]=x;
down(k);
up(k);
}
}
return 0;
}