Pagini recente » Cod sursa (job #19917) | Cod sursa (job #1577487) | Cod sursa (job #1184883) | Cod sursa (job #404742) | Cod sursa (job #922796)
Cod sursa(job #922796)
#include <iostream>
#include<fstream>
using namespace std;
ifstream in ("heapuri.in");
ofstream out("heapuri.out");
typedef int cactus;
# define N 200009
cactus n ,v[N],pozitii[N],lungime,poz,h[N];
inline void schimb (cactus &a, cactus &b)
{
cactus tmp=a;
a=b;
b=tmp;
}
void urca(cactus x)
{
while (x> 1 && v[h[x]]<v[h[x/2]])
{
schimb(h[x],h[x/2]);
pozitii[h[x]]=x;
pozitii[h[x/2]]=x/2;
x/=2;
}
}
void coboara (cactus x)
{
cactus fs= 2*x;
cactus fd=x*2+1;
cactus bun =x;
if(fs<=lungime && v[h[fs]]<v[h[bun]])
bun=fs;
if (fd<=lungime && v[h[fd]]<v[h[bun]])
bun=fd;
if(bun !=x)
{
schimb(h[bun],h[x]);
pozitii [h[bun]]=bun;
pozitii[h[x]]=x;
coboara (bun);
}
}
void sterge(cactus x)
{
h[x]=h[lungime--];
pozitii[h[x]]=x;
if(x>1 && v[h[x]] < v[h[x/2]])
{
urca(x);
}
coboara(x);
}
void proces()
{
in >> n;
cactus tmp,x;
for( cactus i=1;i<=n;i++)
{
in >>tmp;
if (tmp==1)
{
in>>v[++poz];
h[++lungime]=poz;
pozitii[h[lungime]] = lungime;
urca(lungime);
}
else if (tmp==2)
{
in>>x;
sterge(pozitii[x]);
}
else
{
out<<v[h[1]]<<"\n";
}
}
}
int main()
{
proces();
return 0;
}