Pagini recente » Cod sursa (job #2326610) | Borderou de evaluare (job #2010168) | Cod sursa (job #1950905) | Cod sursa (job #1141902) | Cod sursa (job #1843015)
#include <fstream>
#include <algorithm>
#define HeapMax 250001
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n,heap[HeapMax],v[HeapMax],code,x,nr,realno;
inline int father(int k)
{
return k/2;
}
inline int leftson(int k)
{
return k*2;
}
inline int rightson(int k)
{
return k*2+1;
}
inline int minheap()
{
return heap[1];
}
void sift(int n, int k)
{
int son;
do
{
son=0;
if(leftson(k)<=n)
{
son=leftson(k);
if(rightson(k)<=n && heap[leftson(k)]>heap[rightson(k)])
son=rightson(k);
if(heap[son]>=heap[k])
son=0;
}
if(son)
{
swap(heap[k],heap[son]);
k=son;
}
}
while(son);
}
void percolate(int n, int k)
{
int key=heap[k];
while( k>1 && key<heap[father(k)])
{
heap[k]=heap[father(k)];
k=father(k);
}
heap[k]=key;
}
void insert_elem(int &n, int key)
{
heap[++n]=key;
percolate(n,n);
}
void delete_elem(int &n, int k)
{
heap[k]=heap[n];
n--;
if(k>1 && heap[k]<heap[father(k)])
percolate(n,k);
else
sift(n,k);
}
int search_elem(int key, int poz)
{
if(heap[poz]==key)
return poz;
else
{
if(heap[leftson(poz)]<=key)
return search_elem(key, leftson(poz));
if(heap[rightson(poz)<=key])
return search_elem(key, rightson(poz));
}
return 0;
}
int main()
{
int r;
fin>>n;
for(int i=1;i<=n;i++)
{
fin>>code;
if(code==1)
{
fin>>x;
insert_elem(nr, x);
v[++realno]=x;
}
else if(code==2)
{
fin>>x;
r=search_elem(v[x], 1);
delete_elem(nr, r);
}
else
fout<<minheap()<<'\n';
}
fin.close();
fout.close();
return 0;
}