Pagini recente » Cod sursa (job #300564) | Cod sursa (job #1528869) | Statistici Minh Nguyen (shoob) | Cod sursa (job #1281876) | Cod sursa (job #1275043)
#include<fstream>
#include<iostream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int NMAX = 200000;
typedef int heap[NMAX+5];
heap h;
int n,poz[NMAX+5],elem,el;
int tata(int nod)
{
return nod/2;
}
int fiu_st(int nod)
{
return nod*2;
}
int fiu_dr(int nod)
{
return nod*2+1;
}
void sift(int nod,int N)
{
int son = 0,ans;
do{
son = 0;
if(h[nod] > h[fiu_dr(nod)])
son = fiu_dr(nod);
if(h[nod] > h[fiu_st(nod)] && h[fiu_st(nod)] < h[fiu_dr(nod)])
son = fiu_st(nod);
if(son != 0){
swap(h[son],h[nod]);
swap(poz[son],poz[nod]);
ans = son;
}
}while(son && son <= N);
}
int perc(int nod)
{
int key = h[nod];
while(nod >= 1 && h[nod] < h[tata(nod)])
{
swap(h[nod],h[tata(nod)]);
nod = tata(nod);
swap(poz[nod],poz[tata(nod)]);
}
h[nod] = key;
return nod;
}
void del(int pozitie,int& N)
{
swap(h[poz[pozitie]],h[N]);
swap(poz[pozitie],poz[N]);
--N;
sift(pozitie,N);
}
int afis_min()
{
return h[1];
}
int main()
{
in>>n;
int q,x;
for(int i = 1 ; i <= n ; i++){
in>>q;
if(q == 1){
in>>x;
++elem;
h[++el] = x;
poz[elem] = perc(el);
}
else if(q == 2){
in>>x;
del(poz[x],el);
}
else
out<<afis_min()<<"\n";
}
return 0;
}