Pagini recente » Cod sursa (job #1771021) | Cod sursa (job #1848566) | Cod sursa (job #1661817) | Cod sursa (job #591271) | Cod sursa (job #632731)
Cod sursa(job #632731)
#include <stdio.h>
int n,h[200001],a[200001],p[200001],k,x;
void swap(int&x,int&y){
int z; z=x; x=y; y=z; }
void insert(int v){
int n,f;
x++; //for a
k++; //for h
h[k]=v;
a[x]=k;
p[k]=x;
f=k; n=f/2;
while(n!=0&&h[f]<h[n]){
swap(h[f],h[n]);
swap(a[p[f]],a[p[n]]);
swap(p[f],p[n]);
f=n; n=f/2; }
}
void remove(int v){
int n,f;
h[a[v]]=h[k];
p[v]=p[k];
a[p[k]]=v;
n=v; f=n*2; if(f+1<=n&&h[f+1]<h[f])f++;
while(f<=n&&h[n]>h[f]){
swap(h[f],h[n]);
swap(a[p[f]],a[p[n]]);
swap(p[f],p[n]);
n=f; f=n*2; if(f+1<=n&&h[f+1]<h[f])f++; }
}
int main(){
int i,o,x;
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&o);
switch(o){
case 1: {scanf("%d",&x); insert(x); } break;
case 2: {scanf("%d",&x); remove(x); } break;
case 3: printf("%d\n",h[1]); } }
}