Pagini recente » Cod sursa (job #1316846) | Cod sursa (job #1646348) | Cod sursa (job #2119879) | Cod sursa (job #2680945) | Cod sursa (job #953438)
Cod sursa(job #953438)
#include<fstream>
using namespace std;
const int MAXN=200002;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int h[MAXN],poz[MAXN],ord[MAXN];
int n,heapsize;
inline int left(int i){return (i<<1);}
inline int right(int i){return (i<<1)+1;}
inline int parent(int i){return (i>>1);}
void up_heap(int i)
{
while (i>0 && h[i]<h[parent(i)])
{
swap(h[i],h[parent(i)]);
swap(poz[i],poz[parent(i)]);
i=parent(i);
}
}
void down_heap(int i)
{
int l=left(i),r=right(i),smallest=i;
if (l<=heapsize && h[l]<h[smallest])
smallest=l;
if (r<=heapsize && h[r]<h[smallest])
smallest=r;
if (smallest!=i)
{
swap(h[smallest],h[i]);
down_heap(smallest);
}
}
void insert(int elem,int nr_input)
{
++heapsize;
h[heapsize]=elem;
ord[nr_input]=elem;
poz[elem]=heapsize;
up_heap(heapsize);
}
void remove(int nr_input)
{
int elem=ord[nr_input],pozitie=poz[elem];
swap(h[heapsize],h[poz[elem]]);
swap(poz[h[heapsize]],poz[elem]);
poz[h[heapsize]]=0;
h[heapsize]=0;
--heapsize;
down_heap(pozitie);
}
void show()
{
fout<<h[1]<<'\n';
}
int main()
{
int op,x,k;
fin>>n;
for (k=1;k<=n;++k)
{
fin>>op;
if (op==1)
{
fin>>x;
insert(x,heapsize);
}
else if (op==2)
{
fin>>x;
remove(x);
}
else if (op==3)
show();
}
fin.close();
fout.close();
return 0;
}