Pagini recente » Cod sursa (job #1537133) | Cod sursa (job #2288092) | Cod sursa (job #3231487) | Cod sursa (job #2115687) | Cod sursa (job #779709)
Cod sursa(job #779709)
#include <stdio.h>
#include <algorithm>
#define NMax 200010
using namespace std;
const char IN[]="heapuri.in",OUT[]="heapuri.out";
int N,L,Len;
bool b[NMax];
int H[NMax] , v[NMax];
inline bool cmp(int x,int y){
return v[x]<v[y];
}
void remake(int x){
int l=2*x,r=l+1,Min=x;
if (l<=L && cmp(H[l],H[Min])) Min=l;
if (r<=L && cmp(H[r],H[Min])) Min=r;
if (Min!=x)
{
swap(H[Min],H[x]);
remake(Min);
}
}
void pop(){
swap(H[1],H[L]);--L;
remake(1);
}
void push(int x){
H[++L]=x;
for (int i=L;i>1 && cmp(H[i],H[i>>1]);i>>=1)
swap(H[i],H[i>>1]);
}
int main()
{
int c,x;
freopen(IN,"r",stdin);
scanf("%d",&N);
freopen(OUT,"w",stdout);
while (N--)
{
scanf("%d",&c);
while (L>0 && !b[H[1]]) pop();
if (c==1){
scanf("%d",&v[++Len]);
push(Len);b[Len]=true;
}
else if (c==2) {
scanf("%d",&x);
b[x]=false;
}
else{
printf("%d\n",v[H[1]]);
}
}
fclose(stdout);fclose(stdin);
return 0;
}