Pagini recente » Cod sursa (job #1336029) | Profil WilIiamper | Cod sursa (job #1237247) | Cod sursa (job #1995770) | Cod sursa (job #696784)
Cod sursa(job #696784)
#include <fstream>
#define fs(x) x*2
#define fd(x) x*2+1
using namespace std;
int val[200010],h[200010],l[200010],n,m,nr;
int maxx(int a,int b)
{
if(val[a]<val[b])
return a;
return b;
}
void sift(int i)
{
do
{
if(fs(i)<=m&&val[h[fs(i)]]<val[h[i]])
{
if(fd(i)<=m&&val[h[fd(i)]]<val[h[i]])
{
int s=maxx(fs(i),fd(i));
swap(h[s],h[i]);
l[h[s]]=s;l[h[i]]=i;
i=s;
}
else
{
swap(h[fs(i)],h[i]);
l[h[fs(i)]]=fs(i);l[h[i]]=i;
i=fs(i);
}
}
else
if(fd(i)<=m&&val[h[fd(i)]]<val[h[i]])
{
swap(h[fd(i)],h[i]);
l[h[fd(i)]]=fd(i);l[h[i]]=i;
i=fd(i);
}
else return ;
} while (1);
}
void percolate(int i)
{
do
{
if(val[h[i]]<val[h[i/2]])
{
swap(h[i],h[i/2]);
l[h[i]]=i;l[h[i/2]]=i/2;
i=i/2;
}
else
return;
} while (1);
}
void insert(int x)
{
val[++nr]=x;
h[++m]=nr;l[nr]=m;
percolate(m);
}
void delet(int x)
{
int i=l[x];
if(i!=m)
{
h[i]=h[m];h[m--]=0;
if(val[h[i]]<val[h[i/2]])
percolate(i);
else
sift(i);
}
else
h[m--]=0;
}
int main()
{
ifstream f("heapuri.in");
f>>n;
int o;
ofstream g("heapuri.out");
for(int i=0;i<n;i++)
{
f>>o;int x;
switch(o)
{
case 1: f>>x; insert(x);break;
case 2: f>>x; delet(x);break;
case 3: g<<val[h[1]]<<'\n';
}
}
return 0;
}