#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<map>
#include<ctime>
#include<set>
#include<queue>
#include<cmath>
#include<vector>
#include<bitset>
#include<functional>
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define REP(i,l,r) for((i)=(l);(i)<=(r);++(i))
#define REP2(i,l,r) for((i)=(l);(i)!=(r);++(i))
using namespace std;
typedef long long LL;
typedef double ld;
const int MAX=100000+10;
const int NUM=3;
int ran()
{
static int seed=222313214;
seed+=(seed<<2)+1;
return seed;
}
struct node
{
int key,weight;//weight值大的在上面
node *lc,*rc;
bool rev;
int add;
int size;//记录子树大小
int ma;//记录子树中的最小值
int cnt;//引用计数
node(){}
friend node* downit(node* ,int rev,int add);
void down()
{
if(lc)lc=downit(lc,rev,add);
if(rc)rc=downit(rc,rev,add);
add=0;rev=0;
}
void update()
{
size=1;
ma=key;
if(lc)
{
size+=lc->size;
if(lc->ma<ma)
ma=lc->ma;
}
if(rc)
{
size+=rc->size;
if(rc->ma<ma)
ma=rc->ma;
}
}
}tree[MAX*NUM];
int pool[MAX*NUM+100],top;
node* mynew()
{
return tree+pool[top--];
}
void mydelete(node* p)
{
pool[++top]=p-tree;
}
node* newnode(int _key,int _weight,node* _lc,node* _rc)
{
node* p=mynew();
p->key=p->ma=_key;
p->weight=_weight;
p->lc=_lc;
p->rc=_rc;
p->rev=0;
p->add=p->cnt=0;
p->size=1;
if(p->lc)++p->lc->cnt;
if(p->rc)++p->rc->cnt;
return p;
}
node* newnode(node* c)
{
node* p=mynew();
*p=*c;
p->cnt=0;
if(p->lc)++p->lc->cnt;
if(p->rc)++p->rc->cnt;
return p;
}
void delthem(node* u)
{
if(!u)
return;
--u->cnt;
if(u->cnt==0)
{
delthem(u->lc);
delthem(u->rc);
mydelete(u);
}
}
void relax(node* u)
{
if(!u || u->cnt>0)
return;
delthem(u->lc);
delthem(u->rc);
mydelete(u);
}
node* down(node* u)
{
if(!u)return u;
node* p=newnode(u);
if(!p->add && !p->rev)
return p;
p->down();
return p;
}
node* downit(node* p,int rev,int add)
{
if(!p)return 0;
node* np=newnode(p);
if(rev)
{
swap(np->lc,np->rc);
np->rev^=rev;
}
if(add)
{
np->key+=add;
np->add+=add;
np->ma+=add;
}
--p->cnt;
relax(p);
++np->cnt;
return np;
}
node* merge(node* a,node* b)
{
if(!b)return newnode(a);
if(!a)return newnode(b);
a=down(a);
b=down(b);
node* np;
if( a->weight > b->weight )
np=newnode( a->key,a->weight,a->lc,merge(a->rc,b) );
else
np=newnode( b->key,b->weight,merge(a,b->lc),b->rc );
np->update();
relax(a);
relax(b);
return np;
}
node* spilt_l(node* a,int left)
{
if(!a || left==0)
return 0;
a=down(a);
int size_l=(a->lc?a->lc->size:0)+1;
node* np;
if(size_l<=left)
{
np=newnode(a->key,a->weight,a->lc,spilt_l(a->rc,left-size_l));
np->update();
}
else np=spilt_l(a->lc,left);
relax(a);
return np;
}
node* spilt_r(node* a,int right)
{
if(!a || right==0)
return 0;
a=down(a);
int size_r=(a->rc?a->rc->size:0)+1;
node* np;
if(size_r<=right)
{
np=newnode(a->key,a->weight,spilt_r(a->lc,right-size_r),a->rc);
np->update();
}
else np=spilt_r(a->rc,right);
relax(a);
return np;
}
node* insert(node* a,int left,node* p)
{
if(!a)return p;
a=down(a);
int size_l=(a->lc?a->lc->size:0)+1; node* np;
if(p->weight<a->weight)
{
if(size_l<=left) np=newnode( a->key , a->weight , a->lc,insert(a->rc,left-size_l,p) );
else np=newnode( a->key , a->weight , insert(a->lc,left,p) ,a->rc );
}
else
np=newnode(p->key,p->weight,spilt_l(a,left),spilt_r(a,a->size-left));
np->update();
relax(a);
return np;
}
node* get_tree(node* Root,int a,int b)
{
node* tmp=spilt_l(Root,b);
node* p=spilt_r(tmp,b-a+1);
relax(tmp);
return p;
}
int n,m;
int a[MAX];
node* root;
void doit(node*& ll,node*& mid,node*& rr,int a,int b)
{
ll=spilt_l(root,a-1);
rr=spilt_r(root,root->size-b);
mid=get_tree(root,a,b);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#endif
int i;
REP2(i,0,MAX*NUM-10)
pool[++top]=i;
scanf("%d",&n);
REP(i,1,n)
{
scanf("%d",&a[i]);
node* tmp=root;
root=insert(root,i-1,newnode(a[i],ran(),0,0));
relax(tmp);
}
scanf("%d",&m);
REP(i,1,m)
{
char Q[100];
scanf("%s",Q);
int a,b,c;
node* ll(0),*rr(0),*mid(0),*tmp;
if(Q[0]=='A')
{
scanf("%d%d%d",&a,&b,&c);
doit(ll,mid,rr,a,b);
mid=downit(mid,0,c);--mid->cnt;
relax(root);
root=merge( tmp=merge(ll,mid) , rr );
relax(ll);relax(mid);relax(rr);
relax(tmp);
}
else if(Q[0]=='R' && Q[3]=='E')
{
scanf("%d%d",&a,&b);
doit(ll,mid,rr,a,b);
mid=downit(mid,1,0);--mid->cnt;
relax(root);
root=merge( tmp=merge(ll,mid) , rr );
relax(ll);relax(mid);relax(rr);
relax(tmp);
}
else if(Q[0]=='R' && Q[3]=='O')
{
scanf("%d%d%d",&a,&b,&c);
c=c%(b-a+1);
if(!c)continue;
doit(ll,mid,rr,a,b);
node* e=spilt_r(mid,c);
node* f=spilt_l(mid,mid->size-c);
relax(root);
node* tmp2;
root=merge( tmp=merge(ll,tmp2=merge(e,f)) , rr );
relax(ll);relax(mid);relax(rr);
relax(e);relax(f);
relax(tmp);relax(tmp2);
}
else if(Q[0]=='I')
{
scanf("%d%d",&a,&b);
tmp=root;
root=insert(root,a,newnode(b,ran(),0,0));
relax(tmp);
}
else if(Q[0]=='D')
{
scanf("%d",&a);
ll=spilt_l(root,a-1);
rr=spilt_r(root,root->size-a);
relax(root);
root=merge(ll,rr);
relax(ll);relax(rr);
}
else if(Q[0]=='M')
{
scanf("%d%d",&a,&b);
node* p=get_tree(root,a,b);
printf("%d\n",p->ma);
relax(p);
}
}
return 0;
}
I2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTxjc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