Pagini recente » Cod sursa (job #2800737) | Cod sursa (job #978007) | Cod sursa (job #2656093) | Cod sursa (job #2622671) | Cod sursa (job #2742172)
#include <cstdio>
#include <fstream>
#define nmax 200005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int v[nmax], heap[nmax*2], nheap;
int poz[nmax];
int cnt;
void heapnumber()
{
int nr;
fin >> v[++cnt];
poz[cnt] = ++nheap;
heap[nheap] = cnt;
/// urca
nr = nheap;
while(nr > 1 && v[heap[nr/2]] > v[heap[nr]])
{
swap(heap[nr/2], heap[nr]);
swap(poz[heap[nr/2]], poz[heap[nr]]);
nr /= 2;
}
}
void heapout()
{
int fiu, x;
fin >> x;
int p = poz[x];
/// coboara
v[heap[p]] = 1 << 30;
while(p * 2 <= nheap)
{
fiu = p * 2;
if(p * 2 + 1 <= nheap && v[heap[p * 2 + 1]] < v[heap[fiu]])
fiu = p * 2 + 1;
swap(heap[p], heap[fiu]);
swap(poz[heap[p]], poz[heap[fiu]]);
p *= 2;
}
nheap--;
}
void topheap()
{
fout << v[heap[1]] << "\n";
}
int main()
{
int op, q;
fin >> q;
for(; q; q--)
{
fin >> op;
if (op == 1)
heapnumber();
else if(op == 2)
heapout();
else
topheap();
}
return 0;
}