Pagini recente » Cod sursa (job #1568342) | Cod sursa (job #1938716) | Cod sursa (job #1135425) | Cod sursa (job #269295) | Cod sursa (job #2758567)
#include <iostream>
#include <fstream>
using namespace std;
const int dim = 200005;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int heap[dim];
int key[dim];
int poz[dim];
int len = 0;
int cate = 0;
int n,op,x;
void Schimb(int p,int q)
{
swap(heap[p], heap[q]);
poz[heap[p]] = p;
poz[heap[q]] = q;
}
void Urca(int p)
{
while (p > 1 && key[heap[p]] < key[heap[p/2]])
{
Schimb(p, p/2);
p /= 2;
}
}
void Coboara(int p)
{
int bun = 0;
while (p <= len)
{
bun = p;
if (2*p+1 <= len && key[heap[bun]] > key[heap[2*p+1]])
{
bun = 2*p+1;
}
if (2*p <= len && key[heap[bun]] > key[heap[2*p]])
{
bun = 2*p;
}
if (p != bun)
{
Schimb(p, bun);
p = bun;
}
else return ;
}
}
void Insert(int numar)
{
key[++cate] = numar;
heap[++len] = cate;
poz[cate] = len;
Urca(len);
}
void Sterge(int pz)
{
pz = poz[pz];
Schimb(pz, len);
len--;
Urca(pz);
Coboara(pz);
}
void Adauga(int p)
{
heap[++len] = p;
poz[p] = len;
Urca(len);
}
int main()
{
in >> n;
for (int i=1; i<=n; i++)
{
in >> op;
if (op == 1)
{
in >> x;
Insert(x);
}
if (op == 2)
{
in >> x;
Sterge(x);
}
if (op == 3)
{
out << key[heap[1]] << "\n";
}
}
return 0;
}