Cod sursa(job #2462123)

Utilizator patcasrarespatcas rares danut patcasrares Data 26 septembrie 2019 19:42:37
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.16 kb
#include<fstream>

#include<algorithm>

#include<ctime>

#include<vector>

#include<list>

#define DN 100005

#define x first

#define y second

#define pb push_back

using namespace std;

ifstream fin("secv8.in");

ofstream fout("secv8.out");

int q,type,f,poz,p,ma,g;

char ch;

struct treap

{

    int nrc=0,pr=0,val=0,r=0;

    treap *ls=0,*ld=0;

    treap(int a,int d,treap *b,treap *c)

    {

        val=d;

        pr=a;

        ls=b;

        ld=c;

    }

};

treap *t,*nil;

void update(treap *&nod)

{

    if(nod==nil)

        return;

    if(nod->ls!=nil)

        nod->ls->r^=nod->r;

    if(nod->ld!=nil)

        nod->ld->r^=nod->r;

    if(nod->r)

    {

        swap(nod->ls,nod->ld);

        nod->r=0;

    }

}

void getdesc(treap *&nod)

{

    nod->nrc=1+nod->ls->nrc+nod->ld->nrc;

}

void rot1(treap *&nod)

{

    treap *f=nod->ls;

    nod->ls=f->ld;

    f->ld=nod;

    getdesc(nod);

    getdesc(f);

    nod=f;

}

void rot2(treap *&nod)

{

    treap *f=nod->ld;

    nod->ld=f->ls;

    f->ls=nod;

    getdesc(nod);

    getdesc(f);

    nod=f;

}

void balance(treap *&nod)

{

    if(nod->ls->pr>nod->pr)

        rot1(nod);

    else

        if(nod->ld->pr>nod->pr)

            rot2(nod);

        else

            getdesc(nod);

}

void ins(treap *&nod,int f,int p,int poz)

{

    if(nod==nil)

    {

        nod=new treap(p,f,nil,nil);

        nod->nrc=1;

        return;

    }

    update(nod);

    update(nod->ls);

    update(nod->ld);

    if(nod->ls->nrc>=poz)

        ins(nod->ls,f,p,poz);

    else

    {

        poz-=nod->ls->nrc+1;

        ins(nod->ld,f,p,poz);

    }

    balance(nod);

}

void del(treap *&nod)

{

    if(nod->ls==nil&&nod->ld==nil)

    {

        delete nod,nod=nil;

        return;

    }

    update(nod);

    update(nod->ls);

    update(nod->ld);

    if(nod->ls->pr>nod->ld->pr)

    {

        rot1(nod);

        del(nod->ld);

    }

    else

    {

        rot2(nod);

        del(nod->ls);

    }

    getdesc(nod);

}

void rev(int f,int g)

{

    ins(t,0,ma+2,f-1);

    ins(t->ld,0,ma+1,g-(f-1));

    treap *Ts,*T,*Td;

    Ts=t->ls;

    T=t->ld->ls;

    Td=t->ld->ld;

    T->r^=1;

    delete t->ld,t->ld=nil;

    delete t,t=nil;

    t=new treap(ma+2,0,Ts,T);

    del(t);

    T=t;

    t=new treap(ma+2,0,T,Td);

    del(t);

}

void gaseste(treap *&nod,int poz)

{

    update(nod);

    update(nod->ls);

    update(nod->ld);

    if(nod->ls->nrc+1==poz)

    {

        fout<<nod->val<<'\n';

        return;

    }

    if(nod->ls->nrc>=poz)

        gaseste(nod->ls,poz);

    else

        gaseste(nod->ld,poz-nod->ls->nrc-1);

}

void stergetot(treap *&nod)

{

    if(nod==nil)

        return;

    stergetot(nod->ls);

    stergetot(nod->ld);

    delete nod,nod=nil;

}

void del2(int f,int g)

{

    ins(t,0,ma+2,f-1);

    ins(t->ld,0,ma+1,g-(f-1));

    treap *Ts,*T,*Td;

    Ts=t->ls;

    T=t->ld->ls;

    Td=t->ld->ld;

    stergetot(T);

    delete t,t=nil;

    t=new treap(ma+2,0,Ts,Td);

    del(t);

}

void afis(treap *&nod)

{

    if(nod==nil)

        return;

    update(nod);

    update(nod->ls);

    update(nod->ld);

    afis(nod->ls);

    fout<<nod->val<<' ';

    afis(nod->ld);

}

int main()

{

    t=nil=new treap(0,0,nil,nil);

    srand(time(0));

    fin>>q>>type;

    while(q--)

    {

        fin>>ch;

        if(ch=='I')

        {

            fin>>poz>>f;

            p=rand()%DN+1;

            ma=max(ma,p);

            ins(t,f,p,poz-1);

            continue;

        }

        if(ch=='R')

        {

            fin>>f>>g;

            rev(f,g);

            continue;

        }

        if(ch=='A')

        {

            fin>>poz;

            gaseste(t,poz);

            continue;

        }

        fin>>f>>g;

        del2(f,g);

    }

    afis(t);

}