Pagini recente » Cod sursa (job #639375) | Cod sursa (job #1204358) | Cod sursa (job #3257330) | Cod sursa (job #3282281) | Cod sursa (job #2999218)
#include <iostream>
#include <fstream>
using namespace std;
const int NMAX = 2e5 + 1;
int v[NMAX], h[NMAX], poz[NMAX];
void schimb(int p1, int p2)
{
swap(h[p1], h[p2]);
poz[h[p1]] = p1;
poz[h[p2]] = p2;
}
void urca(int h[], int p)
{
while (p > 1 && v[h[p]] < v[h[p/2]])
{
schimb(p, p / 2);
p /= 2;
}
}
void adauga(int h[], int &nh, int val)
{
h[++nh] = val;
poz[val] = nh;
urca(h, nh);
}
void coboara(int h[], int nh, 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(h, nh, bun);
}
}
void sterge(int h[], int &nh, int p)
{
if (p == nh)
{
nh--;
}
else
{
schimb(p, nh--);
urca(h, p);
coboara(h, nh, p);
}
}
int main()
{
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int nrop, nh = 0, nc = 0;
fin >> nrop;
for(int cnt = 0; cnt < nrop; cnt++)
{
int tip;
fin >> tip;
if(tip == 1)
{
fin >> v[++nc];
adauga(h, nh, nc);
}
else if(tip == 2)
{
int p;
fin >> p;
sterge(h, nh, poz[p]);
}
else
fout << v[h[1]] << "\n";
}
return 0;
}