Pagini recente » Cod sursa (job #1916627) | Cod sursa (job #3263531) | Cod sursa (job #1151337) | Cod sursa (job #1197089) | Cod sursa (job #236721)
Cod sursa(job #236721)
#include<stdio.h>
int N,n,i,t,x,v[200011],p[200011],h[200011],nr,aux;
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[t]]=t;
p[h[c]]=c;
c=t;
t=c>>1;
}
}
void down(int x){
int c,t;
t=x;
c=t<<1;
if(c<n && v[h[c]] > v[h[c+1]])
c++;
while(c<=n && v[h[t]] > v[h[c]]){
aux=h[c];
h[c]=h[t];
h[t]=aux;
p[h[t]]=t;
p[h[c]]=c;
t=c;
c=t<<1;
if(c<n && v[h[c]] > v[h[c+1]])
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);
nr++;
v[nr]=x;
n++;
h[n]=nr;
p[nr]=n;
up(n);
}
if(t==2){
fscanf(f,"%d",&x);
h[p[x]]=h[n];
p[h[p[x]]]=p[x];
n--;
down(p[x]);
}
if(t==3){
fprintf(g,"%d\n",v[h[1]]);
}
}
fclose(f);
fclose(g);
return 0;
}