Pagini recente » Cod sursa (job #1382178) | Cod sursa (job #1267731) | Cod sursa (job #2863480) | Cod sursa (job #976971) | Cod sursa (job #1653294)
#include <cstdio>
#define MAXN 100000
using namespace std;
int v[MAXN+1], heap[MAXN], poz[MAXN+1], n, u;
inline int sonleft(int x){ return 2*x+1;}
inline int sonright(int x){ return 2*x+2;}
inline int father(int x){ return (x-1)/2;}
inline void swap1(int a, int b)
{
int aux=heap[a];
heap[a]=heap[b];
heap[b]=aux;
poz[heap[a]]=a;
poz[heap[b]]=b;
}
inline void up(int p)
{
while(p>0&&v[heap[p]]<v[heap[father(p)]])
{
swap1(p, father(p));
p=father(p);
}
}
inline void down(int p)
{
int q, f=1;
while((f)&&sonleft(p)<n)
{
f=0;
q=sonleft(p);
if(sonright(p)<n&&heap[sonright(p)]<heap[q])
q=sonright(p);
if(v[heap[q]]<v[heap[p]])
{
swap1(p, q);
p=q;
f=1;
}
}
}
inline void insert_heap(int x)
{
v[++u]=x;
heap[n]=u;
poz[u]=n;
n++;
up(n-1);
}
inline void delete_heap(int x)
{
int a=heap[n-1];
heap[poz[x]]=a;
poz[a]=poz[x];
n--;
up(poz[a]);
down(poz[a]);
}
int main()
{
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
int q, i, cer, x;
scanf("%d", &q);
for(i=1;i<=q;i++)
{
scanf("%d", &cer);
if(cer==3)
printf("%d\n", v[heap[0]]);
else
{
scanf("%d", &x);
if(cer==1)
insert_heap(x);
else delete_heap(x);
}
}
return 0;
}