Pagini recente » Cod sursa (job #1255632) | Cod sursa (job #3136426) | Cod sursa (job #3132172) | Cod sursa (job #2658532) | Cod sursa (job #1052473)
#include <iostream>
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int heap[2000005],poz[2000005],a[2000005],nr,k;
int i,x,tip,n;
void urca(int pozi)
{
while(pozi>1&& a[heap[pozi]]<a[heap[pozi/2]])
{
swap(heap[pozi],heap[pozi/2]);
poz[heap[pozi]]=pozi;
poz[heap[pozi/2]]=pozi/2;
pozi/=2;
}
}
void coboara(int pozi)
{
{
int poz1=0;
while(pozi!=poz1) {
poz1=pozi;
if(2*poz1<=n && a[heap[pozi]]>a[heap[2*poz1]])
pozi=2*poz1;
if(2*poz1+1<=n && a[heap[pozi]]>a[heap[2*poz1+1]])
pozi=2*poz1+1;
swap(heap[pozi],heap[poz1]);
poz[heap[pozi]]=pozi;
poz[heap[poz1]]=poz1;
}
}}
int main()
{
f>>nr;
for(i=1;i<=nr;i++)
{
f>>tip;
if(tip==1)
{
f>>x;
a[++k]=x;
heap[++n]=k;
poz[k]=n;
urca(n);
}
else if(tip==2)
{
f>>x;
a[x]=a[heap[n--]];
urca(poz[x]);
coboara(poz[x]);
}
else
{
g<<a[heap[1]]<<'\n';
}
}
return 0;
}