Pagini recente » Cod sursa (job #893553) | Cod sursa (job #1610048) | Cod sursa (job #963506) | Istoria paginii runda/simulare-cartita-11/clasament | Cod sursa (job #993456)
Cod sursa(job #993456)
#include <fstream>
#define NMax 100005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int K,H[NMax],A[NMax],Poz[NMax],N,i;
void Swap(int x, int y)
{
swap(H[x],H[y]);
swap(Poz[H[x]],Poz[H[y]]);
}
void Percolate(int x)
{
int TT=x>>1;
if(TT && A[H[x]]<A[H[TT]])
{
Swap(x,TT);
Percolate(TT);
}
}
void Sift(int x)
{
int Son=x<<1;
if(Son+1<=K && A[H[Son+1]]<A[H[Son]])
Son++;
if(Son<=K && A[H[Son]]<A[H[x]])
{
Swap(Son,x);
Sift(Son);
}
}
void Insert(int x)
{
H[++K]=x;
Poz[x]=K;
Percolate(K);
}
void Erase(int x)
{
Swap(x,K);
K--;
Sift(x);
}
int main()
{
fin>>N;
while(N--)
{
int op,x;
fin>>op;
if(op!=3)
fin>>x;
if(op==1)
{
A[++i]=x;
Insert(i);
}
if(op==2)
{
Erase(Poz[x]);
}
if(op==3)
fout<<A[H[1]]<<"\n";
}
return 0;
}