Pagini recente » Cod sursa (job #1616968) | Cod sursa (job #3239546) | Cod sursa (job #1528024) | Cod sursa (job #1442264) | Cod sursa (job #1340667)
#include <iostream>
#include <fstream>
#define N 200004
using namespace std;
int h[N],top;
int v[N],nr;
int poz[N],inv[N];
int Tata(int nod)
{
return nod/2;
}
int Lson(int nod)
{
return nod*2;
}
int Rson(int nod)
{
return nod*2+1;
}
void Percolate(int k)
{
int key=h[k];
int x=k;
while(k>1 && h[Tata(k)]>key)
{poz[inv[k]]=Tata(k);
inv[k]=inv[Tata(k)];
h[k]=h[Tata(k)];
k=Tata(k);
}
inv[k]=inv[x];
poz[inv[x]]=k;
h[k]=key;
}
void Sift(int k)
{
int son=Lson(k);
while(son)
{
son=0;
if(Lson(k)<=top)
{
son=Lson(k);
if(Rson(k)<=top && h[Rson(k)]<h[son])
son=Rson(k);
if(h[son]>=h[k]) son=0;
}
if (son)
{
poz[inv[son]]=k;
poz[inv[k]]=son;
swap(inv[son],inv[k]);
swap(h[son],h[k]);
k=son;
}
}
}
void Op1(int x)
{
top++;
h[top]=x;
inv[top]=nr;
Percolate(top);
}
void Op2(int x)
{
h[x]=h[top];
top--;
if(x>1 && h[x]<h[Tata(x)])
Percolate(x);
else Sift(x);
}
ofstream fout("heapuri.out");
void Op3()
{
fout<<h[1]<<"\n";
}
void Afisare()
{
for(int i=1; i<=top; i++)
cout<<h[i]<<" "<<poz[7]<<"\n";
cout<<"\n";
}
int CautaB(int x)
{
for(int i=1; i<=top; i++)
if(h[i]==x) return i;
}
//int Cauta1(int x,int poz)
//{
// //cout<<h[poz<<" "<<x<<"\n";
// if(h[poz]==x) return poz;
// if(Rson(poz)<=top)
// if ( h[Rson(poz)]<=x ) return Cauta1(x,Rson(poz));
// if(Lson(poz)<=top)
// if ( h[Lson(poz)]<=x) return Cauta1(x,Lson(poz));
//}
int main()
{
int k,com,x,pozi,i;
ifstream fin("heapuri.in");
fin>>k;
for(i=1; i<=k; i++)
{
fin>>com;
if(com==1)
{
fin>>x;
v[++nr]=x;
Op1(x);
}
if(com==2)
{
fin>>x;
pozi=CautaB(v[x]);
//cout<<pozi<<" "<<poz[x]<<"\n";
//Afisare();
Op2(poz[x]);
}
if(com==3) Op3();
}
return 0;
}