Cod sursa(job #2744445)

Utilizator bestman4111Tiberiu Niculae bestman4111 Data 24 aprilie 2021 18:30:39
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include<iostream>
#include<fstream>
using namespace std;

ifstream cit("heapuri.in");
ofstream afis("heapuri.out");

#define maxim 200010
int l, nr;
int heap[maxim], poz[maxim], elem[maxim];

void introd_val(int x){
    while((x/2) && (elem[heap[x]] < elem[heap[x/2]])){
        swap(heap[x], heap[x/2]);
        poz[heap[x]] = x;
        poz[heap[x/2]] = x/2;
        x/=2;
    }
}

void sterg_val(int x){
    int y = 0;
    while(x != y){
        y = x;
        if((y*2 <= l) && (elem[heap[x]] > elem[heap[y*2]])){
            x = y*2;
        }
        if((y*2+1 <= l) && (elem[heap[x]] > elem[heap[y*2+1]])){
            x = y*2+1;
        }
        swap(heap[x], heap[y]);
        poz[heap[x]] = x;
        poz[heap[y]] = y;
    }
}

int main()
{
    int cod, x, n;
    cit>>n;
    for(int i = 0; i < n; i++){
        cit>>cod;
        if(cod < 3){
            cit>>x;
        }
        if(cod == 1){
            l++;
            nr++;
            elem[nr] = x;
            heap[l] = nr;
            poz[nr] = l;
            introd_val(x);
        }
        else if(cod == 2){
            elem[x] = -1;
            introd_val(poz[x]);
            poz[heap[1]] = 0;
            heap[1] = heap[l--];
            poz[heap[1]] = 1;
            if(l>1){
                sterg_val(1);
            }
        }
        else if(cod == 3){
            afis<<elem[heap[1]]<<"\n";
        }
    }
    return 0;
}