Pagini recente » Cod sursa (job #1133196) | Cod sursa (job #2404733) | Cod sursa (job #153592) | Cod sursa (job #204914) | Cod sursa (job #604482)
Cod sursa(job #604482)
#include <iostream>
#define NMax 200005
using namespace std;
int N, Value[NMax], NHeap, Heap[NMax], Poz[NMax];
inline void Swap (int X, int Y)
{
int Aux;
Aux=Poz[Heap[X]];
Poz[Heap[X]]=Poz[Heap[Y]];
Poz[Heap[Y]]=Aux;
Aux=Heap[X];
Heap[X]=Heap[Y];
Heap[Y]=Aux;
}
void Percolate (int X)
{
int Father=(X>>1);
if (Father>0 and Value[Heap[X]]<Value[Heap[Father]])
{
Swap (X, Father);
Percolate (Father);
}
}
void Sift (int X)
{
int Son=(X<<1);
if (Son+1<=NHeap and Value[Heap[Son+1]]<Value[Heap[Son]])
{
++Son;
}
if (Son<=NHeap and Value[Heap[Son]]<Value[Heap[X]])
{
Swap (X, Son);
Sift (Son);
}
}
void Insert (int X)
{
Heap[++NHeap]=X;
Poz[X]=NHeap;
Percolate (Poz[X]);
}
void Delete (int X)
{
Swap (X, NHeap);
Poz[Heap[NHeap]]=0;
Heap[NHeap]=0;
--NHeap;
Sift (X);
}
int main()
{
freopen ("heapuri.in", "r", stdin);
freopen ("heapuri.out", "w", stdout);
int M;
scanf ("%d", &M);
for (; M>0; --M)
{
int Type;
scanf ("%d", &Type);
if (Type==1)
{
++N;
scanf ("%d", &Value[N]);
Insert (N);
}
if (Type==2)
{
int X;
scanf ("%d", &X);
Delete (Poz[X]);
}
if (Type==3)
{
printf ("%d\n", Value[Heap[1]]);
}
}
return 0;
}