Pagini recente » Cod sursa (job #205816) | Cod sursa (job #122980) | Cod sursa (job #2470966) | Cod sursa (job #1946937) | Cod sursa (job #402178)
Cod sursa(job #402178)
#include<iostream.h>
#include<fstream.h>
int h[200001],v[200001],ord[200001],c,n,m;
void sift(int k)
{
int min,poz,aux;
while(2*k<=m){
min=v[h[2*k]];
poz=2*k;
if(2*k+1<=m && v[h[2*k+1]]<min){
min=v[h[2*k+1]];
poz=2*k+1;
}
if(v[h[k]]>v[h[poz]]){
aux=h[k];
h[k]=h[poz];
h[poz]=aux;
ord[h[poz]]=poz;
ord[h[k]]=k;
}
else break;
k=poz;
}
}
void percolate(int k)
{
int aux;
while(k>1 && v[h[k]]<v[h[k/2]]){
aux=h[k];
h[k]=h[k/2];
h[k/2]=aux;
ord[h[k]]=k;
ord[h[k/2]]=k/2;
k=k/2;
}
}
void insert(int x)
{
int i;
c++;
m++;
v[c]=x;
ord[c]=m;
h[m]=c;
for(i=n/2;i>=1;i--)sift(i);
}
void del(int k)
{
h[ord[k]]=h[m--];
if(k>1 && v[h[k]]<v[h[k/2]])percolate(ord[k]);
else sift(ord[k]);
}
int main()
{
int i,x,o;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
fin>>n;
for(i=1;i<=n;i++){
fin>>o;
if(o==1){
fin>>x;
insert(x);
}
else if(o==2){
fin>>x;
del(x);
}
else fout<<v[h[1]]<<'\n';
}
return 0;
}