Pagini recente » Cod sursa (job #2923290) | Cod sursa (job #3268325) | Cod sursa (job #544810) | Cod sursa (job #1352367) | Cod sursa (job #2502906)
#include <iostream>
#include <fstream>
using namespace std;
ifstream x("heapuri.in");
ofstream y("heapuri.out");
int n,i,nr,h[200202],j,o,nod,pozi[200202],ordine[200202];
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[200202], 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]=k;
}
void sift(int h[200002],int n, int k)
{
int son,key=h[k],poz=ordine[k];
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(son<=n && son && key>h[son])
{
pozi[ordine[son]]=k;
h[k]=h[son];
ordine[k]=ordine[son];
k=son;
}
}while(son);
h[k]=key;
ordine[k]=poz;
pozi[poz]=k;
}
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[pozi[k]]=h[n];
pozi[ordine[n]]=pozi[k];
ordine[pozi[k]]=ordine[n];
n--;
percolate(h,n,pozi[k]);
sift(h,n,pozi[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,nod);
}
else
y<<h[1]<<'\n';
}
}
x.close();
y.close();
return 0;
}