Pagini recente » Cod sursa (job #631613) | Cod sursa (job #2469712) | Cod sursa (job #1504889) | Cod sursa (job #2469309) | Cod sursa (job #2033181)
#include<fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n, nr, q, x, a[200002], heap[200002], p[200002], copil, parinte, h;
void urcare(int x)
{
int copil=x;
int parinte=x/2;
while(parinte>=1 && a[heap[parinte]]>a[heap[copil]])
{
swap(heap[parinte], heap[copil]);
p[heap[parinte]]=parinte;
p[heap[copil]]=copil;
copil=parinte;
parinte/=2;
}
}
void coborare(int x)
{
int copil=2*x;
int parinte=x;
while(copil<=n)
{
if(copil+1<=n && a[heap[p[copil+1]]]>a[heap[p[copil]]])
copil++;
if(a[heap[p[parinte]]]<a[heap[p[copil]]])
{
swap(a[heap[p[parinte]]], a[heap[p[copil]]]);
parinte=copil;
copil*=2;
}
}
}
int main()
{
f>>nr;
while(nr>0)
{
f>>q;
if(q==1)
{
f>>x;
a[++n]=x;
heap[++h]=n;
p[n]=h;
urcare(n);
}
if(q==2)
{
f>>x;
heap[p[x]]=heap[h];
h--;
if(heap[p[x]]>heap[p[x]/2])
urcare(x);
else coborare(x);
}
if(q==3)
g<<a[heap[1]]<<"\n";
nr--;
}
return 0;
}