Pagini recente » Cod sursa (job #486002) | Cod sursa (job #2980632) | Cod sursa (job #1637264) | Cod sursa (job #1334772) | Cod sursa (job #631385)
Cod sursa(job #631385)
#include<stdio.h>
#include<vector>
#include<algorithm>
#define Nmax 200005
using namespace std;
int Poz,x,N,i,j,M,type,nr;
vector<int> H(Nmax),poz(Nmax),a(Nmax);
void heap_up(int nod)
{
while ( (nod>>1) && (H[nod>>1]>H[nod]) )
{
swap(H[nod],H[nod>>1]);
swap(poz[H[nod]],poz[H[nod>>1]]);
nod>>=1;
}
}
void heap_down(int nod)
{
while (nod<<1<=N)
{
if ( ((nod<<1)+1<=N) && (H[(nod<<1)+1]<H[nod<<1]) ) Poz=(nod<<1)+1;
else Poz=nod<<1;
if (H[Poz]>H[nod]) return;
swap(H[nod],H[Poz]);
swap(poz[nod],poz[Poz]);
nod=Poz;
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&M);
for (i=1;i<=M;++i)
{
scanf("%d",&type);
if (type==1)
{
scanf("%d",&x);
H[++N]=x;
a[++nr]=x;
poz[x]=N;
heap_up(N);
}
else if (type==2)
{
scanf("%d",&x);
swap(H[poz[a[x]]],H[N--]);
heap_down(poz[a[x]]);
}
else if (type==3)
printf("%d\n",H[1]);
}
}