Pagini recente » Cod sursa (job #243691) | Cod sursa (job #1610164) | Cod sursa (job #109066) | Cod sursa (job #159079) | Cod sursa (job #376688)
Cod sursa(job #376688)
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <set>
#define MAXN 200010
using namespace std;
//set<int> h;
long ord[MAXN],N,cnt,h[200000];
int main() {
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&N);
int i,x,c,k,aux;
for (i=0;i<N;++i) {
scanf("%d",&c);
if (c <= 2) scanf("%d",&x);
switch (c) {
case 1: {N++;h[N]=x;k=N;
while(h[k]<h[k/2])
{aux=h[k/2];
h[k/2]=h[k];
h[k]=aux;
k=k/2;
}
ord[cnt]=x;cnt++;break;}
case 2: { for(i=1;i<=N;i++)
if(h[i]==ord[x-1]) {h[i]=h[N];N--;k=i;break;}
if(h[k]>h[2*k]||h[k]>h[2*k+1])
while(h[k]>h[2*k]||h[k]>h[2*k+1])
{if(h[2*k]>h[2*k+1])
{aux=h[2*k];
h[2*k]=h[k];
h[k]=aux;
k=2*k;
}
else {aux=h[2*k+1];
h[2*k+1]=h[k];
h[k]=aux;
k=2*k+1;
}
}
else while(h[k]<h[k/2])
{aux=h[k/2];
h[k/2]=h[k];
h[k]=aux;
k=k/2;
}
break;}
case 3: {printf("%d\n",h[1]);};
}
}
fclose(stdin);
fclose(stdout);
}