Pagini recente » Cod sursa (job #2174356) | Cod sursa (job #1090909) | Cod sursa (job #386005) | Cod sursa (job #1178968) | Cod sursa (job #943643)
Cod sursa(job #943643)
#include<fstream>
#include<algorithm>
using namespace std;
int a[200001],v[200001],h[200001],i,n,x,m,nr;
void push(int x)
{
int k=n;
h[n]=x;
while ((k>1) && (a[h[k]]<a[h[k/2]]))
{
swap(v[h[k]],v[h[k/2]]);
swap(h[k],h[k/2]);
k/=2;
}
}
void pop(int x)
{
int k=x;
while (k<=n/2)
{
if ((k*2==n) || (a[h[k*2]]<a[h[k*2+1]]))
{
swap(v[h[k]],v[h[2*k]]);
swap(h[k],h[k*2]);
k=k*2;
}
else
{
swap(v[h[k]],v[h[2*k+1]]);
swap(h[k],h[k*2+1]);
k=k*2+1;
}
}
if (k!=n)
{
swap(v[h[k]],v[h[n]]);
swap(h[k],h[n]);
while ((k>1) && (a[h[k]]<a[h[k/2]]))
{
swap(v[h[k]],v[h[k/2]]);
swap(h[k],h[k/2]);
k/=2;
}
}
}
int main()
{
ifstream f("heapuri.in");
ofstream g("heapuri.out");
f >> m;
for (i=1;i<=m;i++)
{
f >> x;
if (x==1)
{
f >> x;
n++;nr++;
a[nr]=x;
v[nr]=n;
push(nr);
}
else if (x==2)
{
f >> x;
pop(v[x]);
n--;
}
else g << a[h[1]] << '\n';
}
return 0;
}