Pagini recente » Cod sursa (job #2162359) | Cod sursa (job #1212462) | Cod sursa (job #149498) | Cod sursa (job #278011) | Cod sursa (job #1519210)
#include <fstream>
#define N 200001
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int h[N], v[N], poz[N], nh, nr, n, x;
void schimb(int p, int q)
{
swap (h[q], h[p]);
poz[h[p]]=p;
poz[h[q]]=q;
}
void coboara(int p)
{
int bun=p, fs=2*p, fd=2*p+1;
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)
{
schimb(bun, p);
coboara(bun);
}
}
void urca(int p)
{
while(p>1 && v[h[p]]<v[h[p/2]])
{
schimb(p, p/2);
p/=2;
}
}
int main()
{
int tip;
f >> n;
for (int i=1; i<=n; ++i)
{
f >> tip;
if(tip==1)
{
f >> v[++nr];
h[++nh] = nr;
poz[nr]=nh;
urca(nh);
}
if(tip==2)
{
f >> x;
int p = poz[x];
schimb(p, nh);
--nh;
urca(p);
coboara(p);
}
if (tip==3)
{
g << v[h[1]] << "\n";
}
}
return 0;
}