Pagini recente » Cod sursa (job #424130) | Cod sursa (job #1820729) | Cod sursa (job #2880934) | Cod sursa (job #1721597) | Cod sursa (job #2498542)
#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=k;
while(k>1 && key<h[father(k)])
{
pozi[k]=pozi[father(k)];
ordine[pozi[k]]=k;
h[k]=h[father(k)];
k=father(k);
}
h[k]=key;
pozi[k]=p;
ordine[pozi[k]]=k;
}
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[k],pozi[son]);
swap(ordine[k],ordine[son]);
swap(h[k],h[son]);
k=son;
}
}while(son);
}
void insert_heap(int nod)
{
h[++n]=nod;
pozi[++j]=n;
percolate(h,n,n);
}
void cut(int h[200002],int &n ,int k)
{
h[k]=h[n];
pozi[k]=pozi[n];
ordine[pozi[k]]=ordine[n];
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;
}