Pagini recente » Cod sursa (job #50757) | Cod sursa (job #2129946) | Cod sursa (job #1768928) | Rating Antares (Antares) | Cod sursa (job #823348)
Cod sursa(job #823348)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int a[200001],b[200001],heap[200001],n=0,nh;
int percolate(int k)
{
while(b[heap[k]]<b[heap[k/2]] && k>1)
{
swap(heap[k],heap[k/2]);
a[heap[k]]=k;
k=k/2;
}
return k;
}
int sift(int &k, int nr)
{
int son; son=2*k;
while(son<=nr && b[heap[son]]<b[heap[k]])
{
if(son+1<=nr && b[heap[son+1]]<b[heap[son]])
son=son+1;
swap(heap[son],heap[k]);
a[heap[k]]=k;
k=son;
son=2*k;
}
return k;
}
int insert(int x)
{
heap[nh]=n;
if(nh>1)
return percolate(nh);
else return 1;
}
void stergere(int x)
{
int k=1;
//k=a[x];
heap[k]=heap[nh]; a[heap[nh]]=k;
if(k>1 && b[heap[k]]<b[heap[k/2]])
a[heap[k]]=percolate(k);
else
{ k=sift(k,nh-1);
a[heap[k]]=k; }
}
int main()
{
int nr,c,x;
f>>nr;
int i;
for(i=0;i<nr;i++)
{
f>>c;
if(c==1) {
f>>x; n++; nh++; b[n]=x;
a[n]=insert(n);}
if(c==2) { f>>x; b[x]=-1; a[x]=percolate(a[x]);
stergere(x); nh--;}
if(c==3) g<<b[heap[1]]<<endl;
}
}