Pagini recente » Cod sursa (job #106811) | Cod sursa (job #28767) | Cod sursa (job #2492981) | Cod sursa (job #481423) | Cod sursa (job #2498956)
#include <iostream>
#include <fstream>
using namespace std;
ifstream x("heapuri.in");
ofstream y("heapuri.out");
int n,i,nr,h[200002],j,o,nod,pozi[200002],ordine[200002];
int father(int nod)
{
return nod/2;
}
int left_son(int nod)
{
return nod*2;
}
int right_son(int nod)
{
return nod*2+1;
}
void percolate(int h[200002], int n, int k)
{
int key=h[k],p=ordine[k];
while(k>1 && key<h[father(k)])
{
pozi[ordine[father(k)]]=k;
h[k]=h[father(k)];
ordine[k]=ordine[father(k)];
k=father(k);
}
h[k]=key;
ordine[k]=p;
pozi[p]=key;
}
void sift(int h[200002],int n, int k)
{
int son;
do
{
son=0;
if(left_son(k)<=n)
{
son=left_son(k);
if(right_son(k)<=n && h[right_son(k)]<h[left_son(k)])
son=right_son(k);
if(h[son]>=h[k])
son=0;
}
if(son)
{
swap(pozi[ordine[son]],pozi[ordine[k]]);
swap(h[k],h[son]);
swap(ordine[k],ordine[son]);
k=son;
}
}while(son);
}
void insert_heap(int nod)
{
h[++n]=nod;
j++;
pozi[j]=n;
ordine[n]=j;
percolate(h,n,n);
}
void cut(int h[200002],int &n ,int k)
{
h[k]=h[n];
pozi[k]=pozi[n];
ordine[pozi[k]]=k;
n--;
if(k>1 && h[k]<h[father(k)])
percolate(h,n,k);
else
sift(h,n,k);
}
int main()
{
x>>nr;
for(i=1;i<=nr;i++)
{
x>>o;
if(o==1)
{
x>>nod;
insert_heap(nod);
}
else
{
if(o==2)
{
x>>nod;
cut(h,n,ordine[nod]);
}
else
y<<h[1]<<'\n';
}
}
x.close();
y.close();
return 0;
}