Pagini recente » Cod sursa (job #2560486) | Cod sursa (job #522684) | Cod sursa (job #2386013) | Cod sursa (job #54299) | Cod sursa (job #1579468)
#include <iostream>
#include <fstream>
#define maxSize 200005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int nr,n;
int h[maxSize], poz[maxSize], ord[maxSize];
void in(int value)
{
++n; ++nr;
h[n]=value;
poz[nr]=n;
ord[n]=nr;
int f=n/2, k=n;
while (k&&h[k]<h[f])
{
swap(h[k],h[f]);
swap(ord[k],ord[f]);
swap(poz[ord[k]],poz[ord[f]]);
k/=2;
f/=2;
}
}
void out(int value)
{
int k=poz[value];
h[k]=h[n];
ord[k]=ord[n];
poz[ord[k]]=k;
--n;
int son;
do {
son=0;
if (2*k<=n&&h[2*k]<h[k]) son=2*k;
if (2*k+1<=n&&h[2*k+1]<h[k]) son=2*k+1;
if (h[son]>=h[k]) son=0;
if (son) {
swap(h[k],h[son]);
swap(ord[k],ord[son]);
swap(poz[ord[k]],poz[ord[son]]);
k=son;
}
} while (son);
}
int main()
{
int tasks,cod,x;
fin>>tasks;
while (tasks--)
{
fin>>cod;
if (cod<3) {
fin>>x;
if (cod==1) {
in(x);
}
if (cod==2) {
out(x);
}
}
else fout<<h[1]<<'\n';
}
return 0;
}