Pagini recente » Cod sursa (job #243410) | Cod sursa (job #2587679) | Cod sursa (job #922077) | Cod sursa (job #2527285) | Cod sursa (job #2173311)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
long long h[200001];
int ct[200001];
int ct1[200001];
int tata(int nod)
{
return nod/2;
}
int fiu_d(int nod)
{
return 2*nod+1;
}
int fiu_s(int nod)
{
return 2*nod;
}
void percolate(int k)
{
int c=h[k];
while(k>1 and c<h[tata(k)])
{
h[k]=h[tata(k)];
int k2=k;
ct[h[k]]=tata(k);
ct[h[tata(k)]]=k2;
k=tata(k);
}
h[k]=c;
ct[h[k]]=k;
}
void shift(int nr, int k)
{
k=ct[k];
int fiu=0, con=h[k];
h[k]=INT_MAX;
do
{
if(fiu_s(k)<=nr and h[fiu]<=h[k])
{
fiu=fiu_s(k);
if(fiu_d(k)<=nr and h[fiu_s(k)]>h[fiu_d(k)])
fiu=fiu_d(k);
if(h[fiu]>=h[k])
fiu=0;
}
else
fiu=0;
if(fiu)
{
swap(h[k], h[fiu]);
swap(ct[h[k]], ct[con]);
k=fiu;
}
}while(fiu);
}
int main()
{
int n, k=1;
fin>>n;
for(int i=1;i<=n;i++)
{
int a, b;
fin>>a;
if(a==1)
{
fin>>b;
h[k]=b;
ct[b]=k;
ct1[k]=b;
percolate(k);
k++;
}
if(a==2)
{
fin>>b;
shift(k-1 , ct1[b]);
}
if(a==3)
fout<<h[1]<<'\n';
}
return 0;
}