Pagini recente » Cod sursa (job #789290) | Cod sursa (job #2782626) | Cod sursa (job #899838) | Cod sursa (job #3264237) | Cod sursa (job #604363)
Cod sursa(job #604363)
#include <iostream>
#define NMax 200005
using namespace std;
int Value[NMax], N, 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 (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 K;
scanf ("%d", &K);
for (; K>0; --K)
{
int Type, X;
scanf ("%d", &Type);
if (Type==1)
{
scanf ("%d", &X);
Value[++N]=X;
Insert (N);
}
if (Type==2)
{
scanf ("%d", &X);
Delete (Poz[X]);
}
if (Type==3)
{
printf ("%d\n", Value[Heap[1]]);
}
}
return 0;
}