Cod sursa(job #1950927)

Utilizator DobosDobos Paul Dobos Data 3 aprilie 2017 12:43:19
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>
#define NMAX 200005

using namespace std;

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


int H[NMAX],P[NMAX],V[NMAX],N,NR;

void UpHeap(int k){

    while( 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;
        k = k >> 1;
    }
}
void DownHeap(int k){

    int 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);
    }
    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);
    }
}


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

    int n,t,x,k = 0;

    fin >> n;

    while(n--){
        fin >> t;

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

         //   fout << x << " " << V[H[1]] << "\n";

        }
        if(t == 2){

            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;
}