Pagini recente » Cod sursa (job #625020) | Cod sursa (job #1747303) | Cod sursa (job #2369415) | Cod sursa (job #2619758) | Cod sursa (job #827575)
Cod sursa(job #827575)
#include<fstream>
#define elmax 1000000000
#define lmax 210000
using namespace std;
int poz[lmax],heap[lmax],a[lmax];
int tata(int nod){
return nod/2;}
int fiust(int nod){
return 2*nod;}
int fiudr(int nod){
return 2*nod+1;}
int minim(){
return a[heap[1]];}
void cerne(int n, int k){
int fiu;
do{
fiu=0;
if(fiust(k)<=n){
fiu=fiust(k);
if((fiudr(k)<=n)&&(a[heap[fiudr(k)]]<a[heap[fiu]]))fiu=fiudr(k);
if(a[heap[k]]<=a[heap[fiu]])fiu=0;
}
if(fiu){
swap(heap[fiu],heap[k]);
poz[heap[fiu]]=fiu;
poz[heap[k]]=k;
k=fiu;}
}while(fiu);
}
void interclaseaza(int n, int k){
while((k>1)&&(a[heap[tata(k)]]>a[heap[k]])){
swap(heap[tata(k)],heap[k]);
poz[heap[k]]=k;
poz[heap[tata(k)]]=tata(k);
k=tata(k);
}
}
void eliminare(int n, int k){
a[k]=elmax;
cerne(n,poz[k]);
}
void inserare(int &n, int el){
n++;
a[n]=el;
heap[n]=n;
poz[n]=n;
interclaseaza(n,n);
}
int main(){
int i,x,y,nrop,n=0;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
f>>nrop;
for(i=1;i<=nrop;i++){
f>>x;
if(x==1){
f>>y;
inserare(n,y);}
else if(x==2){
f>>y;
eliminare(n,y);}
else g<<minim()<<'\n';
}
f.close();
g.close();
}