Pagini recente » Cod sursa (job #924656) | Istoria paginii runda/simulare_de_oni_2 | Cod sursa (job #605813) | Cod sursa (job #1897463) | Cod sursa (job #995107)
Cod sursa(job #995107)
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int N;
int Heap[200002],Value[200002];
int NHeap,Poz[200002];
bool Use[200002];
void Swap(int X, int Y)
{
swap(Heap[X],Heap[Y]);
swap(Poz[Heap[X]],Poz[Heap[Y]]);
}
void Percolate (int X)
{
int Father=(X>>1);
if (Father>0 && Value[Heap[X]]<Value[Heap[Father]])
{
Swap (X,Father);
Percolate (Father);
}
}
void Sift(int X)
{
int Son=(X<<1);
if(Son+1<=NHeap && Value[Heap[Son+1]]<Value[Heap[Son]])
Son++;
if(Son<=NHeap && Value[Heap[Son]]<Value[Heap[X]])
{
Swap(Son,X);
Sift(Son);
}
}
void Insert(int X)
{
Heap[++NHeap]=X;
Poz[X]=NHeap;
Percolate(NHeap);
}
void Erase(int K)
{
Swap(K,NHeap);
--NHeap;
Sift(K);
}
void Solve_Heap()
{
int i,number=0,introduce=0;
for(i=1;i<=N;i++)
{
int type,value;
f>>type;
if(type==1)
{
f>>value;
Value[++introduce]=value;
Insert(introduce);
}
if(type==2)
{
f>>value;
Erase(Poz[value]);
}
if(type==3)
g<<Value[Heap[1]]<<"\n";
}
}
int main()
{
f>>N;
Solve_Heap();
return 0;
}