Pagini recente » Cod sursa (job #271998) | Cod sursa (job #43122) | Cod sursa (job #545990) | Cod sursa (job #11924) | Cod sursa (job #632757)
Cod sursa(job #632757)
#include <stdio.h>
#define MAX 200001
int h[MAX],a[MAX],p[MAX],n,m;
void swap(int&x,int&y){int z=x;x=y;y=z;}
void push(int x){
int t,f;
n++;m++;
h[n]=x;
p[m]=n;
a[n]=m;
f=n; t=f/2;
while(t!=0&&h[f]<h[t]){
swap(h[t],h[f]);
p[a[t]]=f;
p[a[f]]=t;
swap(a[t],a[f]);
f=t; t=f/2; }
}
void pop(int x){
int t,f;
h[p[x]]=h[n];
p[h[n]]=p[x];
a[p[x]]=a[n];
n--;
t=p[x]; f=t*2; if(f+1<=n&&h[f+1]<h[f])f++;
while(f<=n&&h[t]>h[f]){
swap(h[t],h[f]);
p[a[t]]=f;
p[a[f]]=t;
swap(a[t],a[f]);
t=f; f=t*2; if(f+1<=n&&h[f+1]<h[f])f++;}
}
int main(){
int n,i,o,x;
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&o);
switch(o){
case 1: {scanf("%d",&x); push(x);} break;
case 2: {scanf("%d",&x); pop(x); } break;
case 3: printf("%d\n",h[1]); } }
}