Pagini recente » Cod sursa (job #3156925) | Rating Luca Dantas (LucaDantas) | Cod sursa (job #698353) | Cod sursa (job #1510581) | Cod sursa (job #464090)
Cod sursa(job #464090)
using namespace std;
#include<cstdio>
#define nmax 200005
long long h[nmax],ord[nmax],b;
long long a,poz[nmax],nr,k,n,j;
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(h[tata]>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 && h[fiu]>h[fiu+1]) fiu++;
if(h[fiu]<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("%lli",&n);
for(k=1;k<=n;k++)
{
scanf("%lli",&a);
if(a==1) {scanf("%lli",&b); ord[++j]=b; h[++nr]=b; poz[b]=nr; upheap(nr);}
else
if(a==2) {scanf("%lli",&b); b=poz[ord[b]]; schimb(b,nr); nr--; downheap(b);}
else
if(a==3) printf("%lli\n",h[1]);
}
return 0;
}