Pagini recente » Cod sursa (job #3040079) | Cod sursa (job #1142) | Cod sursa (job #124479) | Cod sursa (job #1009548) | Cod sursa (job #2893314)
#include <fstream>
using namespace std;
int A[200001],nr,ord[200001],cont;
bool use[200001];
void down(int x)
{
int aux,ok=1,r;
while (ok)
{
ok=0;
if (2*x<=nr) r = 2*x;
if (2*x+1<=nr) if (A[2*x+1]<A[r]) r++;
if (A[r]<A[x]) ok=1;
if (ok)
{
aux = A[r];
A[r] = A[x];
A[x] = aux;
aux = ord[r];
ord[r] = ord[x];
ord[x] = aux;
x=r;
}
}
}
void up(int x)
{
int aux;
while (A[x/2]>A[x] && x!=1)
{
aux = A[x];
A[x] = A[x/2];
A[x/2] = aux;
aux = ord[x/2];
ord[x/2] = ord[x];
ord[x] = aux;
x=x/2;
}
}
void add(int x)
{
nr++;
cont++;
A[nr] = x;
ord[nr] = cont;
up(nr);
}
void del(int x)
{
A[1] = A[nr];
ord[1] = ord[nr];
nr--;
down(1);
}
int main()
{
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n;
fin>>n;
int i,op,nr;
for (i=1; i<=n; i++)
{
fin>>op;
if (op==1) {fin>>nr;
add(nr);}
else if (op==2) {fin>>nr;
use[nr]=1;}
else
{
while (use[ord[1]]) del(1);
fout<<A[1]<<"\n";
}
}
}