Pagini recente » Cod sursa (job #2889180) | Cod sursa (job #901002) | Cod sursa (job #1879164) | Cod sursa (job #395283) | Cod sursa (job #711373)
Cod sursa(job #711373)
#include<cstdio>
#define nmax 200010
int n,l,nr;
int a[nmax],heap[nmax],pos[nmax];
void push(int x){
int aux;
while(x>1 && a[heap[x]]<a[heap[x>>1]])
{
aux = heap[x];
heap[x] = heap[x>>1];
heap[x>>1] = aux;
pos[heap[x]]=x;
pos[heap[x>>1]]=x>>1;
x>>=1;
}
}
void pop(int x)
{
int aux, y=0;
while(x!=y){
y = x;
if(y<<1<=l && a[heap[x]]>a[heap[y<<1]])x=y<<1;
if((y<<1)+1<=l && a[heap[x]]>a[heap[(y<<1)+1]])x=(y<<1)+1;
aux = heap[x];
heap[x] = heap[y];
heap[y] = aux;
pos[heap[x]] = x;
pos[heap[y]] = y;
}
}
int main(void){
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int i,x,y;
scanf("%d\n",&n);
for(i=1;i<=n;++i)
{
scanf("%d ",&x);
if(x==1){
scanf("%d",&y);
l++; nr++;
a[nr] = y;
heap[l] = nr;
pos[nr] = l;
push(l);
}
if(x==2){
scanf("%d",&y);
a[y] = -1;
push(pos[y]);
pos[heap[1]] = 0;
heap[1] = heap[l--];
pos[heap[1]] = 1;
if(l>1)pop(1);
}
if(x==3)printf("%d\n",a[heap[1]]);
}
return 0;
}