Pagini recente » Cod sursa (job #2809956) | Cod sursa (job #622946) | Cod sursa (job #2108560) | Cod sursa (job #654415) | Cod sursa (job #995102)
Cod sursa(job #995102)
#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 Erase(int K)
{
int Father=(K>>1);
Heap[K] = Heap[NHeap];
--NHeap;
if ((K > 1) && (Value[Heap[K]] > Value[Heap[Father]]))
Percolate(K);
else
Sift(K);
}
void Solve_Heap()
{
int i,number=0,introduce=1;
for(i=1;i<=N;i++)
{
int type,value;
f>>type;
if(type==1)
{
f>>value;
Heap[++NHeap]=introduce;
Poz[introduce++]=NHeap;
Value[introduce-1]=value;
Percolate(NHeap);
}
if(type==2)
{
f>>value;
Erase(Poz[value]);
}
if(type==3)
g<<Value[Heap[1]]<<"\n";
}
}
int main()
{
f>>N;
Solve_Heap();
return 0;
}