Pagini recente » Cod sursa (job #73859) | Cod sursa (job #1477366) | Cod sursa (job #270751) | Cod sursa (job #2065028) | Cod sursa (job #1327545)
#include<iostream>
#include<fstream>
using namespace std;
#define nmax 200002
int nr,lg;
int v[nmax],heap[nmax],poz[nmax];
void up(int st)
{
int a;
while(st/2&&v[heap[st]]<v[heap[st/2]])
{
a=heap[st];
heap[st]=heap[st/2];
heap[st/2]=a;
poz[heap[st/2]]=st/2;
poz[heap[st]]=st;
st=st/2;
}
}
void down(int st)
{
int fiu=-1;
while(st!=fiu)
{
fiu=st;
if(fiu*2<=lg&&v[heap[st]]>v[heap[fiu*2]])
st=fiu*2;
if(fiu*2+1<=lg&&v[heap[st]]>v[heap[fiu*2+1]])
st=fiu*2+1;
int aux=heap[st];
heap[st]=heap[fiu];
heap[fiu]=aux;
poz[heap[st]]=st;
poz[heap[fiu]]=fiu;
}
}
int main()
{
ifstream si;
si.open("heapuri.in");
ofstream so;
so.open("heapuri.out");
int o;
si>>o;
int i,f,x;
for(i=0;i<o;++i)
{
si>>f;
if(f==1)
{
si>>x;
++nr;
++lg;
v[nr]=x;
heap[lg]=nr;
poz[nr]=lg;
up(lg);
}
if(f==2)
{
si>>x;
v[x]=-1;
up(poz[x]);
poz[heap[1]]=0;
heap[1]=heap[lg];
--lg;
poz[heap[1]]=1;
down(1);
}
// for(int j=1;j<=lg;++j)
// cout<<v[heap[j]]<<' ';
// cout<<endl;
if(f==3)
so<<v[heap[1]]<<'\n';
}
}