Pagini recente » Cod sursa (job #2406197) | Cod sursa (job #2931469) | Cod sursa (job #282233) | Cod sursa (job #1333318) | Cod sursa (job #2416444)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define NMAX 200005
int T[NMAX],N,x,y,L=0,K=0;
struct Tabel
{
int Value;
int Position;
} Heap[NMAX*5],aux;
void Upheap(int X)
{
int Father=X/2;
if (!Father)
return;
if (Heap[Father].Value<Heap[X].Value)
return;
T[Heap[Father].Position]=X;
T[Heap[X].Position]=Father;
aux=Heap[Father];
Heap[Father]=Heap[X];
Heap[X]=aux;
Upheap(Father);
}
void Downheap(int X)
{
int Son=X*2;
if (Son>L)
return;
if (Son<L)
if (Heap[Son].Value>Heap[Son+1].Value)
Son++;
if (Heap[X].Value>Heap[Son].Value)
T[Heap[Son].Position]=X;
T[Heap[X].Position]=Son;
aux=Heap[Son];
Heap[Son]=Heap[X];
Heap[X]=aux;
Downheap(Son);
}
void Insert(int X)
{
Heap[L].Value=X;
T[K]=L;
Heap[L].Position=K;
Upheap(L);
}
void Pop(int X)
{
X=T[X];
Heap[X]=Heap[L];
L--;
Downheap(X);
Upheap(X);
}
int main()
{
ios_base::sync_with_stdio(false);
fin>>N;
while (N--)
{
fin>>x;
if (x<3)
{
fin>>y;
if (x==1)
{
K++;
L++;
Insert(y);
}
else
Pop(y);
}
else fout<<Heap[1].Value<<"\n";
}
return 0;
}