Pagini recente » Cod sursa (job #1181597) | Cod sursa (job #1361888) | Cod sursa (job #2537308) | Cod sursa (job #1176000) | Cod sursa (job #2429881)
#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 a, int pos)
{
if(pos<=0)
return;
if(v[a]<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(a,(pos-1)/2);
}
}
void erase(int pos)
{
/* if(pos>0 && v[h[pos]]<v[h[(pos-1)/2]])
{
pushH(h[pos],pos);
return;
}//*/
// h[pos]=0;
if(h[next+1]==0&& h[next]==0)
return;
for(int i=0;i<2;i++)
if(h[next+i]==0)
{
h[pos]=h[next+(i+1)%2];
ind[h[next+(i+1)%2]]=pos;
erase(next+(i+1)%2);
return;
}
if(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
{
std::swap(h[pos],h[next]);
ind[h[pos]]=pos;
ind[h[next]]=next;
erase(next);
return;
}
}
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(elem,sizeH);
sizeH++;
// std::cout<<i+1<<"\n";
// write();
}
else if(c==2)
{
fin>>j;
// write();
int aux= ind[j];
std::swap(h[aux],h[sizeH-1]);
ind[h[aux]]=aux;
ind[h[sizeH-1]]=sizeH-1;
// std::cout<<aux<<" "<<h[ind[j]]<<" "<<sizeH-1<<"\n";
h[sizeH-1]=0;
sizeH--;
erase(aux);
// std::cout<<i+1<<"\n";
// write();
}
else
fout<<v[h[0]]<<"\n";
}
}