Cod sursa(job #2341604)

Utilizator isav_costinVlad Costin Andrei isav_costin Data 11 februarie 2019 23:56:51
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.51 kb
#include <fstream>
#include <cstdlib>

using namespace std;

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

struct Treap;
Treap* NIL;

typedef pair<Treap*,Treap*> Trip;

struct Treap
{
  int val, p, size, lazy;
  Treap *st, *dr;

  Treap( int x )
  {
    val=x;
    p=rand();
    size=1;
    lazy=0;

    st=NIL, dr=NIL;
  }

  void resize( )
  {
    size=st->size+dr->size+1;
  }

  void propagate( )
  {
    if( lazy )
    {
      lazy=0;

      swap(st,dr);

      st->lazy=!st->lazy;
      dr->lazy=!dr->lazy;
    }
  }
};

Treap* join( Treap* a, Treap* b )
{
  if( a==NIL )
    return b;

  if( b==NIL )
    return a;

  a->propagate();
  b->propagate();

  if( a->p>b->p )
  {
    a->dr=join(a->dr,b);
    a->resize();

    return a;
  }
  else
  {
    b->st=join(a,b->st);
    b->resize();

    return b;
  }
}

Trip split( Treap* x, int pos )
{
  if( x==NIL )
    return {NIL,NIL};

  x->propagate();

  if( pos<=x->st->size )
  {
    Trip t=split(x->st,pos);

    x->st=t.second;
    x->resize();

    return {t.first,x};
  }
  else
  {
    Trip t=split(x->dr,pos-x->st->size-1);

    x->dr=t.first;
    x->resize();

    return {x,t.second};
  }
}

int access( Treap* x, int pos )
{
  x->propagate();

  if( pos<=x->st->size )
    return access(x->st,pos);

  if( pos>x->st->size+1 )
    return access(x->dr,pos-x->st->size-1);

  return x->val;
}

int main()
{
  int n, fstf;

  fin>>n>>fstf;

  NIL=new Treap(-1);
  NIL->size=0;
  NIL->p=-1;
  NIL->st=NIL->dr=NIL;

  Treap* root=NIL;

  for( int i=1;i<=n;i++ )
  {
    char ch;

    fin>>ch;

    if( ch=='I' )
    {
      int k, e;

      fin>>k>>e;

      Trip t=split(root,k-1);
      root=join(t.first,join(new Treap(e), t.second));
    }
    else
      if( ch=='A' )
      {
        int k;

        fin>>k;

        fout<<access(root,k)<<'\n';
      }
      else
        if( ch=='R' )
        {
          int l, r;

          fin>>l>>r;

          Trip t1=split(root,l-1);
          Trip t2=split(t1.second,r-l+1);

          t2.first->lazy=!t2.first->lazy;

          root=join(t1.first,join(t2.first,t2.second));
        }
        else
        {
          int l, r;

          fin>>l>>r;

          Trip t1=split(root,l-1);
          Trip t2=split(t1.second,r-l+1);

          root=join(t1.first,t2.second);
        }
  }

  for( int i=1;i<=root->size;i++ )
    fout<<access(root,i)<<" ";

  return 0;
}