Pagini recente » Cod sursa (job #792939) | Cod sursa (job #297294) | Cod sursa (job #2779796) | Cod sursa (job #460893) | Cod sursa (job #795015)
Cod sursa(job #795015)
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 200001
int H[maxn],val[maxn],p[maxn],N,hp;
inline int father(int k){
return k/2;
}
inline int left_son(int k){
return 2*k;
}
inline int right_son(int k){
return 2*k+1;
}
void percolate(int k){
while(k>1 && val[H[k]]<val[H[father(k)]]){
swap(p[H[k]],p[H[father(k)]]);
swap(H[k],H[father(k)]);
k=father(k);
}
}
void sift(int k){
int son=0;
do{
son=0;
if(left_son(k)<=N)
son=left_son(k);
if(right_son(k)<=N && val[H[right_son(k)]]<val[H[left_son(k)]])
son=right_son(k);
if(val[H[son]]>=val[H[k]])
son=0;
if(son){
swap(p[H[k]],p[H[son]]);
swap(H[k],H[son]);
k=son;
}
}while(son);
}
void insert(int key){
hp++;N++;
val[N]=key;
H[hp]=N;
p[N]=hp;
percolate(hp);
}
void cut(int k){
H[p[k]]=H[hp];
hp--;
if(k>1 && val[H[k]]<val[H[father(k)]]){
percolate(p[k]);
}
else{
sift(p[k]);
}
}
int main(){
int a,b,n,i;
freopen("heapuri.in","r",stdin);
scanf("%d",&n);
freopen("heapuri.out","w",stdout);
for(i=1;i<=n;i++){
scanf("%d",&a);
if(a==3)
printf("%d\n",val[H[1]]);
else if(a==1){
scanf("%d",&b);
insert(b);
}
else if(a==2){
scanf("%d",&b);
cut(b);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}