Pagini recente » Cod sursa (job #388342) | Cod sursa (job #936073) | Cod sursa (job #1695892) | Cod sursa (job #308477) | Cod sursa (job #2905839)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
const int N=2e5;
int poz[N+1], v[N+1], h[N+1], nh;
void schimb(int p1, int p2)
{
swap(h[p1],h[p2]);
poz[h[p1]]=p1;
poz[h[p2]]=p2;
}
void coboara(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)
{
schimb(p,bun);
coboara(bun);
}
}
void urca(int p)
{
while(p>1 && v[h[p]]<v[h[p/2]])
{
schimb(p,p/2);
p/=2;
}
}
void adauga(int val)
{
h[++nh]=val;
poz[val]=nh;
urca(nh);
}
void sterge(int p)
{
if(p==nh)
{
nh--;
return;
}
schimb(p,nh--);
urca(p);
coboara(p);
}
void afis()
{
g<<v[h[1]]<<"\n";
}
int main()
{
int nr_op, n=0;
f>>nr_op;
for(int i=0;i<nr_op;i++)
{
int tip;
f>>tip;
if(tip==1)
{
f>>v[++n];
adauga(n);
}
else if(tip==2)
{
int p;
f>>p;
sterge(poz[p]);
}
else
{
g<<v[h[1]]<<"\n";
}
}
}