Cod sursa(job #1667841)

Utilizator PhilipDumitruPhilip Dumitru PhilipDumitru Data 29 martie 2016 12:00:34
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.96 kb
#include <fstream>

using namespace std;

namespace PhD
{
    template <class T>
    class Heap {
    private:
        long long int hSize;
        long long * heap;
        T * elements;
        long long * location;
        long long nElements;
        //const long long maxSize;

        void upheap(long long int node)
        {
            long long parent = node >> 1;
            while (elements[heap[node]] < elements[heap[node >> 1]] && parent)
            {
                switchElem(node, parent);
                node = parent;
                parent = node >> 1;
            }
        }
        void downheap(long long int node)
        {
            long long child = node << 1;
            while (child <= hSize)
            {
                if (child+1 <= hSize)
                {
                    if (elements[heap[child]] < elements[heap[child+1]])
                    {
                        if (elements[heap[child]] < elements[heap[node]])
                        {
                            switchElem(node, child);
                            node = child;
                            child = node << 1;
                        }
                        else return;
                    } else {
                        if (elements[heap[child+1]] < elements[heap[node]])
                        {
                            switchElem(node, ++child);
                            node = child;
                            child = node << 1;
                        }
                        else return;
                    }
                }
                else if (elements[heap[child]] < elements[heap[node]])
                {
                    switchElem(node, child);
                    node = child;
                    child = node << 1;
                }
                else return;
            }
        }
        void switchElem(long long A, long long B)
        {
            long long temp = heap[A];
            heap[A] = heap[B];
            heap[B] = temp;
            location[heap[A]] = A;
            location[heap[B]] = B;
        }
    public:
        Heap(long long int s)/*:maxSize(s+1)*/
        {
            nElements = 0;
            hSize = 0;
            ++s;
            heap = new long long int [s];
            location = new long long int[s];
            elements = new T[s];
        }
        ~Heap()
        {
            delete[] elements;
            delete[] heap;
            delete[] location;
        }

        long long int getSize()
        {
            return hSize;
        }
        long long push(T x)
        {
            elements[++nElements] = x;
            heap[++hSize] = nElements;
            location[nElements] = hSize;
            upheap(hSize);
            return location[nElements];
        }
        T peak()
        {
            return elements[heap[1]];
        }
        void deleteElement(long long x)
        {
            heap[location[x]] = heap[hSize--];
            location[heap[location[x]]] = location[x];
            downheap(location[x]);
        }
        T pop()
        {
            T temp = elements[heap[1]];
            deleteElement[heap[1]];
            return temp;
        }
    };
}
PhD::Heap <long long>* hp;

int main()
{
    FILE * fin = fopen ("heapuri.in", "r");
    FILE * fout = fopen ("heapuri.out", "w");

    long long int n, k, x;
    fscanf(fin, "%lld", &n);
    PhD::Heap <long long> heap(n);
    hp = &heap;
    for (long long i = 0; i < n; ++i)
    {
        fscanf(fin, "%lld", &k);
        if (k == 1)
        {
            fscanf(fin, "%lld", &x);
            heap.push(x);
        }
        else if (k == 2)
        {
            fscanf(fin, "%lld", &x);
            heap.deleteElement(x);
        }
        else if (k == 3)
        {
            fprintf(fout, "%lld\n", heap.peak());
        }
    }

    fclose(fin);
    fclose(fout);
    return 0;
}