Pagini recente » Cod sursa (job #259329) | Cod sursa (job #1119380) | Cod sursa (job #1109671) | Cod sursa (job #1372347) | Cod sursa (job #2315921)
#include <cstdio>
#include <fstream>
using namespace std;
#define nmax 200005
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;
nr = nheap;
while(v[heap[nr/2]] > v[heap[nr]] && nr > 1) {
swap(poz[heap[nr/2]], poz[heap[nr]]);
swap(heap[nr/2],heap[nr]);
nr /= 2;
}
}
void heapout()
{
int fiu;
int x;
fin >> x;
int p = poz[x];
v[heap[poz[x]]] = 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(poz[heap[p]],poz[heap[fiu]]);
swap(heap[p],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;
}