Cod sursa(job #2055225)

Utilizator SenibelanMales Sebastian Senibelan Data 2 noiembrie 2017 22:56:33
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.92 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int NMAX = 200005;
int N;
int query_type;
int nr, inp;

class Heap{
    int infinity;
    pair <int, int> heap_vector[NMAX];
    int heap_length;
    vector <int> indices;

    void upheap(int indice){
        if(heap_vector[indice].first <= heap_vector[indice / 2].first && indice != 1){
            indices[heap_vector[indice].second] = indice / 2;
            indices[heap_vector[indice / 2].second] = indice;
            swap(heap_vector[indice], heap_vector[indice / 2]);
            upheap(indice / 2);
        }
    }

    void downheap(int indice){
        int son1 = 2 * indice;
        int son2 = 2 * indice + 1;
        int min_son = heap_vector[son1].first < heap_vector[son2].first ? son1 : son2;
        if(son1 <= heap_length ||  son2 <= heap_length){
            /*
            if(heap_vector[indice] >= heap_vector[min_son]){
                indices[heap_vector[indice].second] = min_son;
                indices[heap_vector[min_son].second] = indice;
                swap(heap_vector[indice], heap_vector[min_son]);
                if(indice != 1)
                    downheap(min_son);
            }
            */
            if(son1 == heap_length)
                min_son = son1;
            
            if(heap_vector[indice] >= heap_vector[min_son]){
                indices[heap_vector[indice].second] = min_son;
                indices[heap_vector[min_son].second] = indice;
                swap(heap_vector[indice], heap_vector[min_son]);
                downheap(min_son);
            }
        }
    }

  public:

    Heap(){
      heap_length = 0;
      infinity = 2000000000;
    }

    int get_nth_indice(int n){
        return indices[n - 1];
    }

    void add_heap(int X){
        heap_vector[++heap_length].first = X;
        heap_vector[heap_length].second = indices.size();
        indices.push_back(heap_length);
        upheap(heap_length);
    }


    void delete_heap(int nr){
        inp = get_nth_indice(nr);
        indices[heap_vector[heap_length].second] = inp;
        swap(heap_vector[heap_length], heap_vector[inp]); 
        heap_length--;
        downheap(inp);
    }
    
    void print_min(){
        out << heap_vector[1].first << "\n";
    }

    void print_heap(){
        for(int i = 1; i <= heap_length; ++i)
            out << heap_vector[i].first << " & " << heap_vector[i].second << " ";
        out << "\n";
    }


} heap;


int main(){
    in >> N;
    while(in >> query_type){
        switch(query_type){
            case 1:
                in >> nr;
                heap.add_heap(nr);
                // heap.print_heap();
                break;
            case 2:
                in >> nr;
                heap.delete_heap(nr);
                // heap.print_heap();
                break;
            case 3:
                heap.print_min();
                break;
        }
    } 
    return 0;
}