Pagini recente » Cod sursa (job #982854) | Cod sursa (job #1254989)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
const int MAX=200005;
int t, o, i, j, h[MAX], n, k, a[MAX], m, index[MAX], x;
void upheap()
{
while (k>1 && a[h[k]]<a[h[k/2]])
{
swap(h[k], h[k/2]);
swap(index[h[k]], index[h[k/2]]);
k/=2;
}
}
void downheap()
{
int nod;
do
{
nod=0;
if (k*2<=n) nod=k*2;
if (k*2+1<=n && a[h[2*k+1]]<a[h[2*k]])
nod=k*2+1;
if (a[h[nod]]>=a[h[k]]) nod=0;
if (nod)
{
swap(h[k], h[nod]);
swap(index[h[k]], index[h[nod]]);
k=nod;
}
} while (nod);
}
void cut() {
h[k] = h[n];
--n;
if ((k > 1) && (a[h[k]] < a[h[k/2]])) {
upheap();
} else {
downheap();
}
}
int main()
{
cin>>t;
while(t--)
{
cin>>o;
if (o==3) cout<<a[h[1]]<<'\n';
if (o==1)
{
cin>>x;
a[++n]=x;
h[++i]=n;
index[n]=h[i];
k=n;
upheap();
}
if (o==2)
{
cin>>x; k=index[x];
cut();
}
}
return 0;
}