Pagini recente » Cod sursa (job #2846047) | Cod sursa (job #136267) | Cod sursa (job #239474) | Cod sursa (job #2546146) | Cod sursa (job #1094888)
#include<cstdio>
#include<algorithm>
#define NMAX 200001
using namespace std;
int heap[NMAX],n,care[NMAX],nc;
void sift(int k)
{
int son,aux;
do
{
son=0;
if(k*2<=n)
{
son=k*2;
if(k*2+1<=n&&heap[k*2+1]<heap[son])
son=k*2+1;
if(heap[son]>=heap[k])
son=0;
}
if(son)
{
aux=heap[son];
heap[son]=heap[k];
heap[k]=aux;
k=son;
}
}while(son);
}
void percolate(int k)
{
int key=heap[k];
while(k>1&&key<heap[k/2])
{
heap[k]=heap[k/2];
k=k/2;
}
heap[k]=key;
}
int cauta(int x)
{
int k=0,i=1;
while(i<=n&&k==0)
{
if(heap[i]==x)
k=1;
else
i++;
}
return i;
}
void cut(int k)
{
heap[k]=heap[n];
n--;
if(k>1&&heap[k]<heap[k/2])
percolate(k);
else sift(k);
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int tip,nr,n2,c;
n=0; nc=0;
scanf("%d",&n2);
for(int i=1;i<=n2;i++)
{
scanf("%d",&tip);
if(tip==1)
{
scanf("%d",&nr);
care[++nc]=nr;
heap[++n]=nr;
percolate(n);
}
if(tip==2)
{
scanf("%d",&nr);
c=cauta(care[nr]);
cut(c);
}
if(tip==3)
{
printf("%d\n",heap[1]);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}