Pagini recente » Cod sursa (job #2240485) | Cod sursa (job #3179739) | Cod sursa (job #2059074) | Cod sursa (job #1745074) | Cod sursa (job #1007690)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n,el,h[200005],a[200005],pos[200005];
void sift(int k)
{ int ok=1,son;
while(ok)
{ son=2*k; if (son>n) break;
if (son<n && a[h[2*k+1]]<a[h[son]]) son=2*k+1;
if (a[h[son]]<a[h[k]])
{swap(h[son],h[k]);
pos[h[son]]=son;
pos[h[k]]=k;
k=son;}
else ok=0;
}
}
void percolate(int k)
{
while(a[h[k]]<a[h[k/2]] && k>1)
{swap(h[k],h[k/2]);
pos[h[k]]=k;
pos[h[k/2]]=k/2;
k/=2;
}
}
void insert(int nr)
{ n++; h[n]=nr;
percolate(n);
}
void cut(int k)
{ h[k]=h[n];
pos[h[k]]=k;
pos[h[n]]=n;
n--;
if (a[h[k]]<a[h[k/2]] && k>1) percolate(k);
else sift(k);
}
int main()
{ int i,t,p,x;
f>>p; el=0;
for(i=1;i<=p;i++)
{ f>>t;
if (t==1) {el++; f>>a[el]; pos[el]=n+1; insert(el);}
if (t==2) {f>>x; cut(pos[x]);}
if (t==3) g<<a[h[1]]<<"\n";
}
return 0;
}