Pagini recente » Cod sursa (job #1622220) | Cod sursa (job #3256190) | Cod sursa (job #351735) | Cod sursa (job #3251249) | Cod sursa (job #315157)
Cod sursa(job #315157)
#include<algorithm>
using namespace std;
#define DIM 200001
struct heap{
int val,poz;};
int n,m,cnt,a[DIM];
heap h[DIM];
void swap(int x,int y){
heap aux;
a[h[x].poz]=y;
a[h[y].poz]=x;
aux=h[x];
h[x]=h[y];
h[y]=aux;}
void uph(int k){
int aux;
while(k>1){
aux=k>>1;
if(h[k].val<h[aux].val){
a[h[k].poz]=aux;
a[h[aux].poz]=k;
swap(k,aux);}
else
return;}}
void downh(int k){
int aux;
while(k<=cnt){
if(k<<1<=cnt){
aux=k<<1;
if(k+1<=cnt&&h[k+1].val<h[k].val)
++k;}
else
return;
if(h[aux].val<h[k].val){
a[h[aux].poz]=k;
a[h[k].poz]=aux;
swap(k,aux);}
else
return;}}
void insert(int val){
h[++n].val=val;
h[n].poz=cnt;
uph(n);}
void cut(int k){
a[h[n].poz]=k;
h[k]=h[n--];
if(k>1&&h[k].val<h[k>>1].val)
uph(k);
else
downh(k);}
void solve(){
int i,poz,tip,val;
scanf("%d",&m);
for(i=0; i<m; ++i){
scanf("%d",&tip);
if(tip==1){
scanf("%d",&val);
++cnt;
insert(val);}
else if(tip==2){
scanf("%d",&poz);
cut(a[poz]);}
else
printf("%d\n",h[1].val);}}
int main(){
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
solve();
return 0;}