Pagini recente » Cod sursa (job #1995615) | Cod sursa (job #1559114) | Algoritmiada 2010 - Clasament Runda 1, Clasele 9-10 | Cod sursa (job #3287468) | Cod sursa (job #855800)
Cod sursa(job #855800)
#include <stdio.h>
using namespace std;
int h[200010],n,t,x,i,st,dr,op,k,s;
int p[200010],v[200010];
void heapup(int n)
{
int t,x;
if (n>1)
{
t=n/2;
if (h[t]>h[n])
{
x=h[t]; h[t]=h[n]; h[n]=x;
p[h[t]]=t; p[h[n]]=n;
heapup(t);
}
}
}
void buildh(int n)
{
int i;
for (i=1; i<=n; i++)
{
heapup(i);
}
}
void heapdw(int k, int n)
{
int st,dr,x,i;
if (k>=n) return;
if (2*k<=n) st=h[2*k]; else return;
if (2*k+1<=n) dr=h[2*k+1]; else dr=st+1;
if (st<dr)
{
x=st; h[2*k]=h[k]; h[k]=x;
p[h[2*k]]=2*k; p[h[k]]=k;
i=2*k;
heapdw(i,n);
}
else
{
x=dr; h[2*k+1]=h[k]; h[k]=x;
p[h[2*k+1]]=2*k+1; p[h[k]]=k;
i=2*k+1;
heapdw(i,n);
}
}
void heapsort(int k)
{
while (k>2)
{
x=h[1]; h[1]=h[k]; h[k]=x;
k--;
heapdw(1,k);
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d\n",&s);
n=0;
for (i=1; i<=s; i++)
{
scanf("%d ",&op);
if (op==3) printf("%d\n",h[1]);
else
if (op==1)
{
scanf("%d\n",&v[++n]);
h[n]=v[n]; p[h[n]]=n;
heapup(n);
}
else
{
scanf("%d\n",&x);
k=p[v[x]];
x=h[k]; h[k]=h[n]; h[n]=x;
p[h[k]]=k; p[h[n]]=n;
n--;
heapdw(k,n);
}
}
return 0;
}