Pagini recente » Cod sursa (job #1346287) | Cod sursa (job #1544838) | Cod sursa (job #1523240) | Cod sursa (job #424060) | Cod sursa (job #987771)
Cod sursa(job #987771)
using namespace std;
#include<fstream>
ifstream eu("heapuri.in");
ofstream tu("heapuri.out");
# define Nmax 200005
int Heap[Nmax],Poz[Nmax],N,M,A[Nmax],i;
void Sift(int X)
{
int Son=X<<1;
if(Son+1<=N&&A[Heap[Son+1]]<A[Heap[Son]])
++Son;
if(Son<=N&&A[Heap[Son]]<A[Heap[X]])
{
swap(Heap[X],Heap[Son]);
swap(Poz[Heap[X]],Poz[Heap[Son]]);
Sift(Son);
}
}
void Percolate(int X)
{
int Father=X>>1;
if(Father>0&&A[Heap[X]]<A[Heap[Father]])
{
swap(Heap[X],Heap[Father]);
swap(Poz[Heap[X]],Poz[Heap[Father]]);
Percolate(Father);
}
}
int main()
{
eu>>M;
while(M--)
{
int type;int val;
eu>>type;
if(type!=3)
eu>>val;
if(type==1)
{
A[++i]=val;
Heap[++N]=i;
Poz[i]=N;
Percolate(N);
}
if(type==2)
{
Heap[Poz[val]]=Heap[N];
Poz[Heap[N]]=Poz[val];
--N;
Sift(Poz[val]);
}
if(type==3)
tu<<A[Heap[1]]<<"\n";
}
}