Pagini recente » Cod sursa (job #1666743) | Cod sursa (job #75731) | Infoarena Monthly 2012 - Runda 1 | Cod sursa (job #2312364) | Cod sursa (job #2025387)
//5:14
#include <fstream>
#define in "heapuri.in"
#define out "heapuri.out"
#define N 200003
using namespace std;
ifstream fin(in);
ofstream fout(out);
int n,p,x;
int A[N],heap[N],pos[N];
int na,nh;
inline int father(int x)
{
return x/2;
}
inline int left_son(int x)
{
return x*2;
}
inline int right_son(int x)
{
return x*2 + 1;
}
inline void percolate(int poz)
{
while(poz > 1 && A[heap[father(poz)]] > A[heap[poz]])
{
swap(heap[poz],heap[father(poz)]);
swap(pos[heap[poz]], pos[heap[father(poz)]]);
poz = father(poz);
}
}
inline void adaugare(int x)
{
A[++na] = x;
heap[++nh] = na;
pos[na] = nh;
percolate(nh);
}
inline void sift(int poz)
{
int son; // pozitie
do
{
son = 0;
if(left_son(poz) <= nh)
{
son = heap[left_son(poz)];
if(right_son(poz) <= nh && A[heap[right_son(poz)]] < A[son])
son = heap[right_son(poz)];
if(A[heap[son]] >= A[heap[poz]]) son = 0;
}
if(son != 0)
{
swap(heap[poz],heap[son]);
swap(pos[heap[poz]],pos[heap[son]]);
poz = son;
}
} while(son != 0);
}
inline void sterge(int poz)
{
heap[poz] = heap[nh];
pos[heap[nh]] = poz;
--nh;
sift(poz);
}
int main()
{
fin>>n;
while(n--)
{
fin>>p;
if(p < 3) fin>>x;
switch(p)
{
case 1: adaugare(x);
break;
case 2: sterge(pos[x]);
break;
default: fout<<A[heap[1]]<<"\n";
}
}
fin.close(); fout.close();
return 0;
}