Pagini recente » Cod sursa (job #773979) | Cod sursa (job #1729044) | Cod sursa (job #2010728) | Cod sursa (job #1005933) | Cod sursa (job #921574)
Cod sursa(job #921574)
#include<fstream>
#define NM 200001
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int t,h[NM],a[NM],poz[NM],po,i,n,m,x,u;
void sw(int x,int y)
{
int aux;
aux=poz[a[x]];
poz[a[x]]=poz[a[y]];
poz[a[y]]=aux;
aux=h[x];
h[x]=h[y];
h[y]=aux;
aux=a[x];
a[x]=a[y];
a[y]=aux;
}
int mi(int a,int b)
{
if(h[a]>h[b])
return b;
return a;
}
void urca(int k)
{
while(h[k]<h[k>>1]&&k>=2)
{
sw(k,k>>1);
k>>=1; }
}
void coboara(int k)
{
while((k<<1)<=n&&(h[k]>h[(k<<1)]||h[k]>h[(k<<1)+1]))
{
int c=mi(k<<1,(k<<1)+1);
if((k<<1)+1>n)
c=k<<1;
sw(c,k);
k=c;
}
}
int main ()
{
f>>m;
for(i=1;i<=m;++i)
{
f>>t;
if(t==1)
{
f>>x;
++u;
n++;
a[n]=u;
poz[u]=n;
h[n]=x;
urca(n);
}
else
if(t==2)
{
f>>x;
po=poz[x];
sw(poz[x],n);
h[n]=a[n]=poz[x]=0;
n--;
if(po<=n){
urca(po);
coboara(po);}
}
else
g<<h[1]<<"\n";
}
return 0;
}