Mai intai trebuie sa te autentifici.
Cod sursa(job #794766)
Utilizator | Data | 6 octombrie 2012 23:11:17 | |
---|---|---|---|
Problema | Heapuri | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.44 kb |
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 200001
int H[maxn],ord[maxn],p[maxn];
inline int MIN(){
return H[1];
}
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 N,int k){
int key=H[k];
while(k>1 && H[k]<H[father(k)]){
H[k]=H[father(k)];
ord[p[H[father(k)]]]=k;
k=father(k);
}
H[k]=key;
ord[p[key]]=k;
}
void sift(int N,int k){
int son=0;
do{
son=0;
if(left_son(k)<=N)
son=left_son(k);
if(right_son(k)<=N && H[right_son(k)]<H[left_son(k)])
son=right_son(k);
if(H[son]>=H[k])
son=0;
if(son){
swap(ord[p[H[k]]],ord[p[H[son]]]);
swap(H[k],H[son]);
k=son;
}
}while(son);
}
void insert(int& N,int key){
H[++N]=key;
percolate(N,N);
}
void cut(int& N,int k){
H[k]=H[N];ord[p[H[N]]]=k;
N--;
if(k>1 && H[k]<H[father(k)]){
percolate(N,k);
}
else{
sift(N,k);
}
}
int main(){
int N=0,a,b,n,nr=0,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",MIN());
else if(a==1){
scanf("%d",&b);
ord[++nr]=N+1;
p[b]=nr;
insert(N,b);
}
else if(a==2){
scanf("%d",&b);
cut(N,ord[b]);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}