Pagini recente » Cod sursa (job #1055437) | Cod sursa (job #2583836) | Cod sursa (job #391947) | Cod sursa (job #705990) | Cod sursa (job #853951)
Cod sursa(job #853951)
#include <cstdio>
#include <cstlib>
#define nmax 200001
#define mmax 100000001
using namespace std;
fopen("heapuri.in","r",stdin);
fopen("heapuri.out","w",stdout);
int heap[nmax],poz[mmax],A[nmax];
int ind=0;
void swap(int &a,int &b)
{
int aux;
aux=a;
a=b;
b=aux;
}
void up_heap(int x)
{
if(x>1)
if (A[heap[x/2]]>A[heap[x]])
{
swap(heap[x/2],heap[x]);
swap(poz[heap[x/2]],poz[heap[x]]);
up_heap(x/2);
}
}
void down_heap(int x)
{
int m;
if (2*x<=ind)
{
m=2*x;
if (m<=ind && A[heap[m+1]]<A[heap[m]])
m++;
if (A[heap[m]]<A[heap[x]])
{
swap(heap[m],heap[x]);
swap(poz[heap[m]],poz[heap[x]]);
down_heap(m);
}
}
}
int main()
{
int n,tip,x;
scanf("%d",&n);
while(n>0)
{
scanf("%d",&tip);
if(tip==1)
{
scanf("%d",&x);
ind++;
heap[ind]=ind;
A[ind]=x;
poz[ind]=ind;
up_heap(ind);
}
else if(tip==2)
{
scanf("%d",&x);
A[x]=mmax;
down_heap(poz[x]);
}
else printf("%d \n",A[heap[1]]);
n--;
}
return 0;