Pagini recente » Cod sursa (job #2736873) | Cod sursa (job #1040408) | Cod sursa (job #988350) | Cod sursa (job #2635330) | Cod sursa (job #1816161)
#include <fstream>
//#define fswap(a,b) (swap(Heap[a],Heap[b]) swap(Pos[Heap[a]], Pos[Heap[b]]))
using namespace std;
ifstream in ("heapuri.in");
ofstream out ("heapuri.out");
const int NMax = 200005;
int n,m,nr_heap,Pos[NMax],V[NMax],Heap[NMax];
int opt,aux;
void fswap (int A,int B)
{
swap(Heap[A],Heap[B]);
swap(Pos[Heap[A]], Pos[Heap[B]]);
}
void Upheap(int nod)
{
int tata=nod/2;
if (tata&&V[Heap[nod]]<V[Heap[tata]])
{
fswap(tata,nod);
Upheap(tata);
}
}
void Downheap(int nod)
{
int son;
son=2*nod;
if (son>nr_heap)
return;
else if (son==nr_heap&&V[Heap[nod]]>V[Heap[son]])
{
fswap(nod,son);
}
else if (V[Heap[son]]>V[Heap[son+1]])
{
if (V[Heap[nod]]>V[Heap[son+1]])
{
fswap(nod,son+1);
Downheap(son+1);
}
}
else if (V[Heap[nod]]>V[Heap[son]])
{
fswap(nod,son);
Downheap(son);
}
}
void Solve1()
{
in>>aux;
m++;
V[m]=aux;
nr_heap++;
Heap[nr_heap]=m;
Pos[m]=nr_heap;
Upheap(nr_heap);
}
void Solve2()
{
int position;
in>>aux;
position=Pos[aux];
nr_heap--;
fswap(position,nr_heap);
Downheap(position);
}
void Print()
{
out<<V[Heap[1]]<<'\n';
}
void Read()
{
in>>n;
for (int i=0;i<n;i++)
{
in>>opt;
switch(opt)
{
case 1: {Solve1();break;}
case 2: {Solve2();break;}
case 3: {Print();break;}
}
}
}
int main()
{
Read();
return 0;
}