Pagini recente » Cod sursa (job #266720) | Cod sursa (job #2800569) | Cod sursa (job #991489) | Cod sursa (job #2184927) | Cod sursa (job #1878271)
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define ma 200002
#define buffs 1048576
FILE *f=freopen("heapuri.in","r",stdin);
int v[ma],h[ma],n,i,x,a,lgv,lgh,_i,nod,pos[ma];
char buff[buffs];
int _pos=0;
inline void read(int &nr)
{ int semn=1;
while(buff[_pos]<'0'||buff[_pos]>'9') if(++_pos==buffs) fread(buff,1,buffs,stdin),_pos=0;
nr=0;
if(buff[_pos-1]=='-')semn=-1;
while(buff[_pos]>='0'&&buff[_pos]<='9')
{
nr=nr*10+buff[_pos]-'0';
if(++_pos==buffs)fread(buff,1,buffs,stdin),_pos=0;
}
nr*=semn;
}
inline void sift(int x)
{
int ok,cx;
cx=x;
do
{
ok=0;
if(x*2<=lgh) ok=x*2;
if(x*2+1<=lgh&&v[h[x*2+1]]<v[h[x*2]])ok++;
if(v[h[ok]]>=v[h[x]])ok=0;
if(ok)
{
swap(h[x],h[ok]);
pos[h[x]]=x;
pos[h[ok]]=ok;
// swap(v[cx],v[ok]); schimbi cu cel care e pe poz aia in v
x=ok;
}
}
while(ok);
}
inline void urc(int x)
{ int cx;
cx=x;
while(x&&v[h[x/2]]>v[h[x]])
{
// swap(v[x/2],v[cx]);
swap(h[x/2],h[x]);
pos[h[x/2]]=x/2;
pos[h[x]]=x;
x/=2;
}
}
int main()
{
freopen ("heapuri.out","w",stdout);
fread(buff,1,buffs,stdin);
read(n);
lgv=lgh=0;
for(i=1; i<=n; i++)
{
read(a);
if(a!=3)read(x);
if(a==1)
{
v[++lgv]=x;
h[++lgh]=lgv;
pos[lgv]=lgh;
urc(lgh);
}
else if(a==3) printf("%d \n",v[h[1]]);
else
{
_i=pos[x];
h[_i]=h[lgh];
lgh--;
if(_i>1&&h[_i]<h[_i/2])urc(_i);
else sift(_i);
}
}
}