Cod sursa(job #3232947)

Utilizator puica2018Puica Andrei puica2018 Data 2 iunie 2024 07:35:09
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.31 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("secv8.in");
ofstream fout("secv8.out");

const int mod=(int)1e9+7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

class Treap
{
public:
	int val;
	int prior;

	array <Treap*,2> sons;

	int sz;

	bool rev;

	Treap(int _val);
} *T,*t;

int sz(Treap *T)
{
    if(T==NULL)
        return 0;

    return T->sz;
}

void recalc(Treap *T)
{
	if (T==NULL)
        return;

	T->sz=1;

	for (auto son:T->sons)
        if (son!=NULL)
            T->sz+=son->sz;
}

Treap::Treap(int _val)
{
    sons={NULL, NULL};

    sz=1;

    val=_val;

    rev=0;

    prior=rng()%mod;
}

void prop(Treap *T);

Treap* Merge(Treap *l,Treap *r);

void prop(Treap *T)
{
	if (T==NULL)
        return;

	if (T->rev==0)
        return;

	for (auto son:T->sons)
        if (son!=NULL)
            son->rev^=1;

    swap(T->sons[0],T->sons[1]);

	T->rev=0;
}

void Print(Treap *T)
{
    if(T==NULL)
        return;

    prop(T);

    Print(T->sons[0]);

    fout<<T->val<<" ";

    Print(T->sons[1]);
}

Treap* Merge(Treap *l,Treap *r)
{
	if (l==NULL)
        return r;

	if (r==NULL)
        return l;

	prop(l);
	prop(r);

	if (l->prior>r->prior)
    {
		l->sons[1]=Merge(l->sons[1],r);

		recalc(l);

		return l;
	}
	else
	{
		r->sons[0]=Merge(l,r->sons[0]);

		recalc(r);

		return r;
	}
}

array <Treap*,2> split(Treap *T,int ind)
{
	if (T==NULL)
        return {NULL,NULL};

	prop(T);

	if (sz(T->sons[0])>=ind)
    {
        array <Treap*,2> aux=split(T->sons[0],ind);

		T->sons[0]=aux[1];

		recalc(T);

		return {aux[0],T};
	}
	else
	{
        ind-=sz(T->sons[0])+1;

		array <Treap*,2> aux=split(T->sons[1],ind);

        T->sons[1]=aux[0];

		recalc(T);

		return {T,aux[1]};
	}

	return {NULL,NULL};
}

Treap* Insert(Treap *T,int k,int v)
{
    t=new Treap(v);

    array <Treap*,2> a=split(T,k-1);

    return Merge(a[0],Merge(t,a[1]));
}

int valAtPos(Treap *T,int pos1,int pos2)
{
    if(T==NULL)
        return 0;

    prop(T);

    int aux=pos2+sz(T->sons[0]);

    if(aux>=pos1)
        return valAtPos(T->sons[0],pos1,pos2);

    if(aux+1==pos1)
        return T->val;

    return valAtPos(T->sons[1],pos1,aux+1);
}

Treap* Reverse(Treap *T,int l,int r)
{
    array <Treap*,2> a=split(T,l-1);
	array <Treap*,2> b=split(a[1],r-l+1);

    b[0]->rev=1;

	return Merge(a[0],Merge(b[0],b[1]));
}

Treap* Delete(Treap *T,int l,int r)
{
    array <Treap*,2> a=split(T,l-1);
	array <Treap*,2> b=split(a[1],r-l+1);

	return Merge(a[0],b[1]);
}

int q;

bool c;

int main()
{
    fin>>q>>c;

    while(q--)
    {
        char tip;
        fin>>tip;

        if(tip=='I')
        {
            int k,v;
            fin>>k>>v;

            T=Insert(T,k,v);
        }

        if(tip=='A')
        {
            int k;
            fin>>k;

            fout<<valAtPos(T,k,0)<<"\n";
        }

        if(tip=='R')
        {
            int i,j;
            fin>>i>>j;

            T=Reverse(T,i,j);
        }

        if(tip=='D')
        {
            int i,j;
            fin>>i>>j;

            T=Delete(T,i,j);
        }
    }

    Print(T);

    fout<<"\n";

	return 0;
}