Pagini recente » Cod sursa (job #603611) | Cod sursa (job #30181) | Cod sursa (job #720605) | Cod sursa (job #2920519) | Cod sursa (job #464130)
Cod sursa(job #464130)
using namespace std;
#include<cstdio>
#define nmax 200005
int h[nmax],a[nmax],b,tip;
int poz[nmax],nr,k,n,j,l;
void schimb (int x, int y)
{
int aux=h[x];
h[x]=h[y];
h[y]=aux;
poz[h[x]]=x;
poz[h[y]]=y;
}
void upheap(int i)
{
int tata=i/2;
while(a[h[tata]]>a[h[i]] && tata)
{schimb(tata,i); i=tata; tata=tata/2;}
}
void downheap(int i)
{
int fiu=2*i;
while(fiu<=nr)
{
if(fiu<nr && a[h[fiu]]>a[h[fiu+1]]) fiu++;
if(a[h[fiu]]<a[h[i]]) {schimb(fiu,i); i=fiu; fiu=fiu*2;}
else fiu=nr+1;
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&n);
for(k=1;k<=n;k++)
{
scanf("%d",&tip);
if(tip==1) {scanf("%d",&b); a[++j]=b;h[++nr]=j; poz[j]=nr;upheap(nr);}
else
if(tip==2) {scanf("%d",&b); l=poz[b];schimb(poz[b],nr); nr--; downheap(l);}
else
if(tip==3) printf("%d\n",a[h[1]]);
}
return 0;
}