Pagini recente » Cod sursa (job #2463462) | Cod sursa (job #2227855) | Cod sursa (job #3037777) | Cod sursa (job #973194) | Cod sursa (job #288281)
Cod sursa(job #288281)
#include<cstdio>
int n,elem,nh;
const int N=200005;
int v[N],h[N],poz[N];
inline void schimb(int &a,int &b)
{
a=a+b;
b=a-b;
a=a-b;
}
void urca(int ind)
{
int tata=ind/2;
if(ind==1||v[h[ind]]>v[h[tata]])return;
poz[h[ind]]=tata;
poz[h[tata]]=ind;
schimb(h[ind],h[tata]);
urca(tata);
}
void coboara(int ind)
{
int min=ind;
int fius=2*ind,fiud=2*ind+1;
if(fius<=nh&&v[h[fius]]<v[h[min]])
min=fius;
if(fiud<=nh&&v[h[fiud]]<v[h[min]])
min=fiud;
if(min==ind)return;
poz[h[ind]]=min;
poz[h[min]]=ind;
schimb(h[ind],h[min]);
coboara(min);
}
void adaug(int x)
{
v[++elem]=x;
h[++nh]=elem;
poz[elem]=nh;
urca(nh);
}
void sterg(int x)
{
poz[h[nh]]=poz[x];
schimb(h[poz[x]],h[nh]);
--nh;
/*
schimb(v[x],v[elem]);
schimb(poz[x],poz[elem]);
nh--;
*/
coboara(poz[x]);
}
void rez()
{
int op,x;
scanf("%d",&n);
for(;n--;)
{
scanf("%d",&op);
if(op<3)
{
scanf("%d",&x);
if(op==1)
adaug(x);
else
sterg(x);
}
else
printf("%d\n",v[h[1]]);
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
rez();
return 0;
}