Pagini recente » Cod sursa (job #478072) | Cod sursa (job #2064576) | Cod sursa (job #757900) | Cod sursa (job #1128315) | Cod sursa (job #2153931)
#include <iostream>
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int nh=0,n,h[200000],poz[200000],v[200000];
void swapp(int p, int q)
{
swap(h[p],h[q]);
poz[h[p]]=p;
poz[h[q]]=q;
}
void urca(int p)
{
while(p>1 && v[h[p]]<v[h[p/2]])
{
swapp(p,p/2);
p=p/2;
}
}
void coborare(int p)
{
int fs=2*p,fd=2*p+1,bun=p;
if(fs<=nh && v[h[fs]]<v[h[bun]])
{
bun=fs;
}
if(fd<=nh && v[h[fd]]<v[h[bun]])
{
bun=fd;
}
if(bun!=p)
{
swapp(p,bun);
coborare(bun);
}
}
void adauga(int val)
{
h[++nh]=val;
poz[val]=nh;
urca(nh);
}
void sterge(int p)
{
swapp(p,nh--);
urca(p);
coborare(p);
}
int main()
{
int x,t=0,p;
f>>n;
for(int i=0;i<n;i++)
{
f>>x;
if(x==1)
{
f>>v[++t];
adauga(t);
}
if(x==2)
{
f>>p;
sterge(poz[p]);
}
if(x==3)
{
g<<v[h[1]]<<"\n";
}
}
return 0;
}