Pagini recente » Cod sursa (job #1856782) | Cod sursa (job #772328) | Cod sursa (job #3262877) | Cod sursa (job #2036672) | Cod sursa (job #788734)
Cod sursa(job #788734)
#include <cstdio>
#include <algorithm>
using namespace std;
#define Max 200002
struct heap{ int x,n; }h[Max];
int k,n,pos[Max];
void add(int x)
{
int t,f;
n++; k++;
h[k].x=x;
h[k].n=n;
pos[n]=k;
f=k; t=f/2;
while(t>0 && h[f].x<h[t].x)
{
swap(h[t],h[f]);
swap(pos[h[t].n],pos[h[f].n]);
f=t; t=f/2;
}
}
void rem(int x)
{
int t,f;
t=pos[x];
h[t]=h[k--];
pos[h[t].n]=t;
f=2*t;
if(f+1<=k && h[f+1].x<h[f].x)f++;
while(f<=k && h[f].x<h[t].x)
{
swap(h[t],h[f]);
swap(pos[h[t].n],pos[h[f].n]);
t=f; f=2*t;
if(f+1<=k && h[f+1].x<h[f].x)f++;
}
}
int main()
{
int m,c,x;
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&m);
while(m--)
{
scanf("%d",&c);
switch(c)
{
case 1: scanf("%d",&x); add(x); break;
case 2: scanf("%d",&x); rem(x); break;
case 3: printf("%d\n",h[1].x); break;
}
}
return 0;
}