Cod sursa(job #1952483)

Utilizator DobosDobos Paul Dobos Data 4 aprilie 2017 10:25:13
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
#include <bits/stdc++.h>
#define NMAX 200005
#define BMAX 10005
using namespace std;

ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");

char buffer[BMAX];


int H[NMAX],P[NMAX],V[NMAX],N,NR,e = BMAX - 1;


void pars(int &x){
    while(!isdigit(buffer[e])){
        e++;
        if(e == BMAX){
            fin.read(buffer,BMAX);
            e = 0;
        }
    }
    x = 0;
    while(isdigit(buffer[e])){
        x = x * 10 + (buffer[e] - '0');
        e++;
        if(e == BMAX){
            fin.read(buffer,BMAX);
            e = 0;
        }
    }
}



void UpHeap(int k){

    if( k > 1 && V[H[k]] < V[H[k >> 1]]){
        swap(H[k],H[k >> 1]);
        P[H[k]] = k;
        P[H[k >> 1]] = k >> 1;
        UpHeap(k >> 1);
    }
}
void DownHeap(int k){

    int son = (k << 1) + 1;
    if(V[H[k]] > V[H[son]] && son <= N){
        swap(H[k],H[son]);
        P[H[k]] = k;
        P[H[son]] = son;
        DownHeap(son);
    }
    son = (k << 1);
    if(V[H[k]] > V[H[son]] && son <= N){
        swap(H[k],H[son]);
        P[H[k]] = k;
        P[H[son]] = son;
        DownHeap(son);
    }
}


int main()
{
    ios :: sync_with_stdio(false);

    int n,t,x;

  //  fin >> n;
    pars(n);

    while(n--){
      //  fin >> t;
        pars(t);
        if(t == 1){
            pars(x);
         //   fin >> x;
            V[++NR] = x;
            H[++N] = NR;
            P[NR] = N;
            UpHeap(N);

        }
        if(t == 2){
            pars(x);
           // fin >> x;
            int p = P[x];
            P[H[N]] = p;
            swap(H[p],H[N]);
            N--;

            UpHeap(p);
            DownHeap(p);

        }
        if(t == 3)
            fout << V[H[1]] << "\n";
    }



    return 0;
}