Pagini recente » Cod sursa (job #3131650) | Cod sursa (job #2698846) | Cod sursa (job #40271) | Cod sursa (job #1333087) | Cod sursa (job #2429883)
#include <iostream>
#include <fstream>
#define next pos*2+1
std::ifstream fin("heapuri.in");
std::ofstream fout("heapuri.out");
int v[200500];
int ind[200500];
int h[800500];
int n;
int sizeH;
void pushH( int pos)
{
if(pos<=0)
return;
if(v[h[pos]]<v[h[(pos-1)/2]])
{
std::swap(h[pos],h[(pos-1)/2]);
ind[h[pos]]=pos;
ind[h[(pos-1)/2]]=(pos-1)/2;
pushH((pos-1)/2);
}
}
void erase(int pos)
{
if(h[next+1]==0&& h[next]==0)
return;
if(next+1< sizeH && v[h[next+1]]<v[h[next]])
{
std::swap(h[pos],h[next+1]);
ind[h[pos]]=pos;
ind[h[next+1]]=next+1;
erase(next+1);
return;
}
else if(next<sizeH)
{
std::swap(h[pos],h[next]);
ind[h[pos]]=pos;
ind[h[next]]=next;
erase(next);
return;
}
else pushH(pos);
}
int main()
{
fin>>n;
int elem=0;
for(int i=1;i<=n;i++)
{
int c,j;
fin>>c;
if(c==1)
{
fin>>j;
elem++;
v[elem]=j;
h[sizeH]=elem;
ind[elem]=sizeH;
pushH(sizeH);
sizeH++;
}
else if(c==2)
{
fin>>j;
int aux= ind[j];
std::swap(h[aux],h[sizeH-1]);
ind[h[aux]]=aux;
ind[h[sizeH-1]]=sizeH-1;
sizeH--;
erase(aux);
}
else
fout<<v[h[0]]<<"\n";
}
}