题目链接:模拟堆
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int cnt;
int hp[maxn],ph[maxn],heap[maxn];
void heap_swap(int x,int y)
{
swap(ph[hp[x]],ph[hp[y]]);
swap(hp[x],hp[y]);
swap(heap[x],heap[y]);
}
void up(int u)
{
while(u/2&&heap[u/2]>heap[u])
{
heap_swap(u,u/2);
u>>=1;
}
}
void down(int u)
{
int t=u;
if(2*u<=cnt&&heap[2*u]<heap[t])
{
t=2*u;
}
if(2*u+1<=cnt&&heap[2*u+1]<heap[t])
{
t=2*u+1;
}
if(t!=u)
{
heap_swap(u,t);
down(t);
}
}
int main(void)
{
int n,m=0;
scanf("%d",&n);
while(n--)
{
char op[10];
int k,x;
scanf("%s",op);
if(!strcmp(op,"I"))
{
scanf("%d",&x);
heap[++cnt]=x;
ph[++m]=cnt;
hp[cnt]=m;
up(cnt);
}
else if(!strcmp(op,"PM"))
{
printf("%d\n",heap[1]);
}
else if(!strcmp(op,"DM"))
{
heap_swap(1,cnt);
cnt--;
down(1);
}
else if(!strcmp(op,"D"))
{
scanf("%d",&k);
k=ph[k];
heap_swap(k,cnt);
cnt--;
up(k);down(k);
}
else
{
scanf("%d %d",&k,&x);
k=ph[k];
heap[k]=x;
up(k);down(k);
}
}
return 0;
}