Pagini recente » Istoria paginii arhiva | Cod sursa (job #1497648) | Infoarena Monthly 2012, Runda 11 - Clasament | Cod sursa (job #2990932) | Cod sursa (job #1874239)
#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;
char buff[buffs];
int pos=0;
inline void read(int &nr)
{
while(buff[pos]<'0'||buff[pos]>'9') if(++pos==buffs) fread(buff,1,buffs,stdin),pos=0;
nr=0;
while(buff[pos]>='0'&&buff[pos]<='9')
{
nr=nr*10+buff[pos]-'0';
if(++pos==buffs)fread(buff,1,buffs,stdin),pos=0;
}
}
inline void sift(int x)
{
int ok;
do
{
ok=0;
if(x*2<=lgh) ok=x*2;
if(x*2+1<=lgh&&h[x*2+1]<h[x*2])ok++;
if(h[ok]>=h[x])ok=0;
if(ok)
{
swap(h[x],h[ok]);
x=ok;
}
}
while(ok);
}
inline void urc(int x)
{
while(h[x/2]>h[x])
{
swap(h[x/2],h[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]=x;
urc(lgh);
}
else if(a==3) printf("%d \n",h[1]);
else
{
a=v[x];
for(_i=1; _i<=lgh; _i++)
if(h[_i]==a) break;
h[_i]=h[lgh];
lgh--;
if(_i>1&&h[_i]<h[_i/2])urc(_i);
else sift(_i);
}
}
}