Pagini recente » Cod sursa (job #2466965) | Cod sursa (job #1587015) | Cod sursa (job #3261806) | Cod sursa (job #2143069) | Cod sursa (job #1653003)
#include <fstream>
#define N 200010
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n,v,x,lg,Q[N],i;
typedef int Heap[N];
Heap H;
int Father(int nod){
return nod/2;
}
int LeftSon(int nod){
return nod*2;
}
int RightSon(int nod){
return nod*2+1;
}
void Sift(Heap H, int n, int k){
int son;
do{
son=0;
if(LeftSon(k)<=n)
{
son=LeftSon(k);
if(RightSon(k)<=n && H[RightSon(k)]<H[LeftSon(k)])
son=RightSon(k);
if(H[son]>=H[k])
son=0;
}
if(son)
{
swap(H[k],H[son]);
k=son;
}
}while(son);
}
void Percolate(Heap H, int k){
while(H[k]<H[Father(k)])
{
swap(H[k],H[Father(k)]);
k=Father(k);
}
}
void BuildHeap(Heap H, int n){
int i;
for(i=n/2;i>=1;--i)
Sift(H,n,i);
}
int main(){
fin>>n>>v>>x;
H[1]=x;
Q[1]=x;
lg=1;
while(n>1)
{
--n;
fin>>v;
if(v<3)
fin>>x;
switch(v)
{
case 1:{
lg++;
H[lg]=x;
Q[lg]=x;
Percolate(H,lg);
}break;
case 2:{
for(i=1;i<=lg;++i)
if(H[i]==Q[x])
{
swap(H[i],H[lg]);
break;
}
lg--;
BuildHeap(H,lg);
}break;
case 3:{
fout<<H[1]<<'\n';
}break;
}
}
fin.close();
fout.close();
return 0;
}