Cod sursa(job #2743004)

Utilizator lucaperjuLuca Perju Verzotti lucaperju Data 22 aprilie 2021 13:59:36
Problema Heapuri cu reuniune Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <fstream>
using namespace std;
ifstream cin ("mergeheap.in");
ofstream cout ("mergeheap.out");
struct heap
{
    int val;
    heap*son;
    heap*nxt;
};
heap* emptyheap=new heap
{
    0,NULL,NULL
};
heap* meld (heap* a, heap* b)
{
    if(a==emptyheap)
        return b;
    if(b==emptyheap)
        return a;
    if(a->val>b->val)
    {
        b->nxt=a->son;
        a->son=b;
        return a;
    }
    else
    {
        a->nxt=b->son;
        b->son=a;
        return b;
    }
}
int getmax (heap* a)
{
    return a->val;
}
heap* insert (heap* a, int val)
{
    heap* b=new heap
    {
        val,emptyheap,emptyheap
    };
    return meld(a,b);
}
heap* twopass (heap* a)
{
    if(a==emptyheap)
        return emptyheap;
    if(a->nxt==emptyheap)
        return a;
    return meld(meld(a,a->nxt),twopass(a->nxt->nxt));
}
heap* del (heap* a)
{
    return twopass(a->son);
}
heap* myHeaps[103];
int main()
{
    ios_base::sync_with_stdio(false);
    int q,t,m,i,j,n;
    cin>>n>>q;
    for(i=1;i<=n;++i)
        myHeaps[i]=emptyheap;
    while(q--)
    {
        int a,b,c;
        cin>>a>>b;
        if(a&1)
            cin>>c;
        if(a==1)
            myHeaps[b]=insert(myHeaps[b],c);
        if(a==2)
        {
            cout<<getmax(myHeaps[b])<<'\n';
            myHeaps[b]=del(myHeaps[b]);
        }
        if(a==3)
        {
            myHeaps[b]=meld(myHeaps[b],myHeaps[c]);
            myHeaps[c]=emptyheap;
        }
    }
	return 0;
}