Pagini recente » Cod sursa (job #2399492) | Cod sursa (job #2388982) | Cod sursa (job #56371) | Cod sursa (job #1357859) | Cod sursa (job #3000160)
#include <fstream>
using namespace std;
string file = "heapuri";
ifstream cin(file +".in");
ofstream cout(file +".out");
int h[200001], nh, v[200001], n, poz[200001];
// h = heap pe pozitii
// v[i] = elementul al i-lea
// poz[i] = pozitia elementului al i-lea in heap
void schimba(int x, int y)
{
swap(poz[h[x]],poz[h[y]]);
swap(h[x],h[y]);
}
void urca(int p)
{
while (p>1 && v[h[p/2]] > v[h[p]])
{
schimba (p,p/2);
p /= 2;
}
}
void coboara(int p)
{
mor:
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)
{
schimba(bun,p);
p = bun;
goto mor;
}
}
void adauga(int val)
{
h[++nh] = val;
poz[val] = nh;
int p = nh;
urca(p);
}
void sterge (int p)
{
h[p] = h[nh--];
urca(p);
coboara(p);
}
int main()
{
int nop, x, op;
cin >> nop;
for (int i=1; i<=nop; i++)
{
cin >> op;
if (op == 1)
{
cin >> v[++n];
adauga(n);
}
else if (op == 2)
{
cin >> x;
sterge(poz[x]);
}
else
{
cout << v[h[1]] << '\n';
}
}
}