Cod sursa(job #2906020)

Utilizator PatrascuAdrian1Patrascu Adrian Octavian PatrascuAdrian1 Data 24 mai 2022 19:31:57
Problema Heapuri cu reuniune Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 7.6 kb
#include <bits/stdc++.h>

using namespace std;

class InParser
{
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch()
    {
        ++sp;
        if (sp == 4096)
        {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume)
    {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n)
    {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-')
        {
            n = 0;
            sgn = -1;
        }
        else
        {
            n = c - '0';
        }
        while (isdigit(c = read_ch()))
        {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n)
    {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-')
        {
            n = 0;
            sgn = -1;
        }
        else
        {
            n = c - '0';
        }
        while (isdigit(c = read_ch()))
        {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

class OutParser
{
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch)
    {
        if (sp == 50000)
        {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        }
        else
        {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name)
    {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser()
    {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32)
    {
        if (vu32 <= 9)
        {
            write_ch(vu32 + '0');
        }
        else
        {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64)
    {
        if (vu64 <= 9)
        {
            write_ch(vu64 + '0');
        }
        else
        {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch)
    {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch)
    {
        while (*ch)
        {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};

struct node
{
    node *st, *dr, *fiu, *tata;
    int key;
    int grad;
    bool seen;
};

class FiboHeap
{
    node* heap;
public:
    FiboHeap()
    {
        heap = _empty();
    }
    ~FiboHeap()
    {
        if(heap)
            _deleteAll(heap);
    }
    node* Insert(int key)
    {
        node* x = new node;
        x->key = key;
        x->st = x->dr = x;
        x->grad = 0;
        x->seen = false;
        x->fiu = NULL;
        x->tata = NULL;
        heap = _Dalinar(heap,x);
        return x;
    }
    void Dalinar(FiboHeap& other)
    {
        heap = _Dalinar(heap,other.heap);
        other.heap = _empty();
    }

    bool isEmpty()
    {
        return heap == NULL;
    }

    int getMaximum()
    {
        return heap->key;
    }

    int _removeMaximum()
    {
        node* old = heap;
        heap = __removeMaximum(heap);
        int ret = old->key;
        delete old;
        return ret;
    }
private:
    node* _empty()
    {
        return NULL;
    }

    node* _Dalinar(node* a,node* b)
    {
        if(a == NULL)
            return b;
        if(b == NULL)
            return a;
        //Reunesc a cu b in a
        if(a->key < b->key)
        {
            node* temp = a;
            a = b;
            b = temp;
        }
        //fosturile noduri la care sunt legate
        node* an = a->dr;
        node* bp = b->st;
        //linkare intre radacinile multimilor
        a->dr = b;
        b->st = a;
        //relinkarea resturilor
        an->st = bp;
        bp->dr = an;
        return a;
    }

    void _deleteAll(node* n)
    {
        if(n != NULL)
        {
            node* c = n;
            //sterg de la frunze la radacina nodurile.
            do
            {
                node* d = c;
                c = c->dr;
                _deleteAll(d->fiu);
                delete d;
            }
            while(c != n);
        }
    }

    void _addfiu(node* tata,node* fiu)
    {
        fiu->st = fiu->dr = fiu;
        fiu->tata = tata;
        tata->grad++;
        tata->fiu = _Dalinar(tata->fiu,fiu);
    }

    void Debarcare(node* n)
    {
        if(n == NULL)
            return;
        node* c = n;
        do
        {
            c->seen = false;
            c->tata = NULL;
            c = c->dr;
        }
        while(c != n);
    }

    node* __removeMaximum(node* n)
    {
        Debarcare(n->fiu);
        if(n->dr == n)
        {
            n = n->fiu;
        }
        else
        {
            n->dr->st = n->st;
            n->st->dr = n->dr;
            n = _Dalinar(n->dr,n->fiu);
        }
        if(n == NULL)
            return n;
        node* trees[64] = {NULL};

        while(true)
        {
            if(trees[n->grad] != NULL)
            {
                node* t = trees[n->grad];
                if(t == n)
                    break;
                trees[n->grad] = NULL;
                if(n->key > t->key)
                {
                    //sari peste t
                    t->st->dr = t->dr;
                    t->dr->st = t->st;
                    //ca sa adaugi n
                    _addfiu(n,t);
                }
                else
                {
                    t->st->dr = t->dr;
                    t->dr->st = t->st;
                    //daca am doar un nod in fiboheap
                    if(n->dr == n)
                    {
                        t->dr = t->st = t;
                        _addfiu(t,n);
                        n = t;
                    }
                    else
                    {
                        n->st->dr = t;
                        n->dr->st = t;
                        t->dr = n->dr;
                        t->st = n->st;
                        _addfiu(t,n);
                        n = t;
                    }
                }
                continue;
            }
            else
            {
                trees[n->grad] = n;
            }
            n = n->dr;
        }
        node* Max = n;
        node* start = n;
        do
        {
            if(n->key > Max->key)
                Max = n;
            n = n->dr;
        }
        while(n != start);
        return Max;
    }

};

int main()
{

    InParser in("mergeheap.in");
    OutParser out("mergeheap.out");
    int N, T;
    in >> N >> T;
    FiboHeap H[N + 1];
    while(T--)
    {
        int c;
        in >> c;
        if(c  ==  1)
        {
            int m, x;
            in >> m >> x;
            H[m].Insert(x);
        }
        else if(c ==  2)
        {
            int m;
            in >> m;
            out << H[m].getMaximum() << '\n';
            H[m]._removeMaximum();
        }
        else
        {
            int a, b;
            in >> a >> b;
            H[a].Dalinar(H[b]);
        }
    }
    return 0;
}