Pagini recente » Cod sursa (job #491226) | Cod sursa (job #1677911) | Cod sursa (job #175653) | Cod sursa (job #2197108) | Cod sursa (job #978303)
Cod sursa(job #978303)
#include <iostream>
#include <fstream>
using namespace std;
struct vect{
int val,loc;} v[400001],ax;
int n,nv,i,t,nr,pos[400001],x,i1,np;
int main(void)
{
FILE * f;
f=fopen("heapuri.in","r");
ofstream g("heapuri.out");
fscanf(f,"%d",&n);
v[0].val=-2000000000;
for (i=1;i<=n*2;i++)
v[i].val=2000000000;
nv=0;
for (i=1;i<=n;i++)
{
fscanf(f,"%d",&t);
if (t==3)
g<<v[1].val<<'\n';
if (t==1)
{
fscanf(f,"%d",&nr);
nv++;
np++;
v[nv].val=nr;
v[nv].loc=np;
pos[np]=nv;
x=nv;
while (v[x].val<v[x/2].val)
{
ax=v[x];
v[x]=v[x/2];
v[x/2]=ax;
pos[v[x].loc]=x;
pos[v[x/2].loc]=x/2;
x=x/2;
}
}
if (t==2)
{
fscanf(f,"%d",&nr);
x=pos[nr];
v[x]=v[nv];
v[nv].val=2000000000;
nv--;
pos[v[x].loc]=x;
while (v[x].val<v[x/2].val)
{
ax=v[x];
v[x]=v[x/2];
v[x/2]=ax;
pos[v[x].loc]=x;
pos[v[x/2].loc]=x/2;
x=x/2;
}
while (((v[x].val>v[x*2].val)&&(x*2<=nv))||((v[x].val>v[x*2].val)&&(x*2+1<=nv)))
{
if ((v[x*2].val<v[x*2+1].val)||(x*2+1>nv))
{
ax=v[x];
v[x]=v[x*2];
v[x*2]=ax;
pos[v[x].loc]=x;
pos[v[x*2].loc]=x*2;
x=x*2;
}
else
{
ax=v[x];
v[x]=v[x*2+1];
v[x*2+1]=ax;
pos[v[x].loc]=x;
pos[v[x*2+1].loc]=x*2+1;
x=x*2+1;
}
}
}
}
g.close();
return 0;
}