Pagini recente » Cod sursa (job #2341482) | Cod sursa (job #2744657) | Cod sursa (job #59893) | Cod sursa (job #3220254) | Cod sursa (job #613527)
Cod sursa(job #613527)
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int h[200001],a[200001],b[200001],n,i,j,x,y;
void swap (int &q, int &r)
{
int aux=q;
q=r;
r=aux;
}
void heapup (int j)
{
if (h[j]<h[j/2])
{
swap (h[j],h[j/2]);
swap (a[h[j]],a[h[j/2]]);
heapup (j/2);
}
else return;
}
void heaprepair (int m, int j)
{
int x,s,d;
if (j>=m*2)
{
s=m*2;
if (j>=m*2+1) d=m*2+1; else d=s;
if (h[s]<h[d]) x=s; else x=d;
h[m]=h[x];
heaprepair(x,j);
}
else return;
}
int main()
{
f>>n;
j=0;
for (i=1;i<=n;i++)
{
f>>x;
if (x==1)
{
f>>y;
b[++j]=y;
a[y]=j;
h[j]=y;
heapup(j);
}
else
if (x==2)
{
f>>y;
heaprepair(a[b[y]],j);
a[b[y]]=0;
j--;
}
else g<<h[1]<<"\n";
}
f.close();
g.close();
return 0;
}