Pagini recente » Cod sursa (job #1623387) | Cod sursa (job #1617504) | Cod sursa (job #1258686) | Cod sursa (job #1639980) | Cod sursa (job #1815532)
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int h[200001],hh[200001];
int ind[200001],inc=1;
void adauga (int x,int elem){
int p,c,aux;
h[elem]=x;
hh[elem]=elem;
ind[elem]=elem;
c=elem;
p=elem/2;
while (p>=0){
if (h[c]<h[p]){
aux=h[c];
h[c]=h[p];
h[p]=aux;
aux=hh[p];
hh[p]=hh[c];
hh[c]=aux;
ind[hh[p]]=p;
ind[hh[c]]=c;
}
else break;
c=p;
p/=2;
}
}
void sterge (int poz,int elem){
int p,c,aux;
c=poz;
p=poz/2;
h[poz]=h[elem];
hh[poz]=hh[elem];
ind[hh[poz]]=ind[hh[elem]];
while (h[c]<h[p] && p>=0){
aux=h[c];
h[c]=h[p];
h[p]=aux;
aux=hh[p];
hh[p]=hh[c];
hh[c]=aux;
ind[hh[p]]=p;
ind[hh[c]]=c;
c=p;
p/=2;
}
p=poz;
c=2*poz;
while (c<=elem){
if (c+1<=elem && h[c+1]<h[c])
c++;
aux=h[c];
h[c]=h[p];
h[p]=aux;
aux=hh[p];
hh[p]=hh[c];
hh[c]=aux;
ind[hh[p]]=p;
ind[hh[c]]=c;
p=c;
c*=2;
}
}
int main()
{
FILE *fin=fopen ("heapuri.in","r");
FILE *fout=fopen ("heapuri.out","w");
int n,elem,i,cer,x,poz;
fscanf (fin,"%d",&n);
elem=0;
for (i=1;i<=n;i++){
fscanf (fin,"%d",&cer);
//printf ("%d ",cer);
if (cer==3)
fprintf (fout,"%d\n",h[inc]);
// ind= pozitia in hh a elem inserat al i-lea
// in h tii minte elem dupa valori
// hh[i]= al catelea elem inserat a fost h[i]
else if (cer==2){
fscanf (fin,"%d",&x);
poz=ind[x];
sterge (poz,elem);
elem--;
}
else {
elem++;
fscanf (fin,"%d",&x);
adauga (x,elem);
}
}
return 0;
}