Pagini recente » Cod sursa (job #2499713) | Cod sursa (job #2686786) | Cod sursa (job #1510495) | Cod sursa (job #2737599) | Cod sursa (job #720175)
Cod sursa(job #720175)
#include<fstream>
#define MAXN 200010
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int n,a[MAXN],nr,h[MAXN],k,poz[MAXN];
void swap(int x,int y)
{
int aux=h[x];
h[x]=h[y];
h[y]=aux;
}
void upheap(int i)
{
int tata=i/2;
while(i>1 && a[h[tata]]>a[h[i]])
{
swap(tata,i);
poz[h[i]]=i;
poz[h[tata]]=tata;
i=tata;
tata=tata/2;
}
}
void downheap(int i)
{
int sg,dr,max=i;
sg=2*i;
dr=2*i+1;
if(sg<=k && a[h[sg]]<a[h[i]])
max=sg;
if(dr<=k && a[h[dr]]<a[h[max]])
max=dr;
if(max!=i)
{
swap(max,i);
poz[h[max]]=max;
poz[h[i]]=i;
downheap(max);
}
}
void read()
{
in>>n;
for(int i=1;i<=n;i++)
{
int x,y;
in>>x;
if(x==3)
out<<a[h[1]]<<" \n";
if(x==2)
{
in>>y;
a[y]=-1;
upheap(poz[y]);
poz[h[1]]=0;
h[1]=h[k--];
poz[h[1]]=1;
if(k>1) downheap(1);
}
if(x==1)
{
in>>y;
nr++;
k++;
a[nr]=y;
poz[nr]=k;
h[k]=nr;
upheap(k);
}
}
}
int main()
{
read();
return 0;
}