Pagini recente » Cod sursa (job #2844249) | Cod sursa (job #2171689) | Cod sursa (job #51093) | Cod sursa (job #2044696) | Cod sursa (job #1041325)
#include<cstdio>
#include<cstring>
#include<vector>
#define pb push_back
#define N_MAX 200010
using namespace std;
int H[N_MAX],poz[N_MAX],V[N_MAX];
int i,N,k,x,l;
inline void swap(int x,int y)
{
int a=H[x],b=H[y],aux;
H[y]=a;
H[x]=b;
aux=poz[a];
poz[a]=poz[b];
poz[b]=aux;
return;
}
//Heap-Down, folosit pentru a sterge un element deja existent in heap
inline void Heap_Down(int x,int l_max)
{
int f_st=x*2,f_dr=x*2+1,St,Dr;
if (f_st>l_max) return;
St=V[H[f_st]];
if (f_dr<=l_max) Dr=V[H[f_dr]];
else Dr=St+1;
if (St<Dr)
{
swap(f_st,x);
Heap_Down(f_st,l_max);
}
else
{
swap(f_dr,x);
Heap_Down(f_dr,l_max);
}
}
//Heap-Up, folosit pentru a introduce un nou element in heap
inline void Heap_Up(int x)
{
if (x==1) return;
int t=x/2;
if (V[H[x]]<V[H[t]])
{
swap(x,t);
Heap_Up(t);
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d\n",&N);
l=0;
for(i=1;i<=N;i++)
{
scanf("%d",&k);
if (k==1)
{
scanf("%d",&x);
//INSERAREA ELEMENTULUI
V[++l]=x;
poz[l]=l;
H[l]=l;
Heap_Up(l);
}
if (k==2)
{
scanf("%d",&x);
//STERGEREA ELEMENTULUI
int p=poz[x];
swap(l,p);
V[x]=V[l];
l--;
if (V[H[p]]<V[H[p/2]] && p!=1) Heap_Up(p);
else Heap_Down(p,l);
}
if (k==3)
{
//AFISAREA REZULTULUI
printf("%d\n",V[H[1]]);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}