Cod sursa(job #1428381)

Utilizator heracleRadu Muntean heracle Data 4 mai 2015 12:55:31
Problema Hashuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 3.25 kb
#include <cstdio>
#include <random>

FILE* in=fopen("hashuri.in","r");
FILE* out=fopen("hashuri.out","w");

const int R=1000000000;

struct treapurel
{
    int val,prio;
    treapurel *st,*dr,*tata;

    treapurel()
    {
        val=0;
        prio=0;
        st=NULL;
        dr=NULL;
        tata=NULL;
    }
} *rad,*nil;

treapurel *minim( treapurel *a,  treapurel *b)
{
    return a->prio < b->prio ?a:b;
}

void merge( treapurel *sd,  treapurel *s,  treapurel *c, treapurel *d, treapurel *ds)
{
    c->st=s;
    c->dr=d;

    if(s!=NULL)
    {
        s->dr=sd;
        c->tata=s->tata;
        s->tata=c;
    }
    if(d!=NULL)
    {
        d->st=ds;

        if(c->tata->prio > d->tata->prio)
            c->tata=d->tata;

        d->tata=c;
    }
}

void urca(treapurel *x)
{
    if(x->tata->st==x)
    {
        if(x->st==NULL)
            merge(nil,x->st,x,x->tata,x->dr );
        else
            merge(x->st->dr,x->st,x,x->tata,x->dr );
    }
    else
    {
        if(x->dr==NULL)
             merge(x->st,x->tata,x,x->dr,nil);
        else
            merge(x->st,x->tata,x,x->dr,x->dr->st);
    }
}

void delet(int x)
{
    treapurel *act;

    act=rad;

    while(act!=NULL)
    {
        if(act->val==x)
            break;

        if(act->val<x)
        {
            act=act->st;
        }
        else
        {
            act=act->dr;
        }
    }

    if(act==NULL)
        return;

    while(true)
    {
        if(act->st==NULL && act->dr==NULL)
        {
            if(act->tata->st==act)
                act->tata->st=NULL;
            else
                act->tata->dr=NULL;
            delete act;
            return;
        }
        else if(act->st==NULL)
        {
            urca(act->dr);
        }
        else if(act->dr==NULL)
        {
            urca(act->st);
        }
        else
        {
            if(act->st->prio < act->dr->prio)
                urca(act->st);
            else
                urca(act->dr);
        }
    }
}

bool find(int x)
{
    treapurel *act;

    act=rad;

    while(act!=NULL)
    {
        if(act->val==x)
            return 1;

        if(act->val<x)
        {
            act=act->st;
        }
        else
        {
            act=act->dr;
        }
    }

    return 0;
}

void add(int x)
{
    int pr=rand()%R;

    treapurel *act,*last;

    act=rad;

    while(act!=NULL)
    {
        last=act;
        if(act->val<=x)
        {
            act=act->st;
        }
        else
        {
            act=act->dr;
        }
    }
    act=last;

    if(act->val<=x)
    {
        act->st=new treapurel;
        act->st->tata=act;
        act->st->val=x;
        act->st->prio=pr;

        act=act->st;

    }
    else
    {
        act->dr=new treapurel;
        act->dr->tata=act;
        act->dr->val=x;
        act->dr->prio=pr;

        act=act->dr;
    }

    while(act->tata->prio > act->prio)
    {
        urca(act);
    }
}

int main()
{
    rad= new treapurel;
    nil= new treapurel;

    int n;

    fscanf(in,"%d",&n);

    int t,x;

    for(int i=1; i<=n; i++)
    {
        fscanf(in,"%d%d",&t,&x);

        if(t==1)
        {
            add(x);
        }
        if(t==2)
        {
            delet(x);
        }
        if(t==3)
        {
            fprintf(out,"%d\n",find(x));
        }
    }

    return 0;
}