Pagini recente » Cod sursa (job #2654367) | Cod sursa (job #2084358) | Cod sursa (job #1789802) | Cod sursa (job #1530861) | Cod sursa (job #883603)
Cod sursa(job #883603)
#include <fstream>
using namespace std;
int h[200001];
int crono[200001];
int crono2[200001];
void insheap(int poz, int n)
{
int v=h[poz],tata,fiu=poz;
int cr2=crono2[poz];
int cr=crono[cr2];
tata=fiu/2;
while((v<h[tata])&&(tata>0))
{
h[fiu]=h[tata];
crono[crono2[tata]]=fiu;
crono2[fiu]=crono2[tata];
fiu=tata;
tata=fiu/2;
}
h[fiu]=v;
crono2[fiu]=cr2;
crono[cr2]=fiu;
tata=fiu;
fiu=2*tata;
while((v>h[fiu])&&(fiu<=n))
{
if(fiu<n)
if(h[fiu+1]<h[fiu]) fiu++;
h[tata]=h[fiu];
crono[crono2[fiu]]=fiu;
crono2[tata]=crono2[fiu];
tata=fiu;
fiu=tata*2;
}
h[tata]=v;
crono2[tata]=cr2;
crono[cr2]=tata;
}
int main()
{
int n,x,nh=0,i,op,np=0;
ifstream f("heapuri.in");
f>>n;
ofstream g("heapuri.out");
for(i=1;i<=n;i++)
{
f>>op;
switch(op)
{
case 1:
{
f>>x;
nh++;np++;
h[nh]=x;
crono[np]=nh;
crono2[nh]=np;
if(nh>1)
insheap(nh,nh);
break;
}
case 2:
{
f>>x;
h[crono[x]]=h[nh];
crono2[crono[x]]=crono2[nh];
crono[crono2[nh]]=crono[x];
nh--;
insheap(crono[x],nh);
break;
}
case 3:
{
g<<h[1]<<"\n";
break;
}
}
}
f.close();
g.close();
return 0;
}