Cod sursa(job #1557815)

Utilizator PTAdrian64Pop-Tifrea Adrian PTAdrian64 Data 28 decembrie 2015 12:30:16
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <cstdio>
#define nmax 200004

using namespace std;

int N,H[nmax],A[nmax],C[nmax],L=0,R=0;

inline int tata(int x){
  return (x>>1);
}
inline int fiu_stang(int x){
  return (x<<1);
}
inline int fiu_drept(int x){
  return fiu_stang(x)+1;
}
inline void schimba(int &a,int &b){
  int aux = a;
  a = b;
  b = aux;
}

void adauga(int x){

  while(tata(x)>0 && A[H[x]] < A[H[tata(x)]]){
    schimba(H[x],H[tata(x)]);
    C[H[x]] = x;
    C[H[tata(x)]] = tata(x);
    x = tata(x);
  }

}

void sterge(int x){

  int y = 0;
  while(y!=x){

    y = x;
    if(fiu_stang(y) <= L && A[H[fiu_stang(y)]] < A[H[x]])x = fiu_stang(y);
    if(fiu_drept(y) <= L && A[H[fiu_drept(y)]] < A[H[x]])x = fiu_drept(y);

    schimba(H[x],H[y]);
    C[H[x]] = x;
    C[H[y]] = y;

  }

}

int main(){

  freopen("heapuri.in","r",stdin);
  freopen("heapuri.out","w",stdout);

  scanf("%d ",&N);
  int cod,nr;
  while(N--){
    scanf("%d ",&cod);
    switch(cod){
      case 1:
        scanf("%d ",&nr);
        A[++R] = nr;
        H[++L] = R;
        C[R] = L;
        adauga(L);
        break;
      case 2:
        scanf("%d ",&nr);
        A[nr] = -1;
        adauga(C[nr]);
        C[H[1]] = 0;
        H[1] = H[L--];
        C[H[1]] = 1;
        if(L>1)sterge(1);
        break;
      case 3:
        printf("%d\n",A[H[1]]);
        break;
    }

  }
  return 0;
}