Pagini recente » Rating Marcel Ilie (sanitarium) | Cod sursa (job #1136642) | Cod sursa (job #369711) | Utilizatori inregistrati la FMI No Stress 2012 | Cod sursa (job #236699)
Cod sursa(job #236699)
#include<stdio.h>
int nr,aux,t,i,n,N,x,h[200011],v[200011],p[200011];
void up(int x){
int t,c;
c=x;
t=c>>1;
while(c!=1 && v[h[c]] < v[h[t]]){
aux=h[c];
h[c]=h[t];
h[t]=aux;
p[h[c]]=c;
p[h[t]]=t;
c=t;
t=c>>1;
}
}
void down(int x){
int t,c;
t=x;
c=t<<1;
if(v[c] > v[c+1] && c<N)
c++;
while(c<=N && v[h[t]] > v[h[c]] ){
aux=h[c];
h[c]=h[t];
h[t]=aux;
p[h[c]]=c;
p[h[t]]=t;
t=c;
c=t<<1;
if(v[c] > v[c+1] && c<N)
c++;
}
}
int main(){
FILE *f=fopen("heapuri.in","r");
FILE *g=fopen("heapuri.out","w");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++){
fscanf(f,"%d",&t);
if(t==1){
fscanf(f,"%d",&x);
v[++nr]=x;
h[++N]=nr;
p[nr]=N;
up(N);
}
if(t==2){
fscanf(f,"%d",&x);
aux=h[N];
h[N]=h[p[x]];
h[p[x]]=aux;
N--;
p[h[N]]=N;
p[h[p[x]]]=p[x];
down(p[x]);
}
if(t==3)
fprintf(g,"%d\n",v[h[1]]);
}
fclose(f);
fclose(g);
return 0;
}