Pagini recente » Cod sursa (job #2128457) | Cod sursa (job #1643362) | Cod sursa (job #414210) | Cod sursa (job #1894819) | Cod sursa (job #1242699)
#include <cstdio>
#include <algorithm>
using namespace std;
struct da
{
int val,poz;
};
da a[200001];
int poz[200001],i,ii,instr,x,n,xx;
void swapp(int n,int t)
{
swap(a[n].val,a[t].val);
swap(a[n].poz,a[t].poz);
swap(poz[a[n].poz],poz[a[t].poz]);
}
void heapup(int n)
{
if (n<=1)return;
if (a[n].val<a[n/2].val)
{
swapp(n,n/2);
heapup(n/2);
}
}
void heapdown(int poz,int n)
{
int st,dr;
if (2*poz<=n)
{
st=a[2*poz].val;
if (2*poz+1<=n)dr=a[2*poz+1].val;
else dr=st+1;
if (st<=dr)
{
if (st<a[poz].val)
{
swapp(2*poz,poz);
heapdown(2*poz,n);
}
}
else
{
if (dr<a[poz].val)
{
swapp(2*poz+1,poz);
heapdown(2*poz+1,n);
}
}
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d ",&ii);
for (i=1; i<=ii; i++)
{
scanf("%d ",&instr);
if (instr==1)
{
scanf("%d",&x);
a[++n].val=x;
a[n].poz=n;
poz[n]=n;
heapup(n);
}
else if (instr==2)
{
scanf("%d ",&x);
xx=poz[x];
swapp(poz[x],n);
n--;
heapdown(xx,n);
}
else if (instr==3)
{
printf("%d\n",a[1].val);
}
}
return 0;
}