Pagini recente » Cod sursa (job #1753022) | Cod sursa (job #1992177) | Cod sursa (job #2540573) | Cod sursa (job #2527125) | Cod sursa (job #1988939)
#include<cstdio>
const int nmax=200005;
int heap[nmax],last,pos[nmax],cnt,a[nmax];
inline void swapum(int a,int b)
{
heap[a]^=heap[b];
heap[b]=heap[a]^heap[b];
heap[a]^=heap[b];
pos[heap[a]]=a;
pos[heap[b]]=b;
}
inline void urca(int x)
{
int temp=x;
while(temp/2&&a[heap[temp/2]]>a[heap[temp]])
{
swapum(temp,temp/2);
temp/=2;
}
}
inline void coboara(int x)
{
int temp=x;
if(2*temp<=last&&a[heap[2*temp]]<a[heap[temp]])
{
swapum(temp,2*temp);
coboara(2*temp);
}
else if(2*temp+1<=last&&a[heap[2*temp+1]]<a[heap[temp]])
{
swapum(temp,2*temp+1);
coboara(2*temp+1);
}
}
inline int push(int number)
{
a[++cnt]=number;
heap[++last]=cnt;
pos[cnt]=last;
urca(last);
}
inline void fetusdeletus(int x)
{
coboara(x);
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int n,i,op,nr;
scanf("%d",&n);
for(i=1;i<=n;++i)
{
scanf("%d",&op);
if(op==3)
printf("%d\n",a[heap[1]]);
else if(op==1)
{
scanf("%d",&nr);
push(nr);
}
else
{
scanf("%d",&nr);
pos[heap[1]]=-1;
pos[heap[last--]]=1;
heap[1]=heap[last];
coboara(1);
}
}
}