Pagini recente » Cod sursa (job #584720) | Cod sursa (job #2113135) | Cod sursa (job #1265556) | Cod sursa (job #637853) | Cod sursa (job #1275109)
#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;
do{
son = 0;
if(fiu_st(nod) <= N)
son = fiu_st(nod);
if(fiu_dr(nod) <= N && h[fiu_st(nod)] > h[fiu_dr(nod)])
son = fiu_dr(nod);
if(h[nod] < h[son])
son = 0;
if(son != 0){
swap(h[son],h[nod]);
swap(poz[son],poz[nod]);
nod = son;
}
}while(son);
}
int perc(int nod)
{
int key = h[nod];
while(tata(nod) > 1 && h[nod] < h[tata(nod)])
{
swap(h[nod],h[tata(nod)]);
swap(poz[nod],poz[tata(nod)]);
nod = tata(nod);
}
h[nod] = key;
return nod;
}
void del(int pozitie,int& N)
{
swap(h[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;
++el;
h[el] = x;
poz[elem] = perc(el);
}
else if(q == 2){
in>>x;
del(poz[x],el);
}
else
out<<afis_min()<<"\n";
}
return 0;
}