Pagini recente » Cod sursa (job #1712137) | Cod sursa (job #904958) | Cod sursa (job #1733472) | Cod sursa (job #2376053) | Cod sursa (job #1854724)
#include <fstream>
#define lgmax 200001
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
typedef int heap[lgmax];
int tata(int nod){ return nod/2; }
int st(int nod) { return nod*2; }
int dr(int nod) { return nod*2+1; }
void sift(heap h,int hsize,int poz)
{
int next;
do
{
next=0;
if(st(poz)<=hsize)
{
next=st(poz);
if(dr(poz)<=hsize && h[st(poz)]>h[dr(poz)])
next=dr(poz);
if(h[poz]<=h[next])
next=0;
}
if(next)
{
swap(h[next],h[poz]);
poz=next;
}
}while(next);
}
void percolate(heap h,int hsize,int poz)
{
int d=h[poz];
while(poz>1 && h[tata(poz)]>d)
{
h[poz]=h[tata(poz)];
poz=tata(poz);
}
h[poz]=d;
}
void insert(heap h,int &hsize,int nr)
{
h[++hsize]=nr;
percolate(h,hsize,hsize);
}
void cut(heap h,int &hsize,int poz)
{
h[poz]=h[hsize];
hsize--;
if(poz>1 && h[poz]<h[tata(poz)])
percolate(h,hsize,poz);
else
sift(h,hsize,poz);
}
int search(heap h,int hsize,int val)
{
int i;
for(i=1;i<=hsize;i++)
if(h[i]==val)
return i;
}
int main()
{
heap h;
int poz[lgmax];
int i,n,lg=0,op,nr,ct=0;
fin>>n;
for(i=1;i<=n;i++)
{
fin>>op;
if(op==1)
{
fin>>nr;
insert(h,lg,nr);
poz[++ct]=nr;
}
if(op==2)
{
fin>>nr;
cut(h,lg,search(h,lg,poz[nr]));
}
if(op==3)
fout<<h[1]<<'\n';
}
return 0;
}