Pagini recente » Cod sursa (job #2300002) | Cod sursa (job #1417188) | Cod sursa (job #2352697) | Cod sursa (job #2130022) | Cod sursa (job #1810363)
#include<fstream>
#define NMax 200005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int N,NHeap,Q;
int Heap[NMax],Poz[NMax],Elem[NMax];
void UpHeap(int Nod)
{
int Father = Nod >> 1;
if(Father > 0 && Elem[Heap[Father]] > Elem[Heap[Nod]])
{
swap( Heap[Father] , Heap[Nod] );
swap( Poz[ Heap[Father] ] , Poz[ Heap[Nod] ] );
UpHeap(Father);
}
}
void DownHeap(int Nod)
{
int Son = Nod << 1;
if(Son + 1 <= NHeap && Elem[Heap[Son+1]] < Elem[Heap[Nod]])
Son++;
if(Son <= NHeap && Elem[Heap[Son]] < Elem[Heap[Nod]])
{
swap( Heap[Son] , Heap[Nod] );
swap( Poz[ Heap[Son] ] , Poz[ Heap[Nod] ] );
DownHeap(Son);
}
}
void Push(int x)
{
Heap[++NHeap] = x;
Poz[x] = NHeap;
UpHeap(NHeap);
}
void Pop(int nr)
{
swap( Heap[nr] , Heap[NHeap] );
swap( Poz[ Heap[nr] ] , Poz[ Heap[NHeap] ] );
NHeap--;
DownHeap(nr);
}
int main()
{
fin>>Q;
while(Q--)
{
int op; fin>>op;
switch(op)
{
case 1:
{
int x; fin>>x;
Elem[++N] = x;
Push(N);
}break;
case 2:
{
int x; fin>>x;
Pop(Poz[x]);
}break;
case 3:
{
fout<<Elem[Heap[1]]<<"\n";
}break;
}
}
fin.close();
fout.close();
return 0;
}