Pagini recente » Cod sursa (job #1363909) | Cod sursa (job #307433) | Cod sursa (job #1724852) | Cod sursa (job #335060) | Cod sursa (job #2757945)
#include <fstream>
using namespace std;
const int N = 2e5 + 1;
int v[N],nh,h[N],poz[N];
ifstream in("heapuri.in");
ofstream out("heapuri.out");
void schimb(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]])
{
schimb(p,p/2);
p/=2;
}
}
void adauga(int val)
{
h[++nh] = val;
poz[val] = nh;
urca(nh);
}
void coboara(int p)
{
int fs = 2*p;
int fd = 2*p + 1;
int 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(bun,p);
coboara(bun);
}
}
void stergere(int p)
{
if (p == nh)
{
nh--;
}
else
{
h[p] = h[nh--];
poz[h[p]] = p;
urca(p);
coboara(p);
}
}
int main()
{
int nr_op,n = 0;
in >> nr_op;
for(int i = 1; i <= nr_op; i++)
{
int tip;
in >> tip;
if(tip == 1)
{
in >> v[++n];
adauga(n);
}
else if(tip == 2)
{
int x;
in >> x;
stergere(poz[x]);
}
else if(tip == 3)
{
out << v[h[1]] << "\n";
}
}
return 0;
}