Pagini recente » Cod sursa (job #1133372) | Rating Stoiean Tudor (tudor123) | Cod sursa (job #2279596) | Cod sursa (job #848841) | Cod sursa (job #2270540)
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int heap[200005],v1[200005],used[200005];
void heapup(int x)
{
if (x>1)
{
if (v1[heap[x]]<v1[heap[x/2]])
{
swap(heap[x],heap[x/2]);
heapup(x/2);
}
}
}
void heapdown(int x,int u)
{
int st,dr;
if (2*x<=u)
{
st=v1[heap[2*x]];
if (2*x+1<=u)
{
dr=v1[heap[2*x+1]];
}
else
{
dr=st+1;
}
if (min(st,dr)<v1[heap[x]])
{
if (st<dr)
{
swap(heap[x],heap[2*x]);
heapdown(2*x,u);
}
else
{
swap(heap[x],heap[2*x+1]);
heapdown(2*x+1,u);
}
}
}
}
int n,q,i,x,nr,poz,j,numar,nr2=0;
int main()
{
f>>n;
q=0;
numar=0;
for (j=1;j<=200000;j++)heap[j]=1000000001;
for (i=1;i<=n;i++)
{
f>>x;
if (x==1)
{
f>>nr;
q++;
numar++;
v1[numar]=nr;
heap[q]=numar;
heapup(q);
}
else
if (x==2)
{
f>>poz;
used[poz]=1;
}
else
{
while (used[heap[1]]==1)
{
swap(heap[1],heap[q]);
q--;
heapdown(1,q);
}
g<<v1[heap[1]]<<'\n';
}
}
return 0;
}