Cod sursa(job #1813546)

Utilizator binicBinica Nicolae binic Data 23 noiembrie 2016 00:18:37
Problema Secv8 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.86 kb
#include <bits/stdc++.h>
#define INF (1<<30)+5
using namespace std;
int Q, ok, poz, val, x1, x2, x;
char op;

struct T
{
    int val, pr, sz, inv;
    T *l,*r;
    T () {}
    T (int val, int pr, int sz, int inv, T *l, T *r)
    {
        this->val=val;
        this->pr=pr;
        this->sz=sz;
        this->inv=inv;
        this->l=l;
        this->r=r;
    }
}*R, *nil, *aux, *T1, *T2, *T3;

void reup(T *&nod)
{
    nod -> sz = nod -> r -> sz + nod -> l -> sz + 1 ;
}
void push(T *&nod)
{
    if(nod->inv == 1)
    {
        T *aux = nod->l;
        nod->l = nod->r;
        nod->r = aux;
        nod->l->inv ^=1;
        nod->r->inv ^=1;
        nod->inv=0;
    }
}

void rotleft(T *&nod)
{
    push(nod);
    T *aux1 = nod->l;
    push(aux1);
    nod->l=aux1->r;
    aux1->r=nod;
    reup(nod);
    reup(aux1);
    nod=aux1;
}
void rotright(T *&nod)
{
    push(nod);
    T *aux1 = nod->r;
    push(aux1);
    nod->r=aux1->l;
    aux1->l=nod;
    reup(nod);
    reup(aux1);
    nod=aux1;
}
void balance(T *&nod)
{
    push(nod);
    if(nod->l->pr > nod->pr)
        rotleft(nod);
    else if(nod->r->pr > nod->pr)
        rotright(nod);
}

int acces(T *&nod, int poz)
{
    push(nod);
    if( nod->l->sz +1 == poz)
        return nod->val;
    else if( nod->l->sz + 1 > poz)
        return acces( nod->l, poz);
    else
        return acces( nod->r, poz - nod->l->sz - 1 );
}

void ins(T *&nod, int val, int pr, int poz)
{
    if(nod == nil)
    {
        nod = new T(val, pr, 1, 0, nil, nil);
        return ;
    }
    push(nod);
    if( nod->l->sz + 1 >= poz)
        ins( nod->l, val, pr, poz);
    else
        ins(nod->r, val, pr, poz - nod->l->sz - 1 );

    reup(nod);
    balance(nod);
}

void del(T *&nod,  int poz)
{
    if(nod == nil)return;
    push(nod);
    if( nod->l->sz + 1 > poz)
        del( nod->l, poz);
    else if( nod->l->sz + 1 < poz)
        del( nod->r, poz - nod->l->sz - 1 );
    else
    {
        if(nod->l == nil && nod->r == nil)
        {
            delete nod;
            nod=nil;
            return ;
        }
        else
        {
            if(nod->l->pr > nod->r->pr)
                rotleft(nod);
            else rotright(nod);
            del(nod, poz);
        }
    }
    reup(nod);
}

void split( T *&R,  T *&TL,  T *&TR, int poz)
{
    ins(R, 0, INF, poz);
    TL = R->l;
    TR = R->r;
    delete R;
    R = nil;
}

void join( T *&R, T *&TS, T *& TR )
{
    R = new T( 0, 0, TS->sz + TR->sz + 1, 0, TS, TR);
    del(R, TS->sz + 1);
}

void afis(T *&nod)
{
    if(nod == nil) return ;
    push(nod);
    afis(nod->l);
    printf("%d ",nod->val);
    afis(nod->r);
}

int main()
{
    freopen("secv8.in","r",stdin);
    freopen("secv8.out","w",stdout);
    srand(unsigned(time(0)));
    R=nil=new T(0,0,0,0,NULL,NULL);
    scanf("%d %d\n",&Q,&ok);
    while(Q--)
    {
        scanf("%c ",&op);
        if(op == 'I')
        {
            scanf("%d %d\n",&poz, &val);
            ins(R,val,rand()+1,poz);

        }
        else if(op == 'A')
        {
            scanf("%d \n",&x);
            printf("%d\n",acces(R,x));
        }
        else if(op == 'D')
        {
            scanf("%d %d\n",&x1,&x2);
            ///split(R,T1,T2,x1);
            ///split(T2,aux,T3,x2 - T1->sz +1 );
            split(R,aux,T3,x2+1);
            split(aux,T1,T2,x1);
            join(R, T1, T3);
        }
        else if(op == 'R')
        {
            scanf("%d %d\n",&x1,&x2);
            ///split(R,T1,T2,x1);
            ///split(T2,aux,T3,x2- T1->sz +1);
            split(R,aux,T3,x2+1);
            split(aux,T1,T2,x1);
            //T2=aux;
            T2->inv ^=1;
            aux=nil;
            join(aux,T1,T2);
            join(R,aux ,T3);
        }
    }
    afis(R);printf("\n");
    return 0;
}