Cod sursa(job #1238399)
Utilizator | Data | 6 octombrie 2014 21:21:35 | |
---|---|---|---|
Problema | Heapuri | Scor | 40 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 2.14 kb |
#include <cstdio>
using namespace std;
int a[200001], h[200001], poz[200001], m=0, n=0;
int fiust (int x)
{
return x*2;
}
int fiudr (int x)
{
return x*2+1;
}
int tata (int x)
{
return x/2;
}
void swap (int x, int y)
{
poz[h[x]]=y;
poz[h[y]]=x;
int aux=h[x];
h[x]=h[y];
h[y]=aux;
}
void urcare (int x)
{
if (a[h[x]]<a[h[tata(x)]])
{
swap(x,tata(x));
x=tata(x);
urcare(x);
}
}
void coborare (int x, int p)
{
int i, st=0, dr=0;
if (fiust(x)<=p)
{
st=a[h[fiust(x)]];
if (fiudr(x)<=p) dr=a[h[fiudr(x)]];
else dr=st+1;
if(st<dr) i=fiust(x);
else i=fiust(x)+1;
if(a[h[tata(i)]]>a[h[i]])
{
swap(i,tata(i));
coborare(i,tata(i));
}
}
}
int main()
{
int t, i, p, x;
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&t);
for (i=1; i<=t; i++)
{
scanf("%d",&p);
if (p==1)
{
scanf("%d",&x);
a[++m]=x;
h[++n]=m;
poz[m]=n;
urcare(n);
}
else if (p==2)
{
scanf("%d",&x);
int y=poz[x];
swap(poz[x],n);
n--;
coborare(y,n);
}
else if (p==3)
{
printf("%d\n",a[h[1]]);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}