Cod sursa(job #1274588)

Utilizator buzu.tudor67Tudor Buzu buzu.tudor67 Data 23 noiembrie 2014 23:55:52
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include<fstream>
#include<algorithm>
using namespace std;
ifstream fi("heapuri.in");
ofstream fo("heapuri.out");
  
const int MAX_N = 200005;
  
int poz[MAX_N],a[MAX_N],k,n;
int i,t,x,heap[MAX_N];
short int oper;
  
void downheap(int n,int x){
     int y=0;
      
     while(x!=y)
     {
      y=x;
       
      if(2*y<=n && a[heap[x]]>a[heap[2*y]]) x=2*y;
      if(2*y+1<=n && a[heap[x]]>a[heap[2*y+1]]) x=2*y+1;
       
      swap(heap[x],heap[y]);
      poz[heap[x]]=x;
      poz[heap[y]]=y;
     }
}
  
void upheap(int nod){
      
     while(nod>1 && a[heap[nod]]<a[heap[nod/2]])
     {
      swap(heap[nod],heap[nod/2]);
       
      poz[heap[nod]]=nod;
      poz[heap[nod/2]]=nod/2;
       
      nod/=2;
     }
}
  
void insertheap(int &n,int x){
     n++; k++;
     a[k]=x;// valoarea celui de al k element intr. cronologic
     heap[n]=k;//in nodul n din heap se afla cel de al k element intr. cronologic
     poz[k]=n;//pozitia celui de al k element intr. cronologic in heap
     upheap(n);
}
  
void deleteheap(int &n,int x){
     a[x]=-1;
     upheap(poz[x]);
     poz[heap[1]]=0;
     heap[1]=heap[n--];
     poz[heap[1]]=1;
     downheap(n,1);
}
  
int main(){
    fi>>t;
      
    for(i=1;i<=t;i++){
                      fi>>oper;
                      if(oper==3) fo<<a[heap[1]]<<"\n";
                      else {
                            fi>>x;
                            if(oper==1) insertheap(n,x);
                            else deleteheap(n,x);
                           }
                     }
    fi.close();
    fo.close();
    return 0;
}