# include <iostream>
# include <fstream>
# include <cstdlib>
# include <cassert>
using namespace std;
struct Node
{
int val, prio;
int w;
bool rvs;
Node * left, * right;
Node( int _val, int _prio = -2, Node * _left = NULL, Node * _right = NULL )
{
val = _val;
if ( _prio == -2 )
prio = rand();
else
prio = _prio;
rvs = 0;
left = _left;
right = _right;
w = 1 + ( left == NULL ? 0 : left->w ) + ( right == NULL ? 0 : right->w );
}
~Node()
{
delete left;
delete right;
}
};
class Treap
{
public:
Treap();
int kth( int pos );
void push( int pos, int val );
void pop( int pos );
void pop( int a, int b );
void reverse( int a, int b );
void print( ofstream &fout );
int size();
private:
Node * root;
int search( Node * &n, int pos );
void insert( Node * &n, int pos, int val, int prio );
void erase( Node * &n, int pos );
void split( Node * &t, int pos, Node * &t1, Node * &t2 );
void join( Node * &t1, Node * &t2, Node * &t );
void printNode( Node * &n, ofstream &fout );
int weight( Node * &n );
void updateWeight( Node * &n );
void prop( Node * &n );
void balance( Node * &n );
void rotLeft( Node * &n );
void rotRight( Node * &n );
};
void Treap::updateWeight( Node * &n )
{
n->w = 1 + weight( n->left ) + weight( n->right );
}
int Treap::weight( Node * &n )
{
return ( n == NULL ? 0 : n->w );
}
void Treap::rotRight( Node * &n )
{
prop( n );
prop( n->right );
Node * t = n->right;
n->right = t->left;
t->left = n;
n = t;
updateWeight( n->left );
updateWeight( n );
}
void Treap::rotLeft( Node * &n )
{
prop( n );
prop( n->left );
Node * t = n->left;
n->left = t->right;
t->right = n;
n = t;
updateWeight( n->right );
updateWeight( n );
}
void Treap::prop( Node * &n )
{
if ( n != NULL && n->rvs == true ) {
swap( n->left, n->right );
n->rvs = false;
if ( n->left != NULL )
n->left->rvs ^= 1;
if ( n->right != NULL )
n->right->rvs ^= 1;
}
}
void Treap::balance( Node * &n )
{
if ( n->prio == -1 && n->left == NULL && n->right == NULL ) {
delete n;
n = NULL;
} else {
prop( n );
if ( n->left != NULL && n->left->prio > n->prio ) {
rotLeft( n );
balance( n->right );
}
if ( n->right != NULL && n->right->prio > n->prio ) {
rotRight( n );
balance( n->left );
}
updateWeight( n );
}
}
void Treap::printNode( Node * &n, ofstream &fout )
{
prop( n );
if ( n == NULL )
return;
printNode( n->left, fout );
fout << n->val << ' ';
printNode( n->right, fout );
}
void Treap::join( Node * &t1, Node * &t2, Node * &t )
{
t = new Node( 0, -1, t1, t2 );
updateWeight( t );
balance( t );
}
void Treap::split( Node * &t, int pos, Node * &t1, Node * &t2 )
{
insert( t, pos, 0, RAND_MAX );
t1 = t->left;
t2 = t->right;
t = NULL;
}
void Treap::erase( Node * &n, int pos )
{
prop( n );
if ( pos <= weight( n->left ) )
erase( n->left, pos );
else if ( pos == weight( n->left ) + 1 )
n->prio = -1;
else
erase( n->right, pos - weight( n->left ) - 1 );
balance( n );
updateWeight( n );
}
void Treap::insert( Node * &n, int pos, int val, int prio = -2 )
{
if ( n == NULL )
n = new Node( val, prio );
else {
prop( n );
if ( pos <= weight( n->left ) )
insert( n->left, pos, val, prio );
else
insert( n->right, pos - weight( n->left ) - 1, val, prio );
balance( n );
updateWeight( n );
}
}
int Treap::search( Node * &n, int pos )
{
prop( n );
if ( pos <= weight( n->left ) )
return search( n->left, pos );
else if ( pos == weight( n->left ) + 1 )
return n->val;
else
return search( n->right, pos - weight( n ->left ) - 1 );
}
int Treap::size()
{
return weight( root );
}
void Treap::print( ofstream &fout )
{
printNode( root, fout );
fout << endl;
}
void Treap::reverse( int a, int b )
{
assert( 1 <= a && a <= b && b <= size() );
Node * t1, * t2, *t3, * aux;
split( root, a - 1, t1, aux );
split( aux, b - a + 1, t2, t3 );
t2->rvs ^= 1;
join( t2, t3, aux );
join( t1, aux, root );
}
void Treap::pop( int a, int b )
{
assert( 1 <= a && a <= b && b <= size() );
Node * t1, * t2, *t3, * aux;
split( root, a - 1, t1, aux );
split( aux, b - a + 1, t2, t3 );
join( t1, t3, root );
delete t2;
}
void Treap::pop( int pos )
{
assert( 1 <= pos && pos <= size() );
erase( root, pos );
}
void Treap::push( int pos, int val )
{
assert( 1 <= pos && pos <= size() + 1 );
insert( root, pos - 1, val );
}
int Treap::kth( int pos )
{
assert( 1 <= pos && pos <= size() );
return search( root, pos );
}
Treap::Treap()
{
root = NULL;
srand( time( NULL ) );
}
int main()
{
ifstream fin( "secv8.in" );
ofstream fout( "secv8.out" );
int n, r, i, a, b;
char ch;
Treap t;
fin >> n >> r;
for ( i = 0; i < n; i ++ ) {
fin >> ch;
if ( ch == 'I' ) {
fin >> a >> b;
t.push( a, b );
} else if ( ch == 'A' ) {
fin >> a;
fout << t.kth( a ) << '\n';
} else if ( ch == 'R' ) {
fin >> a >> b;
t.reverse( a, b );
} else {
fin >> a >> b;
t.pop( a, b );
}
}
t.print( fout );
return 0;
}